nasmlib: Add nasm_(v)asprintf()

Add a version of (v)asprintf(), which allocates a string on the
heap. Unlike the standard version of (v)asprintf(), we return the
pointer; if one wants the length of the string then one can simply use
the %n pattern.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
This commit is contained in:
H. Peter Anvin (Intel) 2018-12-12 14:34:34 -08:00
parent 64471097ca
commit eb5b3ae0d3
8 changed files with 92 additions and 24 deletions

View File

@ -96,7 +96,7 @@ LIBOBJ = stdlib/snprintf.$(O) stdlib/vsnprintf.$(O) stdlib/strlcpy.$(O) \
stdlib/strnlen.$(O) stdlib/strrchrnul.$(O) \
\
nasmlib/ver.$(O) \
nasmlib/crc64.$(O) nasmlib/malloc.$(O) \
nasmlib/crc64.$(O) nasmlib/alloc.$(O) nasmlib/asprintf.$(O) \
nasmlib/md5c.$(O) nasmlib/string.$(O) nasmlib/nctype.$(O) \
nasmlib/file.$(O) nasmlib/mmap.$(O) nasmlib/ilog2.$(O) \
nasmlib/realpath.$(O) nasmlib/path.$(O) \

View File

@ -68,11 +68,11 @@ LIBOBJ = stdlib\snprintf.$(O) stdlib\vsnprintf.$(O) stdlib\strlcpy.$(O) \
stdlib\strnlen.$(O) stdlib\strrchrnul.$(O) \
\
nasmlib\ver.$(O) \
nasmlib\crc64.$(O) nasmlib\malloc.$(O) \
nasmlib\crc64.$(O) nasmlib\alloc.$(O) nasmlib\asprintf.$(O) \
nasmlib\md5c.$(O) nasmlib\string.$(O) nasmlib\nctype.$(O) \
nasmlib\file.$(O) nasmlib\mmap.$(O) nasmlib\ilog2.$(O) \
nasmlib\realpath.$(O) nasmlib\path.$(O) \
nasmlib\filename.$(O) nasmlib\srcfile.$(O) \
nasmlib\filename.$(O) \
nasmlib\zerobuf.$(O) nasmlib\readnum.$(O) nasmlib\bsi.$(O) \
nasmlib\rbtree.$(O) nasmlib\hashtbl.$(O) \
nasmlib\raa.$(O) nasmlib\saa.$(O) \
@ -97,7 +97,7 @@ LIBOBJ = stdlib\snprintf.$(O) stdlib\vsnprintf.$(O) stdlib\strlcpy.$(O) \
asm\segalloc.$(O) \
asm\preproc-nop.$(O) \
asm\rdstrnum.$(O) \
\
asm\srcfile.$(O) \
macros\macros.$(O) \
\
output\outform.$(O) output\outlib.$(O) output\legacy.$(O) \

View File

@ -57,11 +57,11 @@ LIBOBJ = stdlib\snprintf.$(O) stdlib\vsnprintf.$(O) stdlib\strlcpy.$(O) &
stdlib\strnlen.$(O) stdlib\strrchrnul.$(O) &
&
nasmlib\ver.$(O) &
nasmlib\crc64.$(O) nasmlib\malloc.$(O) &
nasmlib\crc64.$(O) nasmlib\alloc.$(O) nasmlib\asprintf.$(O) &
nasmlib\md5c.$(O) nasmlib\string.$(O) nasmlib\nctype.$(O) &
nasmlib\file.$(O) nasmlib\mmap.$(O) nasmlib\ilog2.$(O) &
nasmlib\realpath.$(O) nasmlib\path.$(O) &
nasmlib\filename.$(O) nasmlib\srcfile.$(O) &
nasmlib\filename.$(O) &
nasmlib\zerobuf.$(O) nasmlib\readnum.$(O) nasmlib\bsi.$(O) &
nasmlib\rbtree.$(O) nasmlib\hashtbl.$(O) &
nasmlib\raa.$(O) nasmlib\saa.$(O) &
@ -86,7 +86,7 @@ LIBOBJ = stdlib\snprintf.$(O) stdlib\vsnprintf.$(O) stdlib\strlcpy.$(O) &
asm\segalloc.$(O) &
asm\preproc-nop.$(O) &
asm\rdstrnum.$(O) &
&
asm\srcfile.$(O) &
macros\macros.$(O) &
&
output\outform.$(O) output\outlib.$(O) output\legacy.$(O) &

