apr-bugs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bugzi...@apache.org
Subject DO NOT REPLY [Bug 48476] apr_psprintf() does not parse the ll or hh size specifiers
Date Fri, 20 Aug 2010 17:07:19 GMT
https://issues.apache.org/bugzilla/show_bug.cgi?id=48476

--- Comment #5 from Chris Knight <Christopher.D.Knight@nasa.gov> 2010-08-20 13:07:13
EDT ---
As discussed on the apr list, %ll, on MacOSX (and I assume on other 64-bit
operating systems) is not recognized as a valid % format. This behavior is
incompatible with the libc printf/sprintf/... formatters and causes crashes,
such as with the below example. My suggestion is to always consider %ll as a
QUAD type, consider %l a QUAD if on 64-bit.

---example---
#include <apr.h>
#include <apr_pools.h>
#include <apr_strings.h>
#include <stdio.h>

int main(int argc, char **argv) {
  apr_pool_t *pool = NULL;
  char *s = "hello world"; u_int64_t v = 12345678;

  apr_pool_initialize(); apr_pool_create(&(pool), NULL);
  printf("%llu%s", v, s); // works
  char *f = apr_psprintf(pool, "%llu%s", v, s); // segfault on strlen
  printf("%s\n", f);
}

---patch---

--- apr_snprintf.c.old    2010-08-20 10:03:41.000000000 -0700
+++ apr_snprintf.c    2010-08-20 10:01:42.000000000 -0700
@@ -813,12 +813,7 @@
              * Modifier check.  Note that if APR_INT64_T_FMT is "d",
              * the first if condition is never true.
              */
-            if ((sizeof(APR_INT64_T_FMT) == 4 &&
-                 fmt[0] == APR_INT64_T_FMT[0] &&
-                 fmt[1] == APR_INT64_T_FMT[1]) ||
-                (sizeof(APR_INT64_T_FMT) == 3 &&
-                 fmt[0] == APR_INT64_T_FMT[0]) ||
-                (sizeof(APR_INT64_T_FMT) > 4 &&
+            if ((sizeof(APR_INT64_T_FMT) > 4 &&
                  strncmp(fmt, APR_INT64_T_FMT, 
                          sizeof(APR_INT64_T_FMT) - 2) == 0)) {
                 /* Need to account for trailing 'd' and null in sizeof() */
@@ -830,7 +825,12 @@
                 fmt++;
             }
             else if (*fmt == 'l') {
-                var_type = IS_LONG;
+        if (fmt[1] == 'l') { // %ll[du] is always quad on 64- & 32-bit
+            var_type = IS_QUAD;
+            fmt++;
+        } else if (sizeof(APR_INT64_T_FMT) == 3) { // %l[du] is quad on 64-bit
+            var_type = IS_QUAD;
+        } else var_type = IS_LONG;
                 fmt++;
             }
             else if (*fmt == 'h') {

-- 
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.

---------------------------------------------------------------------
To unsubscribe, e-mail: bugs-unsubscribe@apr.apache.org
For additional commands, e-mail: bugs-help@apr.apache.org


Mime
View raw message