Skip to content

Commit

Permalink
fmt/pl: rewrite negative handling (avoid undefined behavior) (#314)
Browse files Browse the repository at this point in the history
  • Loading branch information
sreimers authored Apr 11, 2022
1 parent 33763b0 commit fe5ee51
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/fmt/pl.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void pl_set_mbuf(struct pl *pl, const struct mbuf *mb)
*/
int32_t pl_i32(const struct pl *pl)
{
uint32_t v = 0;
int32_t v = 0;
uint32_t mul = 1;
const char *p;
bool neg = false;
Expand All @@ -76,7 +76,7 @@ int32_t pl_i32(const struct pl *pl)
const char ch = *--p;

if ('0' <= ch && ch <= '9') {
v += mul * (ch - '0');
v -= mul * (ch - '0');
mul *= 10;
}
else if (ch == '-' && p == pl->p) {
Expand All @@ -91,7 +91,10 @@ int32_t pl_i32(const struct pl *pl)
}
}

return neg ? -v : v;
if (!neg && v == INT32_MIN)
return INT32_MIN;

return neg ? v : -v;
}


Expand All @@ -104,7 +107,7 @@ int32_t pl_i32(const struct pl *pl)
*/
int64_t pl_i64(const struct pl *pl)
{
uint64_t v = 0;
int64_t v = 0;
uint64_t mul = 1;
const char *p;
bool neg = false;
Expand All @@ -117,7 +120,7 @@ int64_t pl_i64(const struct pl *pl)
const char ch = *--p;

if ('0' <= ch && ch <= '9') {
v += mul * (ch - '0');
v -= mul * (ch - '0');
mul *= 10;
}
else if (ch == '-' && p == pl->p) {
Expand All @@ -132,7 +135,10 @@ int64_t pl_i64(const struct pl *pl)
}
}

return neg ? -v : v;
if (!neg && v == INT64_MIN)
return INT64_MIN;

return neg ? v : -v;
}


Expand Down

0 comments on commit fe5ee51

Please sign in to comment.