]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
ext_file_userip_acl: Polish and missing file handling
authorAmos Jeffries <squid3@treenet.co.nz>
Wed, 14 Nov 2012 06:15:15 +0000 (23:15 -0700)
committerAmos Jeffries <squid3@treenet.co.nz>
Wed, 14 Nov 2012 06:15:15 +0000 (23:15 -0700)
* Display error when dictionary file is unaccessible instead of crashing.

* Polish several useless assignments out of the code.

 Detected by Coverity Scan. Issues 740402, 740403, 740589, 740590.

helpers/external_acl/file_userip/ext_file_userip_acl.cc

index d9a118470f0681d2267f16f5a45b6cacf98e67c2..6f32aabfa2e918449191c97919b56f55eead8640 100644 (file)
@@ -75,9 +75,6 @@ load_dict(FILE * FH) {
                                                   linked list */
     char line[DICT_BUFFER_SIZE]; /* the buffer for the lines read
                                   from the dict file */
-    char *cp;                  /* a char pointer used to parse
-                                  each line */
-    char *username;            /* for the username */
     char *tmpbuf;                      /* for the address before the
                                   bitwise AND */
 
@@ -85,17 +82,28 @@ load_dict(FILE * FH) {
     first_entry = (struct ip_user_dict*)malloc(sizeof(struct ip_user_dict));
     current_entry = first_entry;
 
-    while ((cp = fgets (line, DICT_BUFFER_SIZE, FH)) != NULL) {
+    unsigned int lineCount = 0;
+    while (fgets(line, sizeof(line), FH) != NULL) {
+        ++lineCount;
         if (line[0] == '#') {
             continue;
         }
+
+        char *cp; // a char pointer used to parse each line.
         if ((cp = strchr (line, '\n')) != NULL) {
             /* chop \n characters */
             *cp = '\0';
         }
-        if ((cp = strtok (line, "\t ")) != NULL) {
+        if (strtok(line, "\t ") != NULL) {
+            // NP: line begins with IP/mask. Skipped to the end of it with this strtok()
+
             /* get the username */
-            username = strtok (NULL, "\t ");
+            char *username;
+            if ((username = strtok(NULL, "\t ")) == NULL) {
+                debug("Missing username on line %u of dictionary file\n", lineCount);
+                continue;
+            }
+
             /* look for a netmask */
             if ((cp = strtok (line, "/")) != NULL) {
                 /* store the ip address in a temporary buffer */
@@ -241,7 +249,11 @@ main (int argc, char *argv[])
         usage(program_name);
         exit(1);
     }
-    FH = fopen(filename, "r");
+    FILE *FH = fopen(filename, "r");
+    if (!FH) {
+        fprintf(stderr, "%s: FATAL: Unable to open file '%s': %s", program_name, filename, xstrerror());
+        exit(1);
+    }
     current_entry = load_dict(FH);
 
     while (fgets(line, HELPER_INPUT_BUFFER, stdin)) {