Skip to content

Commit

Permalink
Avoid 32-bit overflow in GUI stats
Browse files Browse the repository at this point in the history
Ensure output performs multiplications in float or uint64. Problem only
likely to hit with large swap files at the moment.

Swap file error reported at:

https://www.snbforums.com/threads/displayed-memory-usage-error-in-webui.76204/page-2#post-734474
  • Loading branch information
kjbracey2 authored and RMerl committed Jan 9, 2022
1 parent d3214d0 commit b38ac86
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions release/src/router/httpd/sysinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ void GetPhyStatus_rtk(int *states);
#endif


#define MBYTES 1024 / 1024
#define MBYTES (1024 * 1024)
#define KBYTES 1024

#define SI_WL_QUERY_ASSOC 1
Expand Down Expand Up @@ -209,19 +209,19 @@ int ej_show_sysinfo(int eid, webs_t wp, int argc, char_t ** argv)
}
} else if(strcmp(type,"memory.total") == 0) {
sysinfo(&sys);
sprintf(result,"%.2f",(sys.totalram * sys.mem_unit / (float)MBYTES));
sprintf(result,"%.2f",(float) sys.totalram * sys.mem_unit / MBYTES);
} else if(strcmp(type,"memory.free") == 0) {
sysinfo(&sys);
sprintf(result,"%.2f",(sys.freeram * sys.mem_unit / (float)MBYTES));
sprintf(result,"%.2f",(float) sys.freeram * sys.mem_unit / MBYTES);
} else if(strcmp(type,"memory.buffer") == 0) {
sysinfo(&sys);
sprintf(result,"%.2f",(sys.bufferram * sys.mem_unit / (float)MBYTES));
sprintf(result,"%.2f",(float) sys.bufferram * sys.mem_unit / MBYTES);
} else if(strcmp(type,"memory.swap.total") == 0) {
sysinfo(&sys);
sprintf(result,"%.2f",(sys.totalswap * sys.mem_unit / (float)MBYTES));
sprintf(result,"%.2f",(float) sys.totalswap * sys.mem_unit / MBYTES);
} else if(strcmp(type,"memory.swap.used") == 0) {
sysinfo(&sys);
sprintf(result,"%.2f",((sys.totalswap - sys.freeswap) * sys.mem_unit / (float)MBYTES));
sprintf(result,"%.2f",(float) (sys.totalswap - sys.freeswap) * sys.mem_unit / MBYTES);
} else if(strcmp(type,"memory.cache") == 0) {
int size = 0;
char *buffer = read_whole_file("/proc/meminfo");
Expand Down Expand Up @@ -267,7 +267,7 @@ int ej_show_sysinfo(int eid, webs_t wp, int argc, char_t ** argv)
char *mount_info = read_whole_file("/proc/mounts");

if ((mount_info) && (strstr(mount_info, "/jffs")) && (statvfs("/jffs",&fiData) == 0 )) {
sprintf(result,"%.2f / %.2f MB",((fiData.f_blocks-fiData.f_bfree) * fiData.f_frsize / (float)MBYTES) ,(fiData.f_blocks * fiData.f_frsize / (float)MBYTES));
sprintf(result,"%.2f / %.2f MB",((float) (fiData.f_blocks-fiData.f_bfree) * fiData.f_frsize / MBYTES) ,((float) fiData.f_blocks * fiData.f_frsize / MBYTES));
} else {
strcpy(result,"<i>Unmounted</i>");
}
Expand All @@ -278,7 +278,7 @@ int ej_show_sysinfo(int eid, webs_t wp, int argc, char_t ** argv)
struct statvfs fiData;

if (statvfs("/jffs",&fiData) == 0 ) {
sprintf(result,"%ld",(fiData.f_bfree * fiData.f_frsize / MBYTES));
sprintf(result,"%llu",((unsigned long long) fiData.f_bfree * fiData.f_frsize / MBYTES));
} else {
strcpy(result,"-1");
}
Expand Down

0 comments on commit b38ac86

Please sign in to comment.