1
0
Fork 0
mirror of git://git.code.sf.net/p/cdesktopenv/code synced 2025-03-09 15:50:02 +00:00

stk(3): fix argument type of exception callback function

The callback function for malloc(3) failure in the stk(3) routines
(which can be specified using stkinstall() and is set to nomemory()
in init.c) uses a size argument of type int. That's a type mismatch
with all the other size arguments and variables in stk(3) which use
size_t, an unsigned type that may be larger than int.

This is all quite inconsequential as nothing in our code base (or
in the complete old AT&T AST code base) actually uses that size for
anything, but it's still wrong, so this corrects the interface.

With this very minor API change, let's bump the libast API version
to 20220801, the date of the upcoming ksh 93u+m 1.0.0 release. :)

The ksh93/sh/init.c nomemory() function now reports the size that
could not be allocated, just because it can.
This commit is contained in:
Martijn Dekker 2022-07-30 00:34:48 +02:00
parent 9f6841c37e
commit 3e84231558
7 changed files with 45 additions and 23 deletions

View file

@ -30,8 +30,8 @@
#endif #endif
#include <ast.h> #include <ast.h>
#if !defined(AST_VERSION) || AST_VERSION < 20220208 #if !defined(AST_VERSION) || AST_VERSION < 20220801
#error libast version 20220208 or later is required #error libast version 20220801 or later is required
#endif #endif
#if !_lib_fork #if !_lib_fork
#error In 2021, ksh joined the 21st century and started requiring fork(2). #error In 2021, ksh joined the 21st century and started requiring fork(2).

View file

@ -222,12 +222,11 @@ static int shlvl;
static int rand_shift; static int rand_shift;
/* /*
* out of memory routine for stak routines * Exception callback routine for stk(3)/stak(3) and sh_*alloc wrappers.
*/ */
static noreturn char *nomemory(int unused) static noreturn char *nomemory(size_t s)
{ {
NOT_USED(unused); errormsg(SH_DICT, ERROR_SYSTEM|ERROR_PANIC, "out of memory (needed %llu bytes)", (uintmax_t)s);
errormsg(SH_DICT, ERROR_SYSTEM|ERROR_PANIC, "out of memory");
UNREACHABLE(); UNREACHABLE();
} }
@ -239,7 +238,7 @@ void *sh_malloc(size_t size)
{ {
void *cp = malloc(size); void *cp = malloc(size);
if(!cp) if(!cp)
nomemory(0); nomemory(size);
return(cp); return(cp);
} }
@ -247,7 +246,7 @@ void *sh_realloc(void *ptr, size_t size)
{ {
void *cp = realloc(ptr, size); void *cp = realloc(ptr, size);
if(!cp) if(!cp)
nomemory(0); nomemory(size);
return(cp); return(cp);
} }
@ -255,7 +254,7 @@ void *sh_calloc(size_t nmemb, size_t size)
{ {
void *cp = calloc(nmemb, size); void *cp = calloc(nmemb, size);
if(!cp) if(!cp)
nomemory(0); nomemory(size);
return(cp); return(cp);
} }
@ -263,7 +262,7 @@ char *sh_strdup(const char *s)
{ {
char *dup = strdup(s); char *dup = strdup(s);
if(!dup) if(!dup)
nomemory(0); nomemory(strlen(s)+1);
return(dup); return(dup);
} }
@ -271,7 +270,7 @@ void *sh_memdup(const void *s, size_t n)
{ {
void *dup = memdup(s, n); void *dup = memdup(s, n);
if(!dup) if(!dup)
nomemory(0); nomemory(n);
return(dup); return(dup);
} }
@ -279,7 +278,7 @@ char *sh_getcwd(void)
{ {
char *cwd = getcwd(NIL(char*), 0); char *cwd = getcwd(NIL(char*), 0);
if(!cwd && errno==ENOMEM) if(!cwd && errno==ENOMEM)
nomemory(0); nomemory(PATH_MAX);
return(cwd); return(cwd);
} }

