squashfs-tools icon indicating copy to clipboard operation
squashfs-tools copied to clipboard

Do not call unportable sysinfo(2) unconditionally

Open danfe opened this issue 2 years ago • 0 comments

mksquashfs obtains physical memory size via POSIX sysconf(3) call, but, per the comment that sysconf(_SC_PHYS_PAGES) relies on /proc being mounted; if it fails use sysinfo, also calls Linuxish sysinfo(2) unconditionally.

On FreeBSD, sysconf(3) does not depend on /proc (it's not even mounted by default for a long time) and thus should not fail, so unportable second step is not needed and should be guarded as Linux-only:

@@ -5747,6 +5745,7 @@ static int get_physical_memory()
        long long page_size = sysconf(_SC_PAGESIZE);
        int phys_mem;
 
+#ifdef __linux__
        if(num_pages == -1 || page_size == -1) {
                struct sysinfo sys;
                int res = sysinfo(&sys);
@@ -5757,6 +5756,7 @@ static int get_physical_memory()
                num_pages = sys.totalram;
                page_size = sys.mem_unit;
        }
+#endif

Naturally, #inclu'sion of <sys/sysmacros.h> and <sys/sysinfo.h> in the header section should be similarly guarded.

danfe avatar Jul 27 '21 16:07 danfe