OsGetPageProt:Fix leaking FDs. Ensure we return -1 if addr not in range.
@@ -490,7 +490,7 @@ osThreadInit(void(*handler)(int), unsigned interval)
int
osGetPageProt(uint64_t addr)
{
- int prot = 0;
+ int prot = -1;
size_t len = 0;
char *buf = NULL;
char log[128];
@@ -501,19 +501,21 @@ osGetPageProt(uint64_t addr)
FILE *fstream = g_fn.fopen("/proc/self/maps", "r");
if (fstream == NULL) return -1;
- errno = 0;
while (g_fn.getline(&buf, &len, fstream) != -1) {
char *end = NULL;
+ errno = 0;
uint64_t addr1 = strtoull(buf, &end, 0x10);
if ((addr1 == 0) || (errno != 0)) {
if (buf) free(buf);
+ g_fn.fclose(fstream);
return -1;
}
uint64_t addr2 = strtoull(end + 1, &end, 0x10);
if ((addr2 == 0) || (errno != 0)) {
if (buf) free(buf);
+ g_fn.fclose(fstream);
return -1;
}
@@ -525,6 +527,7 @@ osGetPageProt(uint64_t addr)
snprintf(log, sizeof(log), "matched 0x%lx to 0x%lx-0x%lx\n\t%c%c%c",
addr, addr1, addr2, perms[0], perms[1], perms[2]);
scopeLog(log, -1, CFG_LOG_DEBUG);
+ prot = 0;
prot |= perms[0] == 'r' ? PROT_READ : 0;
prot |= perms[1] == 'w' ? PROT_WRITE : 0;
prot |= perms[2] == 'x' ? PROT_EXEC : 0;
