while (fgets(buf, PATH_MAX, fp) != NULL) {
if ((idx = strstr(buf, "MemTotal:"))) {
idx += strlen("Memtotal:");
- while (*idx < '0' || *idx > '9')
+ while ((idx < buf + PATH_MAX) && (*idx < '0' || *idx > '9'))
idx++;
total_mem = atoi(idx);
} else if ((idx = strstr(buf, "MemFree:"))) {
idx += strlen("MemFree:");
- while (*idx < '0' || *idx > '9')
+ while ((idx < buf + PATH_MAX) && (*idx < '0' || *idx > '9'))
idx++;
free = atoi(idx);
} else if ((idx = strstr(buf, "MemAvailable:"))) {
idx += strlen("MemAvailable:");
- while (*idx < '0' || *idx > '9')
+ while ((idx < buf + PATH_MAX) && (*idx < '0' || *idx > '9'))
idx++;
available = atoi(idx);
} else if ((idx = strstr(buf, "Cached:")) && !strstr(buf, "Swap")) {
idx += strlen("Cached:");
- while (*idx < '0' || *idx > '9')
+ while ((idx < buf + PATH_MAX) && (*idx < '0' || *idx > '9'))
idx++;
cached = atoi(idx);
} else if ((idx = strstr(buf, "SwapTotal:"))) {
idx += strlen("SwapTotal:");
- while (*idx < '0' || *idx > '9')
+ while ((idx < buf + PATH_MAX) && (*idx < '0' || *idx > '9'))
idx++;
swap_total = atoi(idx);
} else if ((idx = strstr(buf, "SwapFree:"))) {
idx += strlen("SwapFree");
- while (*idx < '0' || *idx > '9')
+ while ((idx < buf + PATH_MAX) && (*idx < '0' || *idx > '9'))
idx++;
swap_free = atoi(idx);
break;