View File

@ -118,6 +118,8 @@ AC_CHECK_FUNCS(strrchrnul)
AC_CHECK_FUNCS(iscntrl)
AC_CHECK_FUNCS(isascii)
AC_CHECK_FUNCS(vasprintf)
AC_CHECK_FUNCS(getuid)
AC_CHECK_FUNCS(getgid)

View File

@ -1,6 +1,6 @@
/* ----------------------------------------------------------------------- *
*
* Copyright 2007-2017 The NASM Authors - All Rights Reserved
* Copyright 2007-2018 The NASM Authors - All Rights Reserved
* See the file AUTHORS included with the NASM distribution for
* the specific copyright holders.
*
@ -81,6 +81,7 @@
#include <stddef.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#ifdef HAVE_SYS_TYPES_H
@ -350,6 +351,24 @@ size_t strnlen(const char *s, size_t maxlen);
# define is_constant(x) false
#endif
/*
* The autoconf documentation states:
*
* `va_copy'
* The C99 standard provides `va_copy' for copying `va_list'
* variables. It may be available in older environments too, though
* possibly as `__va_copy' (e.g., `gcc' in strict pre-C99 mode).
* These can be tested with `#ifdef'. A fallback to `memcpy (&dst,
* &src, sizeof (va_list))' gives maximum portability.
*/
#ifndef va_copy
# ifdef __va_copy
# define va_copy(dst,src) __va_copy(dst,src)
# else
# define va_copy(dst,src) memcpy(&(dst),&(src),sizeof(va_list))
# endif
#endif
/* Watcom doesn't handle switch statements with 64-bit types, hack around it */
#ifdef __WATCOMC__
# define BOGUS_CASE 0x76543210

View File

@ -48,11 +48,11 @@
#endif
/*
* Wrappers around malloc, realloc and free. nasm_malloc will
* fatal-error and die rather than return NULL; nasm_realloc will
* do likewise, and will also guarantee to work right on being
* passed a NULL pointer; nasm_free will do nothing if it is passed
* a NULL pointer.
* Wrappers around malloc, realloc, free and a few more. nasm_malloc
* will fatal-error and die rather than return NULL; nasm_realloc will
* do likewise, and will also guarantee to work right on being passed
* a NULL pointer; nasm_free will do nothing if it is passed a NULL
* pointer.
*/
void * safe_malloc(1) nasm_malloc(size_t);
void * safe_malloc(1) nasm_zalloc(size_t);
@ -64,6 +64,14 @@ char * safe_alloc nasm_strndup(const char *, size_t);
char * safe_alloc nasm_strcat(const char *one, const char *two);
char * safe_alloc end_with_null nasm_strcatn(const char *one, ...);
/*
* Variants on asprintf(). Note that unlike the standard version, we
* return the pointer rather than the length; if you want the length
* use %n.
*/
char * safe_alloc nasm_asprintf(const char *fmt, ...);
char * safe_alloc nasm_vasprintf(const char *fmt, va_list ap);
/* Assert the argument is a pointer without evaluating it */
#define nasm_assert_pointer(p) ((void)sizeof(*(p)))

View File

@ -36,24 +36,15 @@
*/
#include "compiler.h"
#include <stdlib.h>
#include "nasmlib.h"
#include "error.h"
#include "alloc.h"
static no_return nasm_alloc_failed(void)
no_return nasm_alloc_failed(void)
{
nasm_fatal("out of memory");
}
static inline void *validate_ptr(void *p)
{
if (unlikely(!p))
nasm_alloc_failed();
return p;
}
void *nasm_malloc(size_t size)
{
return validate_ptr(malloc(size));

48
nasmlib/alloc.h Normal file
View File

@ -0,0 +1,48 @@
/* ----------------------------------------------------------------------- *
*
* Copyright 1996-2018 The NASM Authors - All Rights Reserved
* See the file AUTHORS included with the NASM distribution for
* the specific copyright holders.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following
* conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* ----------------------------------------------------------------------- */
#ifndef NASMLIB_ALLOC_H
#define NASMLIB_ALLOC_H
#include "compiler.h"
no_return nasm_alloc_failed(void);
static inline void *validate_ptr(void *p)
{
if (unlikely(!p))
nasm_alloc_failed();
return p;
}
#endif /* NASMLIB_ALLOC_H */