malloc: simplify nasm_malloc code, add nasm_strcatn()

Simplify the nasm_malloc() code by moving the pointer check into a
common subroutine.

We can now issue a filename error even for failures like malloc().

Add support for the gcc sentinel attribute (verify that a list ends
with NULL).

Add a handful of safe_alloc attributes.

Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
This commit is contained in:
H. Peter Anvin 2018-05-30 11:40:42 -07:00
parent 1ce81e10ef
commit 740ec3572b
5 changed files with 85 additions and 22 deletions

View file

@ -264,7 +264,7 @@ size_t strnlen(const char *s, size_t maxlen);
#ifdef HAVE_FUNC_ATTRIBUTE_MALLOC
# define safe_alloc never_null __attribute__((malloc))
#else
# define safe_alloc
# define safe_alloc never_null
#endif
#ifdef HAVE_FUNC_ATTRIBUTE_ALLOC_SIZE
@ -277,6 +277,12 @@ size_t strnlen(const char *s, size_t maxlen);
# define safe_realloc(s) never_null
#endif
#ifdef HAVE_FUNC_ATTRIBUTE_SENTINEL
# define end_with_null __attribute__((sentinel))
#else
# define end_with_null
#endif
/*
* How to tell the compiler that a function doesn't return
*/

View file

@ -79,6 +79,8 @@ void * safe_realloc(2) nasm_realloc(void *, size_t);
void nasm_free(void *);
char * safe_alloc nasm_strdup(const char *);
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, ...);
/* Assert the argument is a pointer without evaluating it */
#define nasm_assert_pointer(p) ((void)sizeof(*(p)))
@ -282,8 +284,6 @@ void src_set(int32_t line, const char *filename);
*/
int32_t src_get(int32_t *xline, const char **xname);
char *nasm_strcat(const char *one, const char *two);
char *nasm_skip_spaces(const char *p);
char *nasm_skip_word(const char *p);
char *nasm_zap_spaces_fwd(char *p);