[PATCH] Fix checking for "simple integers"

Florian Forster <[email protected]>
Newsgroups gmane.comp.db.rrdtool.devel
Message-ID <[email protected]>
Hi,

in src/rrd_update.c there is this code (which is checking for "simple
integers"):

  if ( (   updvals[ds_idx + 1][0] < '0'
        || updvals[ds_idx + 1][0] > '9' )
       && updvals[ds_idx + 1][0] != '-'
       && updvals[ds_idx + 1][0] != 'U'
       && updvals[ds_idx + 1][0] == '\0'
     ) { ... }

If you look closely, you notice that this is the same as saying:

  if (updvals[ds_idx + 1][0] == '\0') { ... }

I've written a small patch which changes the behavior to what I assume is
intended and is hopefully a good deal easier to read than the existing check.

Regards,
-octo
-- 
Florian octo Forster
Hacker in training
GnuPG: 0x91523C3D
http://verplant.org/

_______________________________________________
rrd-developers mailing list
[email protected]
https://lists.oetiker.ch/cgi-bin/listinfo/rrd-developers
rrdtool-simple-integer-r1999.patch (text/x-diff, 2 KB)
diff --git a/src/rrd_update.c b/src/rrd_update.c
index 9526ec2..4f30fee 100644
--- a/src/rrd_update.c
+++ b/src/rrd_update.c
@@ -1043,25 +1043,22 @@ static int update_pdp_prep(
             switch (dst_idx) {
             case DST_COUNTER:
             case DST_DERIVE:
-                if ( (   updvals[ds_idx + 1][0] < '0'
-                      || updvals[ds_idx + 1][0] > '9' )
-                     && updvals[ds_idx + 1][0] != '-'
-                     && updvals[ds_idx + 1][0] != 'U'
-                     && updvals[ds_idx + 1][0] == '\0'
-                   ) {
-                    rrd_set_error("not a simple integer: '%s'",
-                                  updvals[ds_idx + 1]);
-                    return -1;
-                }
-                for (ii = 1; updvals[ds_idx + 1][ii] != '\0'; ii++) {
-                    if (    updvals[ds_idx + 1][ii] < '0'
-                         || updvals[ds_idx + 1][ii] > '9'
-                       ) {
-                        rrd_set_error("not a simple integer: '%s'",
-                                      updvals[ds_idx + 1]);
+                /* Check if this is a valid integer. `U' is already handled in
+                 * another branch. */
+                for (ii = 0; updvals[ds_idx + 1][ii] != 0; ii++) {
+                    if ((ii == 0) && (dst_idx == DST_DERIVE)
+                            && (updvals[ds_idx + 1][ii] == '-'))
+                        continue;
+
+                    if ((updvals[ds_idx + 1][ii] < '0')
+                            || (updvals[ds_idx + 1][ii] > '9')) {
+                        rrd_set_error("not a simple %s integer: '%s'",
+                                (dst_idx == DST_DERIVE) ? "signed" : "unsigned",
+                                updvals[ds_idx + 1]);
                         return -1;
                     }
-                }
+                } /* for (ii = 0; updvals[ds_idx + 1][ii] != 0; ii++) */
+
                 if (rrd->pdp_prep[ds_idx].last_ds[0] != 'U') {
                     pdp_new[ds_idx] =
                         rrd_diff(updvals[ds_idx + 1],
signature.asc (application/pgp-signature, 189 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFLTt86Hdggu3Q05IYRAocDAKCQZeqtWG+N3ewvxsXuZiwbH5yiYQCgkjSx
fRswvzxhxiKe20GNRUU+yM8=
=Dfxk
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.