View file

@ -1,6 +1,6 @@
iff AST_API iff AST_API
ver ast 20220208 ver ast 20220801
api ast 20120528 regexec regnexec regrexec regsubexec api ast 20120528 regexec regnexec regrexec regsubexec

View file

@ -45,7 +45,7 @@
extern Sfio_t _Stk_data; extern Sfio_t _Stk_data;
extern Stk_t* stkopen(int); extern Stk_t* stkopen(int);
extern Stk_t* stkinstall(Stk_t*, char*(*)(int)); extern Stk_t* stkinstall(Stk_t*, char*(*)(size_t));
extern int stkclose(Stk_t*); extern int stkclose(Stk_t*);
extern unsigned int stklink(Stk_t*); extern unsigned int stklink(Stk_t*);
extern char* stkalloc(Stk_t*, size_t); extern char* stkalloc(Stk_t*, size_t);

View file

@ -10,7 +10,7 @@
#include <stak.h> #include <stak.h>
Stak_t *stakcreate(int \fIflags\fP); Stak_t *stakcreate(int \fIflags\fP);
Stak_t *stakinstall(Stak_t *\fIstack\fP, char *(\fIoverflow\fP)(int)); Stak_t *stakinstall(Stak_t *\fIstack\fP, char *(\fIoverflow\fP)(size_t));
int stakdelete(Stak_t *\fIstack\fP); int stakdelete(Stak_t *\fIstack\fP);
unsigned int staklink(Stak_t *\fIstack\fP) unsigned int staklink(Stak_t *\fIstack\fP)
@ -29,6 +29,11 @@ char *stakfreeze(unsigned \fIextra\fP);
.fi .fi
.SH DESCRIPTION .SH DESCRIPTION
.PP .PP
(This interface is obsolete; it is now simply a set of macros
that translate these calls into \f3stk\fP(3) and \f3sfio\fP(3) calls
using \f3stkstd\fP.
The following description should continue to apply to the old calls.)
.PP
\f3stak\fP is a package of routines designed to provide efficient \f3stak\fP is a package of routines designed to provide efficient
stack oriented dynamic storage. stack oriented dynamic storage.
A stack abstraction consists of an ordered list of contiguous A stack abstraction consists of an ordered list of contiguous
@ -36,10 +41,12 @@ memory regions, called stack frames, that can hold objects of
arbitrary size. arbitrary size.
A stack is represented by the type \f3Stak_t\fP A stack is represented by the type \f3Stak_t\fP
defined in header \f3<stak.h>\fP. defined in header \f3<stak.h>\fP.
.PP
At any instant there is one active stack. At any instant there is one active stack.
Variable size objects can be Variable size objects can be
added to the active stack added to the active stack
and programs can reference these objects directly with pointers. and programs can reference these objects directly with pointers.
.PP
In addition, the last object on the stack In addition, the last object on the stack
(referred to here as the current object) (referred to here as the current object)
can be built incrementally. can be built incrementally.
@ -53,6 +60,7 @@ relative offsets ranging from zero to the current offset of the object.
There is a preset initial active stack. There is a preset initial active stack.
To use an additional stack, it is necessary to create it and to To use an additional stack, it is necessary to create it and to
install it as the active stack. install it as the active stack.
.PP
A stack is created with the \f3stakcreate\fP() function. A stack is created with the \f3stakcreate\fP() function.
The \fIflags\fP argument is an options bitmask. The \fIflags\fP argument is an options bitmask.
If the \f3STAK_SMALL\fP bit is set, the stack allocates memory in If the \f3STAK_SMALL\fP bit is set, the stack allocates memory in
@ -61,8 +69,10 @@ If successful,
\f3stakcreate\fP() returns a pointer to a stack whose reference \f3stakcreate\fP() returns a pointer to a stack whose reference
count is 1. count is 1.
Otherwise, \f3stakcreate\fP() returns a null pointer. Otherwise, \f3stakcreate\fP() returns a null pointer.
.PP
The \f3staklink\fP() function increases the reference count for the The \f3staklink\fP() function increases the reference count for the
given \fIstack\fP and returns the increased count. given \fIstack\fP and returns the increased count.
.PP
The \f3stakinstall\fP() function The \f3stakinstall\fP() function
makes the specified \fIstack\fP the active stack and returns a pointer makes the specified \fIstack\fP the active stack and returns a pointer
to the previous active stack. to the previous active stack.
@ -82,6 +92,7 @@ When \fIstack\fP is a null pointer,
the active stack is not changed the active stack is not changed
but the \fIoverflow\fP function for the active stack can be changed but the \fIoverflow\fP function for the active stack can be changed
and a pointer to the active stack is returned. and a pointer to the active stack is returned.
.PP
The \f3stakdelete\fP() function decrements the reference count and The \f3stakdelete\fP() function decrements the reference count and
frees the memory associated with frees the memory associated with
the specified stack the specified stack
@ -122,6 +133,7 @@ There is a current offset associated with the current object that
determines where subsequent operations apply. determines where subsequent operations apply.
Initially, this offset is zero, and the offset changes as a result Initially, this offset is zero, and the offset changes as a result
of the operations you specify. of the operations you specify.
.PP
The \f3stakseek\fP() function is used set the offset for the The \f3stakseek\fP() function is used set the offset for the
current object. current object.
The \fIoffset\fP argument to \f3stakseek\fP() specifies the new The \fIoffset\fP argument to \f3stakseek\fP() specifies the new
@ -130,14 +142,17 @@ The frame will be extended or moved
if \f3offset\fP causes the new current offset to extend beyond the if \f3offset\fP causes the new current offset to extend beyond the
current frame. current frame.
\f3stakseek\fP() returns a pointer to the beginning of the current object. \f3stakseek\fP() returns a pointer to the beginning of the current object.
.PP
The \f3staktell\fP() function gives the offset of the current object. The \f3staktell\fP() function gives the offset of the current object.
.PP .PP
The \f3stakputc\fP() function adds a given character to the current object The \f3stakputc\fP() function adds a given character to the current object
on the stack. on the stack.
The current offset is advanced by 1. The current offset is advanced by 1.
.PP
The \f3stakputs\fP() function appends the given \fIstring\fP onto the current The \f3stakputs\fP() function appends the given \fIstring\fP onto the current
object in the stack and returns the length of the string. object in the stack and returns the length of the string.
The current offset is advanced by the length of the string. The current offset is advanced by the length of the string.
.PP
The \f3stakwrite\fP() function appends the given \fIsize\fP byte memory The \f3stakwrite\fP() function appends the given \fIsize\fP byte memory
region starting at \fIaddress\fP onto the current region starting at \fIaddress\fP onto the current
object in the stack and advances the current offset by \fIsize\fP. object in the stack and advances the current offset by \fIsize\fP.
@ -148,13 +163,13 @@ for the current object into a memory address on the stack.
This address is only valid until another stack operation is given. This address is only valid until another stack operation is given.
The result is not defined if \fIoffset\fP exceeds the size of the current The result is not defined if \fIoffset\fP exceeds the size of the current
object. object.
.PP
The \f3stakfreeze\fP() The \f3stakfreeze\fP()
function terminates the current object on the function terminates the current object on the
stack and returns a pointer to the beginning of this object. stack and returns a pointer to the beginning of this object.
If \fIextra\fP is non-zero, \fIextra\fP bytes are added to the stack If \fIextra\fP is non-zero, \fIextra\fP bytes are added to the stack
before the current object is terminated. The first added byte will before the current object is terminated. The first added byte will
contain zero and the contents of the remaining bytes are undefined. contain zero and the contents of the remaining bytes are undefined.
.PP
.SH HISTORY .SH HISTORY
The The
\f3stak\fP \f3stak\fP

