]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
tor_vsscanf(): Don't return -1 if '%%' doesn't match.
authorGeorge Kadianakis <desnacked@riseup.net>
Tue, 3 Apr 2012 14:20:24 +0000 (16:20 +0200)
committerGeorge Kadianakis <desnacked@riseup.net>
Tue, 3 Apr 2012 14:20:24 +0000 (16:20 +0200)
tor_vsscanf() is supposed to return the current number of matches on
match failure.

changes/bug5558 [new file with mode: 0644]
src/common/util.c
src/test/test_util.c

diff --git a/changes/bug5558 b/changes/bug5558
new file mode 100644 (file)
index 0000000..90ab3a6
--- /dev/null
@@ -0,0 +1,3 @@
+  o Minor bugfixes
+    - Enforce correct return behavior of tor_vsscanf(), when the '%%'
+      pattern is used. Fixes bug 5558.
index 266368cc9d0083232cf8646c7ef1d0edc0e2a3f3..551ee1796f723eef07313e3f5c5c1ab1fd66109e 100644 (file)
@@ -2773,7 +2773,7 @@ tor_vsscanf(const char *buf, const char *pattern, va_list ap)
         ++n_matched;
       } else if (*pattern == '%') {
         if (*buf != '%')
-          return -1;
+          return n_matched;
         ++buf;
         ++pattern;
       } else {
index 88f00e071be0452cd5f5f89c1c88a6b7dc125bc9..f2123c5540d1a85fd6c53c68ba1613aee5b6d9a1 100644 (file)
@@ -1384,7 +1384,6 @@ test_util_sscanf(void)
   test_eq(-1, tor_sscanf("wrong", "%5c", s1)); /* %c cannot have a number. */
   test_eq(-1, tor_sscanf("hello", "%s", s1)); /* %s needs a number. */
   test_eq(-1, tor_sscanf("prettylongstring", "%999999s", s1));
-  test_eq(-1, tor_sscanf("We're the 99 monkeys", "We're the 99%%"));
 #if 0
   /* GCC thinks these two are illegal. */
   test_eq(-1, tor_sscanf("prettylongstring", "%0s", s1));
@@ -1468,6 +1467,11 @@ test_util_sscanf(void)
   /* Literal '%' (ie. '%%') */
   test_eq(1, tor_sscanf("99% fresh", "%3u%% fresh", &u1));
   test_eq(99, u1);
+  test_eq(0, tor_sscanf("99 fresh", "%% %3u %s", &u1, s1));
+  test_eq(1, tor_sscanf("99 fresh", "%3u%% %s", &u1, s1));
+  test_eq(2, tor_sscanf("99 fresh", "%3u %5s %%", &u1, s1));
+  test_eq(99, u1);
+  test_streq(s1, "fresh");
   test_eq(1, tor_sscanf("% boo", "%% %3s", s1));
   test_streq("boo", s1);