attractivechaos / klib

A standalone and lightweight C library
http://attractivechaos.github.io/klib/
MIT License
4.18k stars 556 forks source link

Quick fix for kstring.h #97

Closed stevefan1999-personal closed 6 years ago

stevefan1999-personal commented 6 years ago

error C4146: unary minus operator applied to unsigned type, result still unsigned

jmarshall commented 6 years ago

That code was carefully written to use unsigned negation, so as to avoid the undefined behaviour of doing -INT_MIN in signed arithmetic. (See #20, though I couldn't easily get the previous buggy code to produce the wrong output with today's compiler, go figure…)

Your modified code produces the wrong output for kputw(-2147483648) when compiled with recent versions of GCC (e.g. (brewed) 8.1.0 on macOS). See this paper, linked from this blog posting.

You should complain to your compiler vendor, or maybe suggest a kstring.h patch to use pragmas to suppress this stupid warning here :smile:

stevefan1999-personal commented 6 years ago

Hmm, integer expansion UB is one of my concerns, but I'm pretty shocked to see that this is the mitigation...

Seems like we had no workaround for this either. Closing PR.