Skip to content

strtol must not overflow #5632

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

Merged
merged 1 commit into from
Nov 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion regression/cbmc-library/strtol-02/test.desc
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
CORE
main.c

--signed-overflow-check
^EXIT=0$
^SIGNAL=0$
^VERIFICATION SUCCESSFUL$
--
^warning: ignoring
--
Even with --signed-overflow-check enabled, verification should succeed here as
strtol reports overflow condition with errno set to ERANGE.
13 changes: 10 additions & 3 deletions src/ansi-c/library/stdlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -353,16 +353,23 @@ inline long strtol(const char *nptr, char **endptr, int base)
break;

in_number=1;
long res_before=res;
res=res*base+ch-sub;
if(res<res_before)
_Bool overflow = __CPROVER_overflow_mult(res, (long)base);
#pragma CPROVER check push
#pragma CPROVER check disable "signed-overflow"
// This is now safe; still do it within the scope of the pragma to avoid an
// unnecessary assertion to be generated.
if(!overflow)
res *= base;
#pragma CPROVER check pop
if(overflow || __CPROVER_overflow_plus(res, (long)(ch - sub)))
{
errno=ERANGE;
if(sign=='-')
return LONG_MIN;
else
return LONG_MAX;
}
res += ch - sub;
}

if(endptr!=0)
Expand Down