------------------------------------------------------------ revno: 11712 revision-id: squid3@treenet.co.nz-20121126102330-fsa1fwlzizn8sqcj parent: squid3@treenet.co.nz-20121126102101-m5sgutjl10nfx1jx committer: Amos Jeffries branch nick: 3.2 timestamp: Mon 2012-11-26 03:23:30 -0700 message: ext_file_userip_acl: Polish and missing file handling * 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. ------------------------------------------------------------ # Bazaar merge directive format 2 (Bazaar 0.90) # revision_id: squid3@treenet.co.nz-20121126102330-fsa1fwlzizn8sqcj # target_branch: http://bzr.squid-cache.org/bzr/squid3/branches\ # /SQUID_3_2 # testament_sha1: 6bde8f8c12e55337d2908519d744266717429ab6 # timestamp: 2012-11-26 10:25:01 +0000 # source_branch: http://bzr.squid-cache.org/bzr/squid3/branches\ # /SQUID_3_2 # base_revision_id: squid3@treenet.co.nz-20121126102101-\ # m5sgutjl10nfx1jx # # Begin patch === modified file 'helpers/external_acl/file_userip/ext_file_userip_acl.cc' --- helpers/external_acl/file_userip/ext_file_userip_acl.cc 2012-02-05 06:09:46 +0000 +++ helpers/external_acl/file_userip/ext_file_userip_acl.cc 2012-11-26 10:23:30 +0000 @@ -76,9 +76,6 @@ 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 */ @@ -86,17 +83,28 @@ 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 */ @@ -242,7 +250,11 @@ 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)) {