Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Join declarations and assignments #13025

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

tygyh
Copy link
Contributor

@tygyh tygyh commented Aug 19, 2024

No description provided.

SplitPathResult result;

result = {"/shared1", "00000042.app"};
SplitPathResult result = {"/shared1", "00000042.app"};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This reduces readability of the test imo.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is less clear about this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This reduces readability of the test imo.

I also think the same, as result will be assigned different values depending on the test. Leaving the declaration (without initialization) at the beginning makes the intent clearer that this variable might be reused with new values.

Comment on lines +411 to +412
std::unique_ptr<StagingBuffer> temp_buffer = StagingBuffer::Create(
STAGING_BUFFER_TYPE_UPLOAD, upload_size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This affects object lifetime, not sure whether that matters but just noting...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact, I'm pretty sure this breaks the code, looking at this in more detail. upload_buffer keeps a pointer to the now out-of-scope temp_buffer->GetBuffer().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see how this breaks anything

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As @AdmiralCurtiss said, upload_buffer will point to a destroyed object (held by the unique_ptr) when exiting the current else statement (i.e. which will destroy temp_buffer declared in this scope).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it wasn't caught when building I didn't think it would be an issue.

Do you have any tips for how to avoid this in the future? (Other than switching to Rust :P)


if (is_down)
{
utf8 = key_event->text().toUtf8();
QByteArray utf8 = key_event->text().toUtf8();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same problem here. utf8 goes out of scope but chars keeps a pointer to it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see what's wrong here either

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When exiting the if (is_down) statement, utf8 will be destroyed and chars will become a dangling pointer pointing to destroyed data (see chars = utf8.constData()).

@AdmiralCurtiss
Copy link
Contributor

Stopping here because I must ask: Did you even check these refactorings?

Copy link
Contributor

@mitaclaw mitaclaw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here are my findings. I think these changes can be good.

Source/Core/Core/DSP/DSPAssembler.cpp Outdated Show resolved Hide resolved
Source/Core/Core/HW/GCMemcard/GCMemcard.cpp Outdated Show resolved Hide resolved
Source/Core/Core/IOS/Network/IP/Top.cpp Outdated Show resolved Hide resolved
Source/Core/Core/PowerPC/GDBStub.cpp Outdated Show resolved Hide resolved
@@ -923,16 +907,11 @@ static void HandleAddBreakpoint()

static void HandleRemoveBreakpoint()
{
u32 type, addr, len, i;
u32 type(Hex2char(s_cmd_bfr[1])), addr(0), len(0), i(3);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would rather the declaration and assignment of addr, len, and i be moved down to where it is used (where the assignment was previously). Also, why suddenly parenthesis for initialization?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've seen both parentheses and equal signs used for initialization in the codebase and I didn't see any pattern for when to choose which.
I probably edited this part right after some part which used parentheses.

@tygyh tygyh force-pushed the Join-declarations-and-assignments branch from 286dcf8 to 300158a Compare August 20, 2024 12:11
@tygyh tygyh force-pushed the Join-declarations-and-assignments branch from 300158a to 69fcf27 Compare August 27, 2024 21:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants