diff options
| author | Dennis Camera <skonfig@dtnr.ch> | 2022-08-25 22:15:36 +0200 |
|---|---|---|
| committer | Dennis Camera <skonfig@dtnr.ch> | 2022-08-25 22:15:36 +0200 |
| commit | a62828595b9865a13cdc9b4adba3e3aaa9bc8e73 (patch) | |
| tree | 837e8fd58dea93d0a84414e9807ecd5b4b33ac91 /src/util | |
| parent | d73af9d8e436f36b612869422291caeb3239c758 (diff) | |
| download | skonfig-c-a62828595b9865a13cdc9b4adba3e3aaa9bc8e73.tar.gz skonfig-c-a62828595b9865a13cdc9b4adba3e3aaa9bc8e73.zip | |
Enable more __attribute__s
Diffstat (limited to 'src/util')
| -rw-r--r-- | src/util/envp.c | 10 | ||||
| -rw-r--r-- | src/util/envp.h | 23 | ||||
| -rw-r--r-- | src/util/string.h | 15 |
3 files changed, 34 insertions, 14 deletions
diff --git a/src/util/envp.c b/src/util/envp.c index 808d9b8..ca3c848 100644 --- a/src/util/envp.c +++ b/src/util/envp.c @@ -82,7 +82,9 @@ static char *_envp_get_elem(envp_t envp, const char *restrict name) { } char *envp_get(envp_t *envp, const char *name) { +#if !HAVE_CC_NONNULL_CHECK if (NULL == envp) return NULL; +#endif char *elem = _envp_get_elem(*envp, name); @@ -96,6 +98,10 @@ char *envp_get(envp_t *envp, const char *name) { int envp_put(envp_t *envp, char *string) { +#if !HAVE_CC_NONNULL_CHECK + if (NULL == envp || NULL == string) return -1; +#endif + char envname[strchr(string, '=')-string+1]; strncpy(envname, string, sizeof(envname)); @@ -160,7 +166,9 @@ int envp_unset(envp_t *envp, const char *restrict name) { } void envp_merge(envp_t *dest, envp_t *const src) { - if (NULL == src) return; +#if !HAVE_CC_NONNULL_CHECK + if (NULL == src || NULL == dest) return; +#endif for (size_t i = 0; NULL != (*src)[i]; ++i) { envp_put(dest, strdup((*src)[i])); diff --git a/src/util/envp.h b/src/util/envp.h index 37871f9..a696330 100644 --- a/src/util/envp.h +++ b/src/util/envp.h @@ -9,6 +9,11 @@ typedef char ** envp_t; +#if HAVE_FUNC_ATTRIBUTE_MALLOC +/* pre-declare free function for SK_MALLOC_FREE(envp_free) */ +extern void envp_free(envp_t *); +#endif + /** * envp_alloc: * allocates a new, empty envp_t. @@ -17,7 +22,7 @@ typedef char ** envp_t; * * @return new, empty envp_t. */ -envp_t *envp_alloc(void); +envp_t *envp_alloc(void) SK_MALLOC_FREE(envp_free, 1) SK_WARN_UNUSED_RESULT; /** * envp_dup: @@ -27,7 +32,7 @@ envp_t *envp_alloc(void); * * @return new envp_t. */ -envp_t *envp_dup(void); +envp_t *envp_dup(void) SK_MALLOC_FREE(envp_free, 1) SK_WARN_UNUSED_RESULT; /** * envp_get: @@ -37,7 +42,7 @@ envp_t *envp_dup(void); * @param name: the name of the environment variable to return. * @return the value of the environment variable if found, NULL otherwise. */ -char *envp_get(envp_t *envp, const char *name); +char *envp_get(envp_t *envp, const char *name) SK_NONNULL(1, 2); /** * envp_put: @@ -54,7 +59,7 @@ char *envp_get(envp_t *envp, const char *name); * @param string: a string of the form "name=value". * @return 0 on success, -1 otherwise and sets errno. */ -int envp_put(envp_t *envp, char *string); +int envp_put(envp_t *envp, char *string) SK_NONNULL(1, 2); /** * envp_set: @@ -70,7 +75,7 @@ int envp_put(envp_t *envp, char *string); */ int envp_set( envp_t *envp, const char *restrict envname, const char *restrict envval, - bool overwrite); + bool overwrite) SK_NONNULL(1, 2, 3); /** * envp_unset: @@ -80,7 +85,7 @@ int envp_set( * @param name: the name of the environment variable to remove. * @return 0 */ -int envp_unset(envp_t *envp, const char *restrict name); +int envp_unset(envp_t *envp, const char *restrict name) SK_NONNULL(1, 2); /** * envp_merge: @@ -89,7 +94,7 @@ int envp_unset(envp_t *envp, const char *restrict name); * @param dest: the envp to modify. * @param src: the source envp. */ -void envp_merge(envp_t *dest, envp_t *const src); +void envp_merge(envp_t *dest, envp_t *const src) SK_NONNULL(1, 2); /** * envp_print: @@ -98,7 +103,7 @@ void envp_merge(envp_t *dest, envp_t *const src); * @param envp: the envp to print. * @param stream: the stream to output to. */ -void envp_print(envp_t *envp, FILE *restrict stream); +void envp_print(envp_t *envp, FILE *restrict stream) SK_NONNULL(1, 2); /** * envp_free: @@ -106,6 +111,6 @@ void envp_print(envp_t *envp, FILE *restrict stream); * * @param envp: the structure to free. */ -void envp_free(envp_t *envp); +void envp_free(envp_t *envp) SK_NONNULL(1); #endif diff --git a/src/util/string.h b/src/util/string.h index 5e31e21..2359b8f 100644 --- a/src/util/string.h +++ b/src/util/string.h @@ -10,6 +10,11 @@ #include <stddef.h> #include <string.h> +#if HAVE_FUNC_ATTRIBUTE_MALLOC +#include <stdlib.h> /* for SK_MALLOC_FREE(free) */ +#endif + + /** * stringize: * Convert value of preprocessor macro to C string literal. @@ -59,11 +64,13 @@ */ #define string_starts_with(s, p) (strncmp(s, p, strlen(p))) +#ifndef HAVE_STRDUP /** * strdup: * like strdup(3p). */ -char *strdup(const char *s); +char *strdup(const char *s) SK_MALLOC_FREE(free, 1) SK_NONNULL(1); +#endif /** * strajoin: @@ -107,7 +114,7 @@ char *strdup(const char *s); * @param nmemb: the number of elements of @a * @return a newly-allocated C string containing all of the elements joined */ -char *stranjoin(char sep, const char *a[], size_t nmemb); +char *stranjoin(char sep, const char *a[], size_t nmemb) SK_WARN_UNUSED_RESULT SK_MALLOC_FREE(free, 1); /** * stranjoin_r: @@ -132,9 +139,9 @@ size_t stranjoin_r( char *_strjoin( /* internal */ - char sep, void *_, /* const char * */...); + char sep, void *_, /* const char * */...) SK_SENTINEL(0) SK_WARN_UNUSED_RESULT SK_MALLOC_FREE(free, 1); size_t _strjoin_r( /* internal */ - char sep, char *restrict buf, size_t buflen, /* const char * */...); + char sep, char *restrict buf, size_t buflen, /* const char * */...) SK_SENTINEL(0); /** * strjoin: |
