Skip to content

Commit

Permalink
return: fix range warn for sizeof(long)==sizeof(int) (re: b90b8cd)
Browse files Browse the repository at this point in the history
The integer overflow check was based on 'long' being a wider
integer type than 'int', but on 32-bit Linux (at least), they are
the same, so the expected out-of-range warning and 128 exit status
didn't materialise.

src/cmd/ksh93/bltins/cflow.c: b_return():
- Use intmax_t instead of long.
- Don't bother with the check where sizeof(intmax_t)==sizeof(int)
  (generally ancient systems without 'long long').
- Tweaks for code legibility.
  • Loading branch information
McDutchie committed Jan 8, 2024
1 parent 4f0c04c commit 3814230
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/cmd/ksh93/bltins/cflow.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,17 @@ int b_return(int n, char *argv[],Shbltin_t *context)
argv += opt_info.index;
if(*argv)
{
long l = strtol(*argv, NULL, 10);
int r;
intmax_t l = strtoll(*argv, NULL, 10);
if(do_exit)
n = (int)(l & SH_EXITMASK); /* exit: apply bitmask before conversion to avoid undefined int overflow */
else if((long)(n = (int)l) != l) /* return: convert to int and check for overflow (should be safe enough) */
else if(r = (int)l, l != (intmax_t)r) /* return: convert to int and check for overflow (should be safe enough) */
{
errormsg(SH_DICT,ERROR_warn(0),"%s: out of range",*argv);
n = 128; /* overflow is undefined, so use a consistent status for this */
}
else
n = r;
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/ksh93/include/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

#define SH_RELEASE_FORK "93u+m" /* only change if you develop a new ksh93 fork */
#define SH_RELEASE_SVER "1.1.0-alpha" /* semantic version number: https://semver.org */
#define SH_RELEASE_DATE "2024-01-05" /* must be in this format for $((.sh.version)) */
#define SH_RELEASE_DATE "2024-01-08" /* must be in this format for $((.sh.version)) */
#define SH_RELEASE_CPYR "(c) 2020-2024 Contributors to ksh " SH_RELEASE_FORK

/* Scripts sometimes field-split ${.sh.version}, so don't change amount of whitespace. */
Expand Down
22 changes: 22 additions & 0 deletions src/cmd/ksh93/tests/return.sh
Original file line number Diff line number Diff line change
Expand Up @@ -247,5 +247,27 @@ foo && err_exit "'exit' within { block; } with redirection does not preserve exi
foo() { false; (exit); }
foo && err_exit "'exit' within subshell does not preserve exit status"

# ======
if let ".sh.version >= 20211208" && builtin getconf 2>/dev/null
then max=$(getconf INT_MAX) min=$(getconf INT_MIN) err=$tmp/stderr
exp=': out of range'
foo() { return $((max+1)); }
foo 2>$err
[[ e=$? -eq 128 && $(<$err) == *"$exp" ]] || err_exit 'return fails to warn for INT_MAX+1' \
"(expected status 128 and *$(printf %q "$exp"), got status $e and $(printf %q "$(<$err)"))"
foo() { return $((min-1)); }
foo 2>$err
[[ e=$? -eq 128 && $(<$err) == *"$exp" ]] || err_exit 'return fails to warn for INT_MIN-1' \
"(expected status 128 and *$(printf %q "$exp"), got status $e and $(printf %q "$(<$err)"))"
foo() { return $max; }
foo 2>$err
[[ e=$? -eq max && -z $got ]] || err_exit 'return fails for INT_MAX' \
"(expected status 128 and '', got status $e and $(printf %q "$(<$err)"))"
foo() { return $min; }
foo 2>$err
[[ e=$? -eq min && -z $got ]] || err_exit 'return fails for INT_MIN' \
"(expected status 128 and '', got status $e and $(printf %q "$(<$err)"))"
fi

# ======
exit $((Errors<125?Errors:125))

0 comments on commit 3814230

Please sign in to comment.