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 and McDutchie committed Jul 13, 2024
1 parent 53667fb commit 685f18d
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 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 1.0 branch of ksh 93u+m.
For full details, see the git log at: https://github.com/ksh93/ksh/tree/1.0
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:

- Release 1.0.9.
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
4 changes: 2 additions & 2 deletions src/cmd/ksh93/include/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
#include <releaseflags.h>

#define SH_RELEASE_FORK "93u+m" /* only change if you develop a new ksh93 fork */
#define SH_RELEASE_SVER "1.0.9" /* semantic version number: https://semver.org */
#define SH_RELEASE_DATE "2024-07-02" /* must be in this format for $((.sh.version)) */
#define SH_RELEASE_SVER "1.0.10-beta" /* semantic version number: https://semver.org */
#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 @@ -1528,7 +1529,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 @@ -1542,7 +1543,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 @@ -1623,7 +1624,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 @@ -1664,7 +1665,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

0 comments on commit 685f18d

Please sign in to comment.