bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
* [PATCH] Fix getloadavg bug
@ 2020-06-01 16:49 Семен Верченко
  2020-06-02  0:58 ` Paul Eggert
  0 siblings, 1 reply; 2+ messages in thread
From: Семен Верченко @ 2020-06-01 16:49 UTC (permalink / raw)
  To: bug-gnulib

getloadavg function increases elem counter twice:

  for (elem = 0; elem < nelem; elem++) // Here
    {
      // ...

      loadavg[elem++] = numerator / denominator; // And here
    }

It leads to wrong Load Average in uptime command:
$ cat /proc/loadavg
0.01 0.02 0.00 1/122 992
$ uptime
 18:36:59 up   6:20,  0 users,  load average: 0,01, 0,00, 0,02

This patch fixes this bug
---
 lib/getloadavg.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/getloadavg.c b/lib/getloadavg.c
index 61307d377..aeb7070cc 100644
--- a/lib/getloadavg.c
+++ b/lib/getloadavg.c
@@ -550,7 +550,7 @@ getloadavg (double loadavg[], int nelem)
         for (ptr++; '0' <= *ptr && *ptr <= '9'; ptr++)
           numerator = 10 * numerator + (*ptr - '0'), denominator *= 10;
 -      loadavg[elem++] = numerator / denominator;
+      loadavg[elem] = numerator / denominator;
     }
    return elem;
-- 
2.26.2



^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] Fix getloadavg bug
  2020-06-01 16:49 [PATCH] Fix getloadavg bug Семен Верченко
@ 2020-06-02  0:58 ` Paul Eggert
  0 siblings, 0 replies; 2+ messages in thread
From: Paul Eggert @ 2020-06-02  0:58 UTC (permalink / raw)
  To: Семен Верченко
  Cc: bug-gnulib

[-- Attachment #1: Type: text/plain, Size: 74 bytes --]

Thanks, I installed that into Gnulib master on savannah via the attached.

[-- Attachment #2: 0001-getloadavg-fix-double-increment-bug.patch --]
[-- Type: text/x-patch, Size: 1714 bytes --]

From 1770bc4660a40265d6b9ee111f52e7c4c7ebb4c2 Mon Sep 17 00:00:00 2001
From: Paul Eggert <eggert@cs.ucla.edu>
Date: Mon, 1 Jun 2020 17:57:27 -0700
Subject: [PATCH] getloadavg: fix double-increment bug

* lib/getloadavg.c (getloadavg): Fix double-increment typo on
Linux without glibc, Android, Cygwin.  This fixes a bug I
introduced in 2011-02-08T20:23:29Z!eggert@cs.ucla.edu.
Problem and fix reported by Semen Verchenko in:
https://lists.gnu.org/r/bug-gnulib/2020-06/msg00007.html
---
 ChangeLog        | 7 +++++++
 lib/getloadavg.c | 2 +-
 2 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/ChangeLog b/ChangeLog
index 5a07e5ae7..7e3e70852 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,12 @@
 2020-06-01  Paul Eggert  <eggert@cs.ucla.edu>
 
+	getloadavg: fix double-increment bug
+	* lib/getloadavg.c (getloadavg): Fix double-increment typo on
+	Linux without glibc, Android, Cygwin.  This fixes a bug I
+	introduced in 2011-02-08T20:23:29Z!eggert@cs.ucla.edu.
+	Problem and fix reported by Semen Verchenko in:
+	https://lists.gnu.org/r/bug-gnulib/2020-06/msg00007.html
+
 	tempname: use getrandom, not getentropy
 	This removes a dependency, as getentropy depends on getrandom.
 	* lib/tempname.c: Include sys/random.h instead of unistd.h.
diff --git a/lib/getloadavg.c b/lib/getloadavg.c
index 61307d377..aeb7070cc 100644
--- a/lib/getloadavg.c
+++ b/lib/getloadavg.c
@@ -550,7 +550,7 @@ getloadavg (double loadavg[], int nelem)
         for (ptr++; '0' <= *ptr && *ptr <= '9'; ptr++)
           numerator = 10 * numerator + (*ptr - '0'), denominator *= 10;
 
-      loadavg[elem++] = numerator / denominator;
+      loadavg[elem] = numerator / denominator;
     }
 
   return elem;
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-06-02  0:59 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-01 16:49 [PATCH] Fix getloadavg bug Семен Верченко
2020-06-02  0:58 ` Paul Eggert

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).