Package: vsearch
Followup-For: Bug #776814

This changes vsearch to use the more portable sysconf API for memory
sizing.  (sysconf is posix, while _SC_PHYS_PAGES is an extension)

While this does change the memory sizing method used on Linux,
I verified that the results are the same on my Jessie amd64 system:

    $ uname -a
    Linux babs 3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt2-1 (2014-12-08) x86_64 
GNU/Linux
    $ cat memsize.c
    #include <stdio.h>
    #include <sys/sysinfo.h>
    #include <unistd.h>

    static
    long long memsize_sysconf() {
        return (long long)sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE);
    }

    long long memsize_sysinfo() {
        struct sysinfo si;
        if(sysinfo(&si) < 0) return 0;
        int mem_unit = si.mem_unit ?: 1;
        return (long long)si.totalram * mem_unit;
    }

    int main() {
        printf("memsize via sysconf: %16lld\n", memsize_sysconf());
        printf("memsize via sysinfo: %16lld\n", memsize_sysinfo());
        return memsize_sysconf() != memsize_sysinfo();
    }
    $ gcc memsize.c && ./a.out || echo fail
    memsize via sysconf:       8078680064
    memsize via sysinfo:       8078680064

-- System Information:
Debian Release: 8.0
  APT prefers testing
  APT policy: (990, 'testing'), (500, 'stable'), (1, 'experimental')
Architecture: kfreebsd-amd64 (x86_64)

Kernel: kFreeBSD 10.1-0-amd64
Locale: LANG=en_US.UTF-8, LC_CTYPE=en_US.UTF-8 (charmap=UTF-8)
Shell: /bin/sh linked to /bin/dash
Init: sysvinit (via /sbin/init)
Description: <short summary of the patch>
 By using the POSIX sysconf(3) together with the nonstandard extension
 _SC_PHYS_PAGES, the system's installed memory can be queried portably on
 Linux and kFreeBSD.
 .
 vsearch (1.0.7+dfsg-1.1) UNRELEASED; urgency=medium
 .
   * Non-maintainer upload.
   * Port to debian kfreebsd (Closes: #776814)
Author: Jeff Epler <[email protected]>

---

Bug-Debian: https://bugs.debian.org/776814
Forwarded: no
Reviewed-By: <name and email of someone who approved the patch>
Last-Update: <2015-02-03>

--- vsearch-1.0.7+dfsg.orig/src/arch.cc
+++ vsearch-1.0.7+dfsg/src/arch.cc
@@ -21,6 +21,8 @@
 
 #include "vsearch.h"
 
+#include <unistd.h>
+
 unsigned long arch_get_memused()
 {
   struct rusage r_usage;
@@ -44,6 +46,15 @@ unsigned long arch_get_memtotal()
   if(-1 == sysctl(mib, 2, &ram, &length, NULL, 0))
     fatal("Cannot determine amount of RAM");
   return ram;
+#elif defined(_SC_PHYS_PAGES) && defined(_SC_PAGESIZE)
+  long phys_pages = sysconf(_SC_PHYS_PAGES),
+       pagesize = sysconf(_SC_PAGESIZE);
+  // sysconf(3) notes that pagesize * phys_pages can overflow, such as
+  // when long is 32-bits and there's more than 4GB RAM.  Since vsearch
+  // apparently targets LP64 systems like x86_64 linux, this will not
+  // arise in practice on the intended platform.
+  if( pagesize > LONG_MAX / phys_pages )  return LONG_MAX;
+  return pagesize * phys_pages;
 #else
   struct sysinfo si;
   if (sysinfo(&si))

Reply via email to