]> git.ipfire.org Git - thirdparty/FORT-validator.git/commitdiff
Review strchr() and strrchr() usage
authorAlberto Leiva Popper <ydahhrk@gmail.com>
Tue, 3 Oct 2023 18:49:00 +0000 (12:49 -0600)
committerAlberto Leiva Popper <ydahhrk@gmail.com>
Tue, 3 Oct 2023 18:50:29 +0000 (12:50 -0600)
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.

src/common.c
src/crypto/base64.c
src/init.c
src/rtr/rtr.c
src/types/uri.c

index b3bb81b7e54b55243196fa62589ebb51343f1df6..0037875ce1449d322bb7fdf5d4daeb743009782c 100644 (file)
@@ -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
index aec71ad157e21973983c76ebbacfca746e2375ef..0b7c2fa44c3d76b589a42bc280c0028cc30bf643 100644 (file)
@@ -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;
 
index e2b06747b965eff7b4389b49a5069235d5c2b4bb..01e796c3baba62954f1d43f76282f7a469204ffc 100644 (file)
@@ -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;
index ca196c8dc3d69dfdd9711310f93760ddcca80772..230928c46ad8730307b966d72f223ca68c37d137 100644 (file)
@@ -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;
index 2fb7dbb30e74c0441f86c00eff557670870d4343..c11674f34cd0acc1a07d474722fb6bfbc402af35 100644 (file)
@@ -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;