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

Fix corner cases and style in the argv/argc code. #118

Merged
merged 1 commit into from
Oct 22, 2019
Merged

Conversation

sunfishcode
Copy link
Member

This fixes some potential memory leaks in pathological situations, and
updates the code to use //-style comments.

This is a subset of #112, extracted for easier reviewing.

@sunfishcode
Copy link
Member Author

@tschneidereit There's a Windows CI failure affecting this and other PRs which seems unrelated to the actual PRs; do you know what might be causing this?

The error message is:

error: could not remove 'setup' file: 'C:\Users\VssAdministrator\.cargo\bin/rustup-init.exe'
info: caused by: Access is denied. (os error 5)

@alexcrichton
Copy link
Collaborator

@sunfishcode this is unfortunately a known bug, but you can fix it by passing --no-self-update to rustup update

sunfishcode added a commit that referenced this pull request Oct 22, 2019
This works around [this bug], which manifests as

```
error: could not remove 'setup' file: 'C:\Users\VssAdministrator\.cargo\bin/rustup-init.exe'
info: caused by: Access is denied. (os error 5)
```

as suggested by [this comment].

[this bug]: https://github.com/microsoft/azure-pipelines-image-generation/issues/1224
[this comment]: #118 (comment)
sunfishcode added a commit that referenced this pull request Oct 22, 2019
This works around [this bug], which manifests as

```
error: could not remove 'setup' file: 'C:\Users\VssAdministrator\.cargo\bin/rustup-init.exe'
info: caused by: Access is denied. (os error 5)
```

as suggested by [this comment].

[this bug]: https://github.com/microsoft/azure-pipelines-image-generation/issues/1224
[this comment]: #118 (comment)
This fixes some potential memory leaks in pathological situations, and
updates the code to use `//`-style comments.
@sunfishcode
Copy link
Member Author

Thanks! The workaround is now in, and the CI is passing.

if (*argv == NULL || argv_buf == NULL) {
// Add 1 for the NULL pointer to mark the end, and check for overflow.
size_t num_ptrs = new_argc + 1;
if (num_ptrs == 0) {
Copy link
Member

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 can be zero since you just checked that new_argc is not zero.

Copy link
Member Author

Choose a reason for hiding this comment

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

It would be zero if the + 1 overflows (per the comment). Would it be more clear if this code used __builtin_add_overflow instead?

Copy link
Member

Choose a reason for hiding this comment

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

Oh yes, forgot about the pesky people passing 2^32 command line arguments :)

/* Make sure the last pointer in the array is NULL. */
*argv[*argc] = NULL;
// Allocate memory for storing the argument chars.
char *argv_buf = malloc(argv_buf_size);
Copy link
Member

Choose a reason for hiding this comment

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

In emscripten I ended up inlining this function into _start itself so that these could become alloca and simple programs could avoid depending malloc + free. Not related to this change but worth mentioning for later maybe?

Copy link
Member Author

Choose a reason for hiding this comment

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

That's a good idea. Once the libpreopen reorg PR lands, and the next PR to make it so that the libpreopen code doesn't get linked in if you don't use it, then this will be the only malloc left, so we should remove it. I'll submit a separate PR for that.

@sunfishcode sunfishcode merged commit 1f11b91 into master Oct 22, 2019
@sunfishcode sunfishcode deleted the args-cleanup branch October 22, 2019 18:25
sunfishcode added a commit that referenced this pull request Oct 25, 2019
This way, if an application doesn't otherwise use malloc, they don't
need to link in malloc.

Idea from #118 (comment)!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants