]> granicus.if.org Git - php/commitdiff
- Fixed a couple of bugs in the new smart_str macros, and allow them to
authorDerick Rethans <derick@php.net>
Tue, 16 Aug 2005 18:02:41 +0000 (18:02 +0000)
committerDerick Rethans <derick@php.net>
Tue, 16 Aug 2005 18:02:41 +0000 (18:02 +0000)
  allocate two extra bytes (so that we can pad them with two \0's for UTF-16)
- Fixed usage of smart_str's in the PAD and INS_STRING macros.

ext/standard/php_smart_str.h
main/spprintf.c

index c6d0dbbbaf4ae902cb72e4eca49d833040a9a9e9..63ee00a3b2a1fdb481392f52b13b0f0bc0cc4ebe 100644 (file)
 #include <zend.h>
 #endif
 
+/* It's safe to write to "one behind the length" as we allocate two extra bytes
+ * to allow for Unicode trailers */
 #define smart_str_0(x) do {                                                                                    \
        if ((x)->c) {                                                                                                   \
                (x)->c[(x)->len] = '\0';                                                                        \
+               (x)->c[(x)->len + 1] = '\0';                                                            \
        }                                                                                                                               \
 } while (0)
 
@@ -49,7 +52,7 @@
 #endif
 
 #define SMART_STR_DO_REALLOC(d, what) \
-       (d)->c = SMART_STR_REALLOC((d)->c, (d)->a + 1, (what))
+       (d)->c = SMART_STR_REALLOC((d)->c, (d)->a + 2, (what))
 
 #define smart_str_alloc4(d, n, what, newlen) do {                                      \
        if (!(d)->c) {                                                                                                  \
 #define smart_str_appends(dest, src) \
        smart_str_appendl((dest), (src), strlen(src))
 
-/* normall character appending */
+/* normal character appending */
 #define smart_str_appendc(dest, c) \
        smart_str_appendc_ex((dest), (c), 0)
 
 /* appending of a single UTF-16 code unit (2 byte)*/
-#define smart_str_append2c(dest, c) while (0) {        \
+#define smart_str_append2c(dest, c) do {       \
        smart_str_appendc_ex((dest), (c&0xFF), 0);      \
        smart_str_appendc_ex((dest), (c&0xFF00 ? c>>8 : '\0'), 0);      \
-}
+} while (0)
 
 #define smart_str_free(s) \
        smart_str_free_ex((s), 0)
index 4d22e968ccc08c438a20e432928d121b545d8ea5..4d51b80ecde1f8f1749398c4b62bb9d208b8b0f4 100644 (file)
        }                                                                                               \
 } while (0)
 
-#define INS_STRING(unicode, s_uni, xbuf, s, slen)      \
+#define INS_STRING(unicode, s_unicode, xbuf, s, s_len) \
 do {                                                                                           \
        if (unicode) {                                                                  \
-               size_t newlen, p, sz = 2*(slen);                        \
+               size_t newlen, p, sz = 2*(s_len);                       \
                smart_str_alloc(xbuf, (sz), 0);                         \
-               if (s_uni) {                                                            \
+               if (s_unicode) {                                                        \
                        memcpy(xbuf->c + xbuf->len, s, (sz));   \
                } else {                                                                        \
-                       p = (slen);                                                             \
+                       p = (s_len);                                                    \
                        while(p--) {                            \
-                               smart_str_append2c(xbuf, *s++);         \
+                               smart_str_append2c(xbuf, *s);           \
+                               *s++;                                                           \
                        }                                                                               \
                }                                                                                       \
-               xbuf->len += (sz);                                                      \
        } else {                                                                                \
-               smart_str_appendl(xbuf, s, slen);                       \
+               smart_str_appendl(xbuf, s, s_len);                      \
        }                                                                                               \
 } while (0)
        
@@ -163,8 +163,8 @@ do {                                                                                                \
                } else {                                                                        \
                        smart_str_alloc(xbuf, sz, 0);                   \
                        memset(xbuf->c + xbuf->len, ch, sz);    \
+                       xbuf->len += sz;                                                \
                }                                                                                       \
-               xbuf->len += sz;                                                        \
        }                                                                                               \
 } while (0)
 
@@ -847,7 +847,6 @@ PHPAPI int vuspprintf(char **pbuf, size_t max_len, const char *format, va_list a
        if (max_len && xbuf.len > max_len) {
                xbuf.len = max_len;
        }
-       smart_str_appendc(&xbuf, '\0'); /* we need \0\0 as termination */
        smart_str_0(&xbuf);
                
        *pbuf = xbuf.c;