fosstodon.org is one of the many independent Mastodon servers you can use to participate in the fediverse.
Fosstodon is an invite only Mastodon instance that is open to those who are interested in technology; particularly free & open source software. If you wish to join, contact us for an invite.

Administered by:

Server stats:

10K
active users

@alanc You could also maybe fix it by moving those variables out of the function, like this: godbolt.org/z/sxG6eaWMx

edit: ...the idea being that gcc optimized them out because it noticed they weren't being used. By assigning the values to globals it can't reason about how they might be used by other compilation units

godbolt.orgCompiler Explorer - C (x86-64 clang (trunk)) char *m0, *r0, *c0, *p; int main(void) { m0 = malloc(0); p = malloc(10); r0 = realloc(p, 0); c0 = calloc(0, 10); exit((m0 == NULL || r0 == NULL || c0 == NULL) ? 0 : 1); return 0; }

@vegard @thesamesam @alanc this demonstrates a strange optimization weakness in LLVM, because the asm does not modify its input

asms would need to be asm("" : "+r"(arg)) and you'd need to pass an lvalue to them

but wrapping the output of malloc in an asm does not solve the initial problem fully: if the compiler assumes that malloc(0) returns 0, it will make that substitution, and no amount of wrapping the 0 in asm will make the test invoke malloc

@alanc @thesamesam @amonakov @vegard My guess: The size computation can wrap around and it that case there could be an oob access.

@alanc @thesamesam @vegard such warnings are often confusing, but if it helps, I can suggest that you treat those as if the compiler was telling you: when the computation in a 32-bit int feeding the size argument of malloc overflows, you're gonna have a bad time

(not that you wouldn't have a bad time before the patch, but now there's an explicit check for (potentially overflowed) size being 0, so the compiler — probably as a result of CFG path specialization — considers that possibility)