Fix a memory leak from use of realpath() in HTTP request processing.

realpath() returns a heap-allocated buffer if given NULL for the destination buffer.
This must be freed by the caller; dump1090 does not do this.

Instead of worrying about freeing it, take the simpler approach of just providing a
stack-allocated destination buffer.
This commit is contained in:
Oliver Jowett 2014-12-07 14:05:24 +00:00
parent bff92c4ad7
commit d09eefb827
2 changed files with 8 additions and 5 deletions

View file

@ -57,6 +57,7 @@
#include <ctype.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <limits.h>
#include "rtl-sdr.h"
#include "anet.h"
#else

View file

@ -762,14 +762,16 @@ int handleHTTPRequest(struct client *c, char *p) {
} else {
struct stat sbuf;
int fd = -1;
char *rp, *hrp;
char rp[PATH_MAX], hrp[PATH_MAX];
if (!realpath(getFile, rp))
rp[0] = 0;
if (!realpath(HTMLPATH, hrp))
strcpy(hrp, HTMLPATH);
rp = realpath(getFile, NULL);
hrp = realpath(HTMLPATH, NULL);
hrp = (hrp ? hrp : HTMLPATH);
clen = -1;
content = strdup("Server error occured");
if (rp && (!strncmp(hrp, rp, strlen(hrp)))) {
if (!strncmp(hrp, rp, strlen(hrp))) {
if (stat(getFile, &sbuf) != -1 && (fd = open(getFile, O_RDONLY)) != -1) {
content = (char *) realloc(content, sbuf.st_size);
if (read(fd, content, sbuf.st_size) != -1) {