From 142f55107064d4cbd4e70ae625e909caa8d13952 Mon Sep 17 00:00:00 2001 From: "Kelvin M. Klann" Date: Thu, 20 Jul 2023 04:51:35 -0300 Subject: hostnames.c: fix scan-build warning This is breaking scan-build in CI[1]: /usr/share/clang/scan-build-14/bin/../libexec/ccc-analyzer [...] -c hostnames.c -o hostnames.o hostnames.c:59:10: warning: Null pointer passed to 1st parameter expecting 'nonnull' [core.NonNullParamChecker] return strdup(rv); ^~~~~~~~~~ 1 warning generated. Likely caused by commit d2802ce60 ("fnettrace cleanup", 2023-07-15). This also fixes a memory leak of `cmd`. [1] https://github.com/netblue30/firejail/actions/runs/5568460702/jobs/10171098449 --- src/fnettrace/hostnames.c | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/fnettrace/hostnames.c b/src/fnettrace/hostnames.c index 7cb34e2c4..14ebc4ca0 100644 --- a/src/fnettrace/hostnames.c +++ b/src/fnettrace/hostnames.c @@ -40,30 +40,32 @@ char *retrieve_hostname(uint32_t ip) { errExit("asprintf"); FILE *fp = popen(cmd, "r"); - if (fp) { - char *ptr; - if (fgets(buf, MAXBUF, fp)) { - ptr = strchr(buf, '\n'); - if (ptr) - *ptr = '\0'; - if (strncmp(buf, "GeoIP Country Edition:", 22) == 0) { - ptr = buf + 22; - if (*ptr == ' ' && *(ptr + 3) == ',' && *(ptr + 4) == ' ') { - rv = ptr + 5; - if (strcmp(rv, "United States") == 0) - rv = "US"; - } + if (!fp) { + geoip_not_found = 1; + goto out; + } + + char *ptr; + if (fgets(buf, MAXBUF, fp)) { + ptr = strchr(buf, '\n'); + if (ptr) + *ptr = '\0'; + if (strncmp(buf, "GeoIP Country Edition:", 22) == 0) { + ptr = buf + 22; + if (*ptr == ' ' && *(ptr + 3) == ',' && *(ptr + 4) == ' ') { + rv = ptr + 5; + if (strcmp(rv, "United States") == 0) + rv = "US"; } } - pclose(fp); - return strdup(rv); } - else - geoip_not_found = 1; + pclose(fp); + if (rv) + rv = strdup(rv); +out: free(cmd); - - return NULL; + return rv; } void load_hostnames(const char *fname) { -- cgit v1.2.3-54-g00ecf