View file

@ -10,7 +10,7 @@
#include <stk.h> #include <stk.h>
Stk_t *stkopen(int \fIflags\fP); Stk_t *stkopen(int \fIflags\fP);
Stk_t *stkinstall(Stk_t *\fIstack\fP, char *(\fIoverflow\fP)(int)); Stk_t *stkinstall(Stk_t *\fIstack\fP, char *(\fIoverflow\fP)(size_t));
int stkclose(Stk_t *\fIstack\fP); int stkclose(Stk_t *\fIstack\fP);
unsigned int stklink(Stk_t *\fIstack\fP) unsigned int stklink(Stk_t *\fIstack\fP)
@ -36,11 +36,13 @@ A stack is represented by the type \f3Stk_t\fP
defined in header \f3<stk.h>\fP. defined in header \f3<stk.h>\fP.
The type \f3Stk_t\fP is compatible with the type \f3Sfio_t\fP The type \f3Stk_t\fP is compatible with the type \f3Sfio_t\fP
defined by the \f3sfio\fP(3) library. defined by the \f3sfio\fP(3) library.
.PP
At any instant there is one active stack which can be referenced At any instant there is one active stack which can be referenced
by the constant \f3stkstd\fP. by the constant \f3stkstd\fP.
Variable size objects can be Variable size objects can be
added to the active stack added to the active stack
and programs can reference these objects directly with pointers. and programs can reference these objects directly with pointers.
.PP
In addition, the last object on the stack In addition, the last object on the stack
(referred to here as the current object) (referred to here as the current object)
can be built incrementally. can be built incrementally.
@ -54,23 +56,26 @@ relative offsets ranging from zero to the current offset of the object.
There is a preset initial active stack. There is a preset initial active stack.
To use an additional stack, it is necessary to create it and to To use an additional stack, it is necessary to create it and to
install it as the active stack. install it as the active stack.
.PP
A stack is created with the \f3stkopen\fP() function. A stack is created with the \f3stkopen\fP() function.
The \fIflags\fP argument is an options bitmask. The \fIflags\fP argument is an options bitmask.
If the \f3STK_SMALL\fP bit is set, the stack allocates memory in If the \f3STK_SMALL\fP bit is set, the stack allocates memory in
small blocks, optimizing for memory usage at the expense of performance. small blocks, optimizing for memory usage at the expense of performance.
If the \f3STK_NULL\fP bit is set, a stack overflow will cause stack If the \f3STK_NULL\fP bit is set, a stack overflow will cause stack
operations to return a null pointer instead of throwing an error. operations to return a null pointer instead of throwing an exception.
If successful, If successful,
\f3stkopen\fP() returns a pointer to a stack whose reference \f3stkopen\fP() returns a pointer to a stack whose reference
count is 1. count is 1.
Otherwise, \f3stkopen\fP() returns a null pointer. Otherwise, \f3stkopen\fP() returns a null pointer.
.PP
The \f3stklink\fP() function increases the reference count for the The \f3stklink\fP() function increases the reference count for the
given \fIstack\fP and returns the increased count. given \fIstack\fP and returns the increased count.
.PP
The \f3stkinstall\fP() function The \f3stkinstall\fP() function
makes the specified \fIstack\fP the active stack and returns a pointer makes the specified \fIstack\fP the active stack and returns a pointer
to the previous active stack. to the previous active stack.
When the \fIoverflow\fP argument is not null, If the \fIoverflow\fP argument is not null and the stack was not opened with
it specifies a function that will the \f3STK_NULL\fP option, \fIoverflow\fP specifies a function that will
be called whenever \f3malloc\fP(3) fails while trying to grow the be called whenever \f3malloc\fP(3) fails while trying to grow the
stack. stack.
The \fIoverflow\fP function will be called with the size that was passed The \fIoverflow\fP function will be called with the size that was passed
@ -85,6 +90,7 @@ When \fIstack\fP is a null pointer,
the active stack is not changed the active stack is not changed
but the \fIoverflow\fP function for the active stack can be changed but the \fIoverflow\fP function for the active stack can be changed
and a pointer to the active stack is returned. and a pointer to the active stack is returned.
.PP
The \f3stkclose\fP() function decrements the reference count and The \f3stkclose\fP() function decrements the reference count and
frees the memory associated with frees the memory associated with
the specified stack the specified stack
@ -126,6 +132,7 @@ There is a current offset associated with the current object that
determines where subsequent operations apply. determines where subsequent operations apply.
Initially, this offset is zero, and the offset changes as a result Initially, this offset is zero, and the offset changes as a result
of the operations you specify. of the operations you specify.
.PP
The \f3stkseek\fP() function is used set the offset for the The \f3stkseek\fP() function is used set the offset for the
current object. current object.
The \fIoffset\fP argument to \f3stkseek\fP() specifies the new The \fIoffset\fP argument to \f3stkseek\fP() specifies the new
@ -134,6 +141,7 @@ The frame will be extended or moved
if \f3offset\fP causes the new current offset to extend beyond the if \f3offset\fP causes the new current offset to extend beyond the
current frame. current frame.
\f3stkseek\fP() returns a pointer to the beginning of the current object. \f3stkseek\fP() returns a pointer to the beginning of the current object.
.PP
The \f3stktell\fP() function gives the offset of the current object. The \f3stktell\fP() function gives the offset of the current object.
.PP .PP
The \f3stkptr\fP() function converts the given \f3offset\fP The \f3stkptr\fP() function converts the given \f3offset\fP
@ -141,6 +149,7 @@ for the current object into a memory address on the stack.
This address is only valid until another stack operation is given. This address is only valid until another stack operation is given.
The result is not defined if \fIoffset\fP exceeds the size of the current The result is not defined if \fIoffset\fP exceeds the size of the current
object. object.
.PP
The \f3stkfreeze\fP() The \f3stkfreeze\fP()
function terminates the current object on the function terminates the current object on the
stack and returns a pointer to the beginning of this object. stack and returns a pointer to the beginning of this object.
@ -151,7 +160,6 @@ contain zero and the contents of the remaining bytes are undefined.
The \f3stkon\fP() The \f3stkon\fP()
function returns non-zero if the address given by \fIaddr\fP is function returns non-zero if the address given by \fIaddr\fP is
on the stack \fIstack\fP and \f30\fP otherwise. on the stack \fIstack\fP and \f30\fP otherwise.
.PP
.SH HISTORY .SH HISTORY
The The
\f3stk\fP \f3stk\fP

View file

@ -51,7 +51,7 @@
#define STK_FSIZE (1024*sizeof(char*)) #define STK_FSIZE (1024*sizeof(char*))
#define STK_HDRSIZE (sizeof(Sfio_t)+sizeof(Sfdisc_t)) #define STK_HDRSIZE (sizeof(Sfio_t)+sizeof(Sfdisc_t))
typedef char* (*_stk_overflow_)(int); typedef char* (*_stk_overflow_)(size_t);
static int stkexcept(Sfio_t*,int,void*,Sfdisc_t*); static int stkexcept(Sfio_t*,int,void*,Sfdisc_t*);
static Sfdisc_t stkdisc = { 0, 0, 0, stkexcept }; static Sfdisc_t stkdisc = { 0, 0, 0, stkexcept };
@ -113,7 +113,7 @@ static const char Omsg[] = "malloc failed while growing stack\n";
/* /*
* default overflow exception * default overflow exception
*/ */
static noreturn char *overflow(int n) static noreturn char *overflow(size_t n)
{ {
NoP(n); NoP(n);
write(2,Omsg, sizeof(Omsg)-1); write(2,Omsg, sizeof(Omsg)-1);