]> the.earth.li Git - onak.git/commitdiff
Fix issues found by llvm scan-build static analysis
authorJonathan McDowell <noodles@earth.li>
Thu, 14 Sep 2023 06:53:41 +0000 (12:23 +0530)
committerJonathan McDowell <noodles@earth.li>
Thu, 14 Sep 2023 06:53:41 +0000 (12:23 +0530)
A mixture of fixes: a number of uses of getenv into functions that don't
like NULL, a couple of potential double-setting of config file details
resulting in a brief memory leak, and a true, but not an issue, report
of setting without using of our offset within a packet.

cgi/hashquery.c
getcgi.c
onak.c
sigcheck.c
wotsap.c

index 5c056a2d3298ab3bb37769cbcd92eb1c0faae7f4..a8ca3d06109d0b909a80c6aaa7ef4c84f6db65e9 100644 (file)
@@ -42,7 +42,7 @@ void doerror(char *error)
 
 int main(int argc, char *argv[])
 {
-       char *request_method;
+       char *request_method, *env;
        int count, found, i;
        uint8_t **hashes;
        struct buffer_ctx cgipostbuf;
@@ -57,7 +57,8 @@ int main(int argc, char *argv[])
                doerror("hashquery must be a HTTP POST request.\n");
        }
 
-       if (!(cgipostbuf.size = atoi(getenv("CONTENT_LENGTH")))) {
+       env = getenv("CONTENT_LENGTH");
+       if ((env == NULL) || !(cgipostbuf.size = atoi(env))) {
                doerror("Must provide a content length.\n");
        }
 
index c6f60c89afd5d6ce977733d252c384fa84039586..7c0656020690be5e14dddb57fcf5fb8a98f59f52 100644 (file)
--- a/getcgi.c
+++ b/getcgi.c
@@ -139,7 +139,7 @@ void unescape_url(char *url)
 char **getcgivars(int argc, char *argv[]) 
 {
        int i;
-       char *request_method;
+       char *request_method, *env;
        int content_length, paircount;
        char *cgiinput = NULL;
        char **cgivars = NULL;
@@ -161,27 +161,31 @@ char **getcgivars(int argc, char *argv[])
                return NULL;
        } else if (!strcmp(request_method, "GET") ||
                        !strcmp(request_method, "HEAD")) {
-               cgiinput=strdup(getenv("QUERY_STRING"));
+               env = getenv("QUERY_STRING");
+               if (env != NULL) {
+                       cgiinput = strdup(env);
+               }
        } else if (!strcmp(request_method, "POST")) {
-               if (getenv("CONTENT_TYPE") != NULL &&
-                               strcasecmp(getenv("CONTENT_TYPE"),
-                                       "application/x-www-form-urlencoded")) {
+               env = getenv("CONTENT_TYPE");
+               if ((env != NULL) && strcasecmp(env,
+                               "application/x-www-form-urlencoded")) {
                        printf("getcgivars(): Unsupported Content-Type.\n");
                        exit(1);
                }
-               
-               if (!(content_length = atoi(getenv("CONTENT_LENGTH")))) {
+
+               env = getenv("CONTENT_LENGTH");
+               if ((env == NULL) || !(content_length = atoi(env))) {
                        printf("getcgivars(): No Content-Length was sent with"
                                        " the POST request.\n");
                        exit(1);
                }
-               
-               if (!(cgiinput= (char *) malloc(content_length+1))) {
+
+               if (!(cgiinput = (char *) malloc(content_length+1))) {
                        printf("getcgivars(): Could not malloc for "
                                        "cgiinput.\n");
                        exit(1);
                }
-               
+
                if (!fread(cgiinput, content_length, 1, stdin)) {
                        printf("Couldn't read CGI input from STDIN.\n");
                        exit(1);
@@ -194,6 +198,11 @@ char **getcgivars(int argc, char *argv[])
                exit(1);
        }
 
+       /* If we didn't get any cgiinput info, nothing to return */
+       if (cgiinput == NULL) {
+               return NULL;
+       }
+
        /* Change all plusses back to spaces */
 
        for(i=0; cgiinput[i]; i++) if (cgiinput[i]=='+') cgiinput[i] = ' ';
diff --git a/onak.c b/onak.c
index a27ff983e78908d6de2e57013173d7b97d7118cf..d21ce7afb683c99cb675aa5c453d1f1006bb55de 100644 (file)
--- a/onak.c
+++ b/onak.c
@@ -187,6 +187,9 @@ int main(int argc, char *argv[])
                        binary = true;
                        break;
                case 'c':
+                       if (configfile != NULL) {
+                               free(configfile);
+                       }
                        configfile = strdup(optarg);
                        break;
                case 'e':
index 29ab652e71dda830273b4f45c1bd994094ece9e0..2c1fc4c0871adc3bde0b5de10db26c7a57cd0b65 100644 (file)
@@ -263,6 +263,12 @@ static onak_status_t onak_parse_key_material(struct openpgp_packet *pk,
                return ONAK_E_UNSUPPORTED_FEATURE;
        }
 
+       /*
+        * Keep scan-build happy; we bump this in MPI_TO_MPZ and then don't use
+        * it again the last time we do so.
+        */
+       (void)ofs;
+
        key->type = pk->data[5];
 
        if (ret != ONAK_E_OK) {
@@ -311,7 +317,7 @@ onak_status_t onak_check_hash_sig(struct openpgp_publickey *sigkey,
                }
 
                /* Skip to the signature material */
-               ofs += 19;
+               ofs = 19;
                sigkeytype = sig->data[15];
        } else if (sig->data[0] >= 4) {
                /* Skip the hashed data */
index 8996c9b2d11549ba8e9f7ff93177b8776db4e5e4..0a9f9df543df48c4b0852c2716fd623ea389af7a 100644 (file)
--- a/wotsap.c
+++ b/wotsap.c
@@ -197,6 +197,9 @@ int main(int argc, char *argv[])
        while ((optchar = getopt(argc, argv, "c:")) != -1 ) {
                switch (optchar) {
                case 'c':
+                       if (configfile != NULL) {
+                               free(configfile);
+                       }
                        configfile = strdup(optarg);
                        break;
                }