Skip to content

Commit

Permalink
jobs: change p_job variable type from short to int (#763)
Browse files Browse the repository at this point in the history
This change addresses an issue where ksh has undefined behavior
when the number of jobs exceeds `2^15 - 1` (32767), which is the
maximum value for a `short`. The short integer overflow happened in
jobs.c, line 1210.
  • Loading branch information
vmihalko authored Jul 13, 2024
1 parent e4fcc3d commit 7219ffa
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 18 deletions.
5 changes: 5 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ This documents significant changes in the dev branch of ksh 93u+m.
For full details, see the git log at: https://github.com/ksh93/ksh
Uppercase BUG_* IDs are shell bug IDs as used by the Modernish shell library.

2024-07-13:

- The shell is now capable of handling more than 32767 simultaneous
background jobs, subject to system limitations.

2024-07-02:

- Support for UNC paths (//server/dir) is now enabled by default on Cygwin.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ outputs the name of this subdirectory.
Dynamically linked binaries, if supported for your system, are stored in
`dyn/bin` and `dyn/lib` subdirectories of your architecture directory.
If built, they are built in addition to the statically linked versions.
Export `AST_NO_DYLIB` to deactivate building dyanmically linked versions.
Export `AST_NO_DYLIB` to deactivate building dynamically linked versions.

If you have trouble or want to tune the binaries, you may pass additional
compiler and linker flags. It is usually best to export these as environment
Expand Down
8 changes: 2 additions & 6 deletions src/cmd/INIT/mamake.c
Original file line number Diff line number Diff line change
Expand Up @@ -2458,9 +2458,7 @@ int main(int argc, char **argv)
case 'K':
continue;
case 'V':
(void)write(1, id + 10, strlen(id) - 12);
putchar('\n');
exit(0);
return !(write(1, id + 10, strlen(id) - 12) > 0 && putchar('\n') == '\n');
case 'f':
append(state.opt, " -f ");
append(state.opt, opt_info.arg);
Expand Down Expand Up @@ -2575,9 +2573,7 @@ int main(int argc, char **argv)
setval(state.vars, "-strip-symbols", "1");
continue;
case 'V':
(void)write(1, id + 10, strlen(id) - 12);
putchar('\n');
exit(0);
return !(write(1, id + 10, strlen(id) - 12) > 0 && putchar('\n') == '\n');
case 'f':
case 'r':
case 'C':
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/ksh93/README
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ The options have the following defaults and meanings:
NOECHOE off Disable the '-e' option to the 'echo' command,
unless SHOPT_ECHOPRINT is enabled.

OPTIMIZE on Optimize loop invariants for with for and while loops.
OPTIMIZE on Optimize loop invariants in 'for' and 'while' loops.

PRINTF_LEGACY The printf built-in accepts a format operand that starts
with '-' without the standard preceding '--' options
Expand Down
3 changes: 1 addition & 2 deletions src/cmd/ksh93/data/builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,7 @@ const char sh_set[] =
* --posix is an AST optget(3) default option, so for ksh to use it, it must be listed
* explicitly (and handled by sh_argopts() in sh/args.c) to stop optget(3) overriding it.
*/
"[05:posix?Enable the \bposix\b option. When given at invocation time, "
"disables importing variable type attributes from the environment.]"
"[05:posix?Enable the \bposix\b option.]"
"[p?Privileged mode. Disabling \b-p\b sets the effective user ID to the "
"real user ID, and the effective group ID to the real group ID. "
"Enabling \b-p\b restores the effective user and group IDs to their "
Expand Down
5 changes: 3 additions & 2 deletions src/cmd/ksh93/include/jobs.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* *
* This software is part of the ast package *
* Copyright (c) 1982-2011 AT&T Intellectual Property *
* Copyright (c) 2020-2023 Contributors to ksh 93u+m *
* Copyright (c) 2020-2024 Contributors to ksh 93u+m *
* and is licensed under the *
* Eclipse Public License, Version 2.0 *
* *
Expand All @@ -14,6 +14,7 @@
* Martijn Dekker <[email protected]> *
* Johnothan King <[email protected]> *
* Anuradha Weeraman <[email protected]> *
* Vincent Mihalkovic <[email protected]> *
* *
***********************************************************************/
#ifndef JOB_NFLAG
Expand Down Expand Up @@ -51,7 +52,7 @@ struct process
pid_t p_pid; /* process ID */
pid_t p_pgrp; /* process group */
pid_t p_fgrp; /* process group when stopped */
short p_job; /* job number of process */
int p_job; /* job number of process */
unsigned short p_exit; /* exit value or signal number */
unsigned short p_exitmin; /* minimum exit value for xargs */
unsigned short p_flag; /* flags - see below */
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-07-02" /* must be in this format for $((.sh.version)) */
#define SH_RELEASE_DATE "2024-07-13" /* 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
11 changes: 6 additions & 5 deletions src/cmd/ksh93/sh/jobs.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* David Korn <[email protected]> *
* Martijn Dekker <[email protected]> *
* Johnothan King <[email protected]> *
* Vincent Mihalkovic <[email protected]> *
* *
***********************************************************************/
/*
Expand Down Expand Up @@ -437,7 +438,7 @@ int job_reap(int sig)
pw->p_flag &= ~P_NOTIFY;
if(job.jobcontrol && pid==pw->p_fgrp && pid==tcgetpgrp(JOBTTY))
{
px = job_byjid((int)pw->p_job);
px = job_byjid(pw->p_job);
for(; px && (px->p_flag&P_DONE); px=px->p_nxtproc);
if(!px)
tcsetpgrp(JOBTTY,job.mypid);
Expand Down Expand Up @@ -1527,7 +1528,7 @@ int job_switch(struct process *pw,int bgflag)
{
const char *msg;
job_lock();
if(!pw || !(pw=job_byjid((int)pw->p_job)))
if(!pw || !(pw=job_byjid(pw->p_job)))
{
job_unlock();
return 1;
Expand All @@ -1541,7 +1542,7 @@ int job_switch(struct process *pw,int bgflag)
}
if(bgflag=='b')
{
sfprintf(outfile,"[%d]\t",(int)pw->p_job);
sfprintf(outfile,"[%d]\t",pw->p_job);
sh.bckpid = pw->p_pid;
pw->p_flag |= P_BG;
msg = "&";
Expand Down Expand Up @@ -1622,7 +1623,7 @@ static struct process *job_unpost(struct process *pwtop,int notify)
sfprintf(sfstderr,"ksh: job line %4d: drop PID=%lld critical=%d PID=%d env=%u\n",__LINE__,(Sflong_t)sh.current_pid,job.in_critical,pwtop->p_pid,pwtop->p_env);
sfsync(sfstderr);
#endif /* DEBUG */
pwtop = pw = job_byjid((int)pwtop->p_job);
pwtop = pw = job_byjid(pwtop->p_job);
if(!pw)
return NULL;
#if SHOPT_BGX
Expand Down Expand Up @@ -1663,7 +1664,7 @@ static struct process *job_unpost(struct process *pwtop,int notify)
sfprintf(sfstderr,"ksh: job line %4d: free PID=%lld critical=%d job=%d\n",__LINE__,(Sflong_t)sh.current_pid,job.in_critical,pwtop->p_job);
sfsync(sfstderr);
#endif /* DEBUG */
job_free((int)pwtop->p_job);
job_free(pwtop->p_job);
return NULL;
}

Expand Down
1 change: 1 addition & 0 deletions src/cmd/ksh93/tests/arith.sh
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,7 @@ if [[ -o ?posix ]]
then (set -o posix; let x=010; [[ $x == 8 ]]) || err_exit 'let not treating 010 as octal with posix on'
fi

unset A
float z=0
integer aa=2 a=1
typeset -A A
Expand Down

0 comments on commit 7219ffa

Please sign in to comment.