Skip to content

dinitctl: fix a possible oom on offline enable/disable with service@arg - #596

Open
mobin-2008 wants to merge 2 commits into
davmac314:masterfrom
mobin-2008:dinitctl_offline_enable_disable_fixes
Open

mobin-2008 wants to merge 2 commits into
davmac314:masterfrom
mobin-2008:dinitctl_offline_enable_disable_fixes

Conversation

@mobin-2008

Copy link
Copy Markdown
Collaborator

Use the service name directly instead of creating a string_view in find_service_desc() when combining the paths.

It didn't lead to "out-of-memory" when the service name was shorter than 16 characters.

This is a regression since 2a211b6 (introduced back in 0.20.0). The previous code assumed that the service name includes the service argument thus it created a string_view with the service name as the base and (service argument - service name) as the count. It doesn't include the service argument anymore, causing the string_view to allocate a wrong size buffer and potentially, causing dinitctl to crash.

I found this when offline enabling a service.
Fixed the issue and also included a integration test.

Tested on my own Artix.

Use the service name directly instead of creating a string_view in
find_service_desc() when combining the paths.

It didn't lead to "out-of-memory" when the service name was shorter
than 16 characters.

This is a regression since 2a211b6
(introduced back in 0.20.0). The previous code assumed that the service
name includes the service argument thus it created a string_view with
the service name as the base and (service argument - service name) as the
count. It doesn't include the service argument anymore, causing the
string_view to allocate a wrong size buffer and potentially, causing
dinitctl to crash.
Check for dinitctl when it enables and disables a service with argument
while offline.
@mobin-2008 mobin-2008 added Bug/Bugfix C-dinitctl Things about dinitctl labels Nov 8, 2026
@davmac314

Copy link
Copy Markdown
Owner

A shame this was found only after release, oh well. Good to get it fixed, but I have several review comments to follow.

@davmac314 davmac314 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Commit comments:

dinitctl: fix a possible oom on offline enable/disable with service@arg

I guess you're probably trying to highlight the user-visible effect, but this is actually a definite buffer overrun and that's much more significant than the (potential) out-of-memory.

You can mention "possible crash" if there is space. If not, you can mention it in the message body instead. Don't use non-standard terms like "oom" (especially not without at least expanding the acronym in the message body).

Use the service name directly instead of creating a string_view in
find_service_desc() when combining the paths.

This comment doesn't make sense; a string_view is using the service name directly (if done correctly).

For "when combining the paths" - why not "as an argument to combine_paths(...)"? You don't have to obscure things, just say what the change is.

It didn't lead to "out-of-memory" when the service name was shorter
than 16 characters.

