aboutsummaryrefslogtreecommitdiff
path: root/src/lj_str.c
diff options
context:
space:
mode:
authorMike Pall <mike>2022-12-22 00:03:06 +0100
committerMike Pall <mike>2022-12-22 00:03:06 +0100
commit8a5e398c52c7f8ca3e1a0e574cc2ba38224b759b (patch)
treedae089564f58db2963bae8e3530c1faae104cc61 /src/lj_str.c
parentb2791179ef96d652d00d78d2a8780af690537f6a (diff)
downloadluajit-8a5e398c52c7f8ca3e1a0e574cc2ba38224b759b.tar.gz
luajit-8a5e398c52c7f8ca3e1a0e574cc2ba38224b759b.tar.bz2
luajit-8a5e398c52c7f8ca3e1a0e574cc2ba38224b759b.zip
Avoid negation of signed integers in C that may hold INT*_MIN.
Reported by minoki. Recent C compilers 'take advantage' of the undefined behavior. This completely changes the meaning of expressions like (k == -k).
Diffstat (limited to 'src/lj_str.c')
-rw-r--r--src/lj_str.c2
1 files changed, 1 insertions, 1 deletions
diff --git a/src/lj_str.c b/src/lj_str.c
index 60912aed..f1fc8ee1 100644
--- a/src/lj_str.c
+++ b/src/lj_str.c
@@ -190,7 +190,7 @@ size_t LJ_FASTCALL lj_str_bufnum(char *s, cTValue *o)
190/* Print integer to buffer. Returns pointer to start. */ 190/* Print integer to buffer. Returns pointer to start. */
191char * LJ_FASTCALL lj_str_bufint(char *p, int32_t k) 191char * LJ_FASTCALL lj_str_bufint(char *p, int32_t k)
192{ 192{
193 uint32_t u = (uint32_t)(k < 0 ? -k : k); 193 uint32_t u = k < 0 ? ~(uint32_t)k+1u : (uint32_t)k;
194 p += 1+10; 194 p += 1+10;
195 do { *--p = (char)('0' + u % 10); } while (u /= 10); 195 do { *--p = (char)('0' + u % 10); } while (u /= 10);
196 if (k < 0) *--p = '-'; 196 if (k < 0) *--p = '-';