From: Alberto Leiva Popper Date: Tue, 3 Oct 2023 18:49:00 +0000 (-0600) Subject: Review strchr() and strrchr() usage X-Git-Tag: 1.6.0~54 X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=f59248e98ed8dbbd165d926d3c27b3bfc555dda0;p=thirdparty%2FFORT-validator.git Review strchr() and strrchr() usage The code was sometimes falling into the strchr() const trap. This wasn't really a problem because literals were never involved, and the strings were not thread-shared. But better be safe than sorry. --- diff --git a/src/common.c b/src/common.c index b3bb81b7..0037875c 100644 --- a/src/common.c +++ b/src/common.c @@ -93,7 +93,9 @@ static int process_file(char const *dir_name, char const *file_name, char const *file_ext, int *fcount, process_file_cb cb, void *arg) { - char *ext, *fullpath, *tmp; + char const *ext; + char *fullpath; + char *tmp; int error; if (file_ext != NULL) { @@ -223,7 +225,7 @@ valid_file_or_dir(char const *location, bool check_file, bool check_dir, } static int -dir_exists(char const *path, bool *result) +dir_exists(char *path, bool *result) { struct stat _stat; char *last_slash; @@ -285,31 +287,28 @@ create_dir_recursive(char const *path) { char *localuri; int i, error; - bool exist = false; + bool exist; - error = dir_exists(path, &exist); - if (error) - return error; - if (exist) - return 0; + localuri = pstrdup(path); /* Remove const */ - localuri = pstrdup(path); + exist = false; + error = dir_exists(localuri, &exist); + if (error || exist) + goto end; for (i = 1; localuri[i] != '\0'; i++) { if (localuri[i] == '/') { localuri[i] = '\0'; error = create_dir(localuri); localuri[i] = '/'; - if (error) { - /* error msg already printed */ - free(localuri); - return error; - } + if (error) + goto end; /* error msg already printed */ } } +end: free(localuri); - return 0; + return error; } static int diff --git a/src/crypto/base64.c b/src/crypto/base64.c index aec71ad1..0b7c2fa4 100644 --- a/src/crypto/base64.c +++ b/src/crypto/base64.c @@ -195,9 +195,10 @@ free_copy: } static char * -to_base64url(char *base, size_t base_len) +to_base64url(char const *base, size_t base_len) { - char *pad, *tmp; + char const *pad; + char *tmp; size_t len; int i; diff --git a/src/init.c b/src/init.c index e2b06747..01e796c3 100644 --- a/src/init.c +++ b/src/init.c @@ -10,7 +10,7 @@ fetch_url(char const *url) { char const *prefix = "https://"; char const *dest_dir; - char *dest_file; + char const *dest_file; char *dest; size_t prefix_len; size_t url_len; diff --git a/src/rtr/rtr.c b/src/rtr/rtr.c index ca196c8d..230928c4 100644 --- a/src/rtr/rtr.c +++ b/src/rtr/rtr.c @@ -73,7 +73,7 @@ destroy_db(void) static int parse_address(char const *full_address, char **address, char **service) { - char *ptr; + char const *ptr; char *tmp_addr; char *tmp_serv; size_t tmp_addr_len; diff --git a/src/types/uri.c b/src/types/uri.c index 2fb7dbb3..c11674f3 100644 --- a/src/types/uri.c +++ b/src/types/uri.c @@ -179,7 +179,7 @@ static int ia5str2global(struct rpki_uri *uri, char const *mft, IA5String_t *ia5) { char *joined; - char *slash_pos; + char const *slash_pos; int dir_len; int error;