Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try fixing a couple of more static-analysis issues #154

Merged
merged 3 commits into from
May 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 19 additions & 22 deletions src/pactester.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#define LINEMAX 4096 // Max length of any line read from text files (4 KiB)
#define PACMAX (1024 * 1024) // Max size of the PAC script (1 MiB)

void usage(const char *progname)
__attribute__((noreturn)) void usage(const char *progname)
{
fprintf(stderr, "\nUsage: %s <-p pacfile> <-u url> [-h host] "
"[-c client_ip] [-e]", progname);
Expand Down Expand Up @@ -66,14 +66,15 @@ char *get_host_from_url(const char *url)
if (p[0] == '\0'|| // We reached end without hitting :
p[1] != '/' || p[2] != '/' // Next two characters are not //
) {
fprintf(stderr, "pactester.c: Not a proper URL\n");
free(q);
fprintf(stderr, "pactester.c: Not a proper URL\n");
return NULL;
}
p = p + 3; // Get past '://'
// Host part starts from here.
char *host = p;
if (*p == '\0' || *p == '/' || *p == ':') { // If host part is null.
free(q);
fprintf(stderr, "pactester.c: Not a proper URL\n");
return NULL;
}
Expand Down Expand Up @@ -155,19 +156,16 @@ int main(int argc, char* argv[])
while (fgets(buffer, LINEMAX, stdin)) {
if (strlen(buffer) == 0)
break;
char *old = script;
script_size += strlen(buffer);
if (script_size > PACMAX) {
fprintf(stderr, "Input file is too big. Maximum allowed size is: %d",
PACMAX);
free(script);
return 1;
exit(1);
}
script = realloc(script, script_size);
if (script == NULL) {
perror("pactester.c: Failed to allocate the memory for the script");
free(old);
return 1;
exit(1);
}
strcat(script, buffer);
}
Expand All @@ -181,9 +179,8 @@ int main(int argc, char* argv[])
if (!pacparser_parse_pac_string(script)) {
fprintf(stderr, "pactester.c: Could not parse the pac script: %s\n",
script);
free(script);
pacparser_cleanup();
return 1;
exit(1);
}
free(script);
}
Expand All @@ -192,16 +189,15 @@ int main(int argc, char* argv[])
fprintf(stderr, "pactester.c: Could not parse the pac file: %s\n",
pacfile);
pacparser_cleanup();
return 1;
exit(1);
}
}

if (client_ip)
if (!pacparser_setmyip(client_ip)) {
fprintf(stderr, "pactester.c: Error setting client IP\n");
pacparser_cleanup();
return 1;
}
if (client_ip && !pacparser_setmyip(client_ip)) {
fprintf(stderr, "pactester.c: Error setting client IP\n");
pacparser_cleanup();
exit(1);
}

char *proxy;

Expand All @@ -211,25 +207,26 @@ int main(int argc, char* argv[])
// function will print a proper error message in that case).
host = host ? host: get_host_from_url(url);
if (!host) {
return 1;
exit(1);
}
proxy = pacparser_find_proxy(url, host);
if (proxy == NULL) {
fprintf(stderr, "pactester.c: %s %s.\n",
"Problem in finding proxy for", url);
pacparser_cleanup();
return 1;
exit(1);
}
printf("%s\n", proxy);
exit(0);
}

else if (urlslist) {
if (urlslist) {
char line[LINEMAX];
FILE *fp;
if (!(fp = fopen(urlslist, "r"))) {
fprintf(stderr, "pactester.c: Could not open urlslist: %s", urlslist);
pacparser_cleanup();
return 1;
exit(1);
}
while (fgets(line, sizeof(line), fp)) {
char *url = line;
Expand All @@ -254,13 +251,13 @@ int main(int argc, char* argv[])
fprintf(stderr, "pactester.c: %s %s.\n",
"Problem in finding proxy for", url);
pacparser_cleanup();
free(proxy);
return 1;
exit(1);
}
if (proxy)
printf("%s : %s\n", url, proxy);
}
fclose(fp);
exit(0);
}

pacparser_cleanup();
Expand Down