"it" can't be used like this. "It" means either "what was mentioned immediately before" (which in this case is the fix/change - doesn't make sense!) or "the general state of everything" (as in "it is raining" - again, doesn't make sense in this context). You would need to replace "it" with some short description of what it is that you are actually talking about -

But, I don't this sentence even belongs in the commit message. The service name being shorter than 16 characters isn't significant nor is it definite - it's just an artifact of the particular std::string implementation on a given system.

The previous code

Don't explain in this commit message what changed in an earlier commit. This commit message is for explaining what changed in this commit.

This commit message should explain what was wrong (after it broke), that this commit fixes. That should be done mostly in the first paragraph. If it needs further explanation, add that here. If it doesn't, don't.

assumed that the service name includes the service argument

I think you're talking about variables in code but you're describing them instead as concepts ("service name", "service argument"), which makes this confusing (in a sense, the service name does include the service argument, if there is one - that's independent of the code).

If you mean that it assumed some variable contained some value, say that, and refer to the variable by its name.

causing the string_view to allocate a wrong size buffer

string_view doesn't allocate any buffer.

and potentially, causing dinitctl to crash.

There shouldn't be a comma there.

The possible crash should be mentioned in the first paragraph, it's the direct consequence of the bug and is resolved via the fix.

}

mkdir(sd_dir.c_str(), S_IRWXU);
cp_file((igr_input_basedir + "/svc-arg-enable-offline/sd/AAAAAAAAAAAAAAAA").c_str(),

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

The filename seems like it's screaming at me. AAAAAAAA.....

I know the other offline disable test also uses a capital letter (A) but it's an exception to the general style of the tests (probably should be changed) and it's more of a "capital at the start" scenario. This just looks really odd - and more significantly there is no explanation at all why such a filename is used (you could even have made the name self-explanatory in this case! But a comment as well would help).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

In the real world, most service names have more length than 1 character. I probably change it to another name that makes more sense.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

In the real world, most service names have more length than 1 character

Yes but

  1. this is a test, not "the real world"
  2. real service names typically reflect what program or service they run

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

What I mean is that, it can be closer to the real world. I personally prefer something like service in comparison to A

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

It should be consistent with the other tests, unless there is a reason not to be.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

So I go with A for now.

@davmac314 davmac314 Nov 12, 2026 •

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Things like this seem to come up a bit, so let me be clear:

  • you should always be consistent with existing code/practice, unless there's a good reason for an exception - and in that case, make it clear what that reason is, in the PR (PR description, and/or comments in the code).
  • if you think the existing code/practice isn't good, you should either:
    • make it clear (in the PR description) that you have deliberately broken the convention, and make a case that we should change it going forward, or
    • open a discussion about it (either an issue, or email, or whatever), and if there's agreement then you can fix every case in a separate PR, rather than just the current one. In that case the current PR should be consistent with existing practice.

Don't just open a PR with new code or change that is inconsistent with what's already in the codebase, not saying anything at all, and then argue when I ask for consistency, please.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I really didn't think it was breaking the convention, now looking at it, it does break the convention.

if you think the existing code/practice isn't good, you should either:
make it clear (in the PR description) that you have deliberately broken the convention, and make a case that we should change it going forward, or
open a discussion about it (either an issue, or email, or whatever), and if there's agreement then you can fix every case in a separate PR, rather than just the current one. In that case the current PR should be consistent with existing practice.

I'm still working on my communication skills, sorry.

rm_tree(sd_dir.c_str());
}

void svc_arg_enable_offline_test()

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

This is in general so much of a copy of offline_enable_test that it should be incorporated into it, I don't think it makes sense to have another test especially as it's basically copy-pasted code.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

(I.e. in modify that test to test both a service without argument and service with argument).

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Or alternatively factor out the common parts into one or more functions.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I guess it's fine to leave the refactoring out of this PR, if you prefer. Either you can do it as a separate PR or I can manage it.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I will refactor the offline-enable test to also test against a service with an argument, in this PR.

Comment thread src/dinitctl.cc

for (std::string path : paths) {
std::string test_path = combine_paths(path, ::string_view(svc_name, at_ptr - svc_name));
std::string test_path = combine_paths(path, svc_name);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

By specifying svc_name here, you are forcing the string length to be counted again in order to construct a string_view. The length was basically just calculated by the loop above (the original at_ptr - svc_name is the correct length if svc_name hasn't been reassigned) - so use the calculated length, just fix it so that it won't use a wrong value if svc_name does get reassigned on line 2075.

Comment thread src/dinitctl.cc
@@ -2076,7 +2076,7 @@ static std::pair<int,int> find_service_desc(const char *svc_name, const std::vec
}

for (std::string path : paths) {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

This isn't a problem with your change, but could you please change path to const std::string &path (or just std::string &path if it can't be const, though I think that will be fine).

@mobin-2008

Copy link
Copy Markdown
Collaborator Author

Commit comments:

dinitctl: fix a possible oom on offline enable/disable with service@arg

I guess you're probably trying to highlight the user-visible effect, but this is actually a definite buffer overrun and that's much more significant than the (potential) out-of-memory.

Will be changed to buffer overrun, may causing crash.

You can mention "possible crash" if there is space. If not, you can mention it in the message body instead. Don't use non-standard terms like "oom" (especially not without at least expanding the acronym in the message body).

The "oom" will be removed from the subject.

Use the service name directly instead of creating a string_view in
find_service_desc() when combining the paths.

This comment doesn't make sense; a string_view is using the service name directly (if done correctly).

I replaced the string_view construction with only the service name. The string_view only served for cutting down the service name containing the service argument, which is wrong now as explained later in the commit. Maybe "Use the service name without cutting the service argument from it" and then change the explanation to explain that the cut is no longer needed. (I read the code comments just now and you mentioned that there was an additional purpose for that string_view, will restore it as the code comment suggests).

For "when combining the paths" - why not "as an argument to combine_paths(...)"? You don't have to obscure things, just say what the change is.

Ok.

It didn't lead to "out-of-memory" when the service name was shorter
than 16 characters.

"it" can't be used like this. "It" means either "what was mentioned immediately before" (which in this case is the fix/change - doesn't make sense!) or "the general state of everything" (as in "it is raining" - again, doesn't make sense in this context). You would need to replace "it" with some short description of what it is that you are actually talking about

But, I don't this sentence even belongs in the commit message. The service name being shorter than 16 characters isn't significant nor is it definite - it's just an artifact of the particular std::string implementation on a given system.

This section will be removed due to being not limited to any service name length.

The previous code

Don't explain in this commit message what changed in an earlier commit. This commit message is for explaining what changed in this commit.

This commit message should explain what was wrong (after it broke), that this commit fixes. That should be done mostly in the first paragraph. If it needs further explanation, add that here. If it doesn't, don't.

Ok.

assumed that the service name includes the service argument

I think you're talking about variables in code but you're describing them instead as concepts ("service name", "service argument"), which makes this confusing (in a sense, the service name does include the service argument, if there is one - that's independent of the code).

If you mean that it assumed some variable contained some value, say that, and refer to the variable by its name.

causing the string_view to allocate a wrong size buffer

string_view doesn't allocate any buffer.

and potentially, causing dinitctl to crash.

There shouldn't be a comma there.

The possible crash should be mentioned in the first paragraph, it's the direct consequence of the bug and is resolved via the fix.

Ok. Variables will be used instead of concepts, I will mention that the combine_paths() combines using std::string (which allocates the memory) and fix that extra comma.

@davmac314

Copy link
Copy Markdown
Owner

Maybe "Use the service name without cutting the service argument from it" and then change the explanation to explain that the cut is no longer needed.

But the argument has been cut from the name, and it does need to be. Do you understand the bug here? It sounds a little bit like you're not sure what's really happening.

@mobin-2008

Copy link
Copy Markdown
Collaborator Author

Maybe "Use the service name without cutting the service argument from it" and then change the explanation to explain that the cut is no longer needed.

But the argument has been cut from the name, and it does need to be. Do you understand the bug here? It sounds a little bit like you're not sure what's really happening.

Yes, I think I do. I mean the cut was already done but I didn't mention it here.

@davmac314

Copy link
Copy Markdown
Owner

Yes, I think I do. I mean the cut was already done but I didn't mention it here.

Ok. Please try to make the commit message clear and direct.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug/Bugfix C-dinitctl Things about dinitctl

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants