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
#include <ast.h>
#if !defined(AST_VERSION) || AST_VERSION < 20220208
#error libast version 20220208 or later is required
#if !defined(AST_VERSION) || AST_VERSION < 20220801
#error libast version 20220801 or later is required
#endif
#if !_lib_fork
#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;
/*
* 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");
errormsg(SH_DICT, ERROR_SYSTEM|ERROR_PANIC, "out of memory (needed %llu bytes)", (uintmax_t)s);
UNREACHABLE();
}
@ -239,7 +238,7 @@ void *sh_malloc(size_t size)
{
void *cp = malloc(size);
if(!cp)
nomemory(0);
nomemory(size);
return(cp);
}
@ -247,7 +246,7 @@ void *sh_realloc(void *ptr, size_t size)
{
void *cp = realloc(ptr, size);
if(!cp)
nomemory(0);
nomemory(size);
return(cp);
}
@ -255,7 +254,7 @@ void *sh_calloc(size_t nmemb, size_t size)
{
void *cp = calloc(nmemb, size);
if(!cp)
nomemory(0);
nomemory(size);
return(cp);
}
@ -263,7 +262,7 @@ char *sh_strdup(const char *s)
{
char *dup = strdup(s);
if(!dup)
nomemory(0);
nomemory(strlen(s)+1);
return(dup);
}
@ -271,7 +270,7 @@ void *sh_memdup(const void *s, size_t n)
{
void *dup = memdup(s, n);
if(!dup)
nomemory(0);
nomemory(n);
return(dup);
}
@ -279,7 +278,7 @@ char *sh_getcwd(void)
{
char *cwd = getcwd(NIL(char*), 0);
if(!cwd && errno==ENOMEM)
nomemory(0);
nomemory(PATH_MAX);
return(cwd);
}

View file

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

View file

@ -45,7 +45,7 @@
extern Sfio_t _Stk_data;
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 unsigned int stklink(Stk_t*);
extern char* stkalloc(Stk_t*, size_t);

View file

@ -10,7 +10,7 @@
#include <stak.h>
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);
unsigned int staklink(Stak_t *\fIstack\fP)
@ -29,6 +29,11 @@ char *stakfreeze(unsigned \fIextra\fP);
.fi
.SH DESCRIPTION
.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
stack oriented dynamic storage.
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.
A stack is represented by the type \f3Stak_t\fP
defined in header \f3<stak.h>\fP.
.PP
At any instant there is one active stack.
Variable size objects can be
added to the active stack
and programs can reference these objects directly with pointers.
.PP
In addition, the last object on the stack
(referred to here as the current object)
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.
To use an additional stack, it is necessary to create it and to
install it as the active stack.
.PP
A stack is created with the \f3stakcreate\fP() function.
The \fIflags\fP argument is an options bitmask.
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
count is 1.
Otherwise, \f3stakcreate\fP() returns a null pointer.
.PP
The \f3staklink\fP() function increases the reference count for the
given \fIstack\fP and returns the increased count.
.PP
The \f3stakinstall\fP() function
makes the specified \fIstack\fP the active stack and returns a pointer
to the previous active stack.
@ -82,6 +92,7 @@ When \fIstack\fP is a null pointer,
the active stack is not changed
but the \fIoverflow\fP function for the active stack can be changed
and a pointer to the active stack is returned.
.PP
The \f3stakdelete\fP() function decrements the reference count and
frees the memory associated with
the specified stack
@ -122,6 +133,7 @@ There is a current offset associated with the current object that
determines where subsequent operations apply.
Initially, this offset is zero, and the offset changes as a result
of the operations you specify.
.PP
The \f3stakseek\fP() function is used set the offset for the
current object.
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
current frame.
\f3stakseek\fP() returns a pointer to the beginning of the current object.
.PP
The \f3staktell\fP() function gives the offset of the current object.
.PP
The \f3stakputc\fP() function adds a given character to the current object
on the stack.
The current offset is advanced by 1.
.PP
The \f3stakputs\fP() function appends the given \fIstring\fP onto the current
object in the stack and returns 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
region starting at \fIaddress\fP onto the current
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.
The result is not defined if \fIoffset\fP exceeds the size of the current
object.
.PP
The \f3stakfreeze\fP()
function terminates the current object on the
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
before the current object is terminated. The first added byte will
contain zero and the contents of the remaining bytes are undefined.
.PP
.SH HISTORY
The
\f3stak\fP

View file

@ -10,7 +10,7 @@
#include <stk.h>
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);
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.
The type \f3Stk_t\fP is compatible with the type \f3Sfio_t\fP
defined by the \f3sfio\fP(3) library.
.PP
At any instant there is one active stack which can be referenced
by the constant \f3stkstd\fP.
Variable size objects can be
added to the active stack
and programs can reference these objects directly with pointers.
.PP
In addition, the last object on the stack
(referred to here as the current object)
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.
To use an additional stack, it is necessary to create it and to
install it as the active stack.
.PP
A stack is created with the \f3stkopen\fP() function.
The \fIflags\fP argument is an options bitmask.
If the \f3STK_SMALL\fP bit is set, the stack allocates memory in
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
operations to return a null pointer instead of throwing an error.
operations to return a null pointer instead of throwing an exception.
If successful,
\f3stkopen\fP() returns a pointer to a stack whose reference
count is 1.
Otherwise, \f3stkopen\fP() returns a null pointer.
.PP
The \f3stklink\fP() function increases the reference count for the
given \fIstack\fP and returns the increased count.
.PP
The \f3stkinstall\fP() function
makes the specified \fIstack\fP the active stack and returns a pointer
to the previous active stack.
When the \fIoverflow\fP argument is not null,
it specifies a function that will
If the \fIoverflow\fP argument is not null and the stack was not opened with
the \f3STK_NULL\fP option, \fIoverflow\fP specifies a function that will
be called whenever \f3malloc\fP(3) fails while trying to grow the
stack.
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
but the \fIoverflow\fP function for the active stack can be changed
and a pointer to the active stack is returned.
.PP
The \f3stkclose\fP() function decrements the reference count and
frees the memory associated with
the specified stack
@ -126,6 +132,7 @@ There is a current offset associated with the current object that
determines where subsequent operations apply.
Initially, this offset is zero, and the offset changes as a result
of the operations you specify.
.PP
The \f3stkseek\fP() function is used set the offset for the
current object.
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
current frame.
\f3stkseek\fP() returns a pointer to the beginning of the current object.
.PP
The \f3stktell\fP() function gives the offset of the current object.
.PP
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.
The result is not defined if \fIoffset\fP exceeds the size of the current
object.
.PP
The \f3stkfreeze\fP()
function terminates the current object on the
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()
function returns non-zero if the address given by \fIaddr\fP is
on the stack \fIstack\fP and \f30\fP otherwise.
.PP
.SH HISTORY
The
\f3stk\fP

View file

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