1 |
jpp |
1.1 |
diff -up sudo-1.8.6p3/common/lbuf.c.lbufexpandcode sudo-1.8.6p3/common/lbuf.c |
2 |
|
|
--- sudo-1.8.6p3/common/lbuf.c.lbufexpandcode 2013-08-12 17:28:52.429562473 +0200 |
3 |
|
|
+++ sudo-1.8.6p3/common/lbuf.c 2013-08-12 17:29:21.486668465 +0200 |
4 |
|
|
@@ -77,6 +77,17 @@ lbuf_destroy(struct lbuf *lbuf) |
5 |
|
|
debug_return; |
6 |
|
|
} |
7 |
|
|
|
8 |
|
|
+static void |
9 |
|
|
+lbuf_expand(struct lbuf *lbuf, size_t extra) |
10 |
|
|
+{ |
11 |
|
|
+ if (lbuf->len + extra + 1 >= lbuf->size) { |
12 |
|
|
+ do { |
13 |
|
|
+ lbuf->size += 256; |
14 |
|
|
+ } while (lbuf->len + extra + 1 >= lbuf->size); |
15 |
|
|
+ lbuf->buf = erealloc(lbuf->buf, lbuf->size); |
16 |
|
|
+ } |
17 |
|
|
+} |
18 |
|
|
+ |
19 |
|
|
/* |
20 |
|
|
* Parse the format and append strings, only %s and %% escapes are supported. |
21 |
|
|
* Any characters in set are quoted with a backslash. |
22 |
|
|
@@ -86,47 +97,40 @@ lbuf_append_quoted(struct lbuf *lbuf, co |
23 |
|
|
{ |
24 |
|
|
va_list ap; |
25 |
|
|
int len; |
26 |
|
|
- char *cp, *s = NULL; |
27 |
|
|
+ char *cp, *s; |
28 |
|
|
debug_decl(lbuf_append_quoted, SUDO_DEBUG_UTIL) |
29 |
|
|
|
30 |
|
|
va_start(ap, fmt); |
31 |
|
|
while (*fmt != '\0') { |
32 |
|
|
- len = 1; |
33 |
|
|
if (fmt[0] == '%' && fmt[1] == 's') { |
34 |
|
|
- s = va_arg(ap, char *); |
35 |
|
|
- len = strlen(s); |
36 |
|
|
- } |
37 |
|
|
- /* Assume worst case that all chars must be escaped. */ |
38 |
|
|
- if (lbuf->len + (len * 2) + 1 >= lbuf->size) { |
39 |
|
|
- do { |
40 |
|
|
- lbuf->size += 256; |
41 |
|
|
- } while (lbuf->len + len + 1 >= lbuf->size); |
42 |
|
|
- lbuf->buf = erealloc(lbuf->buf, lbuf->size); |
43 |
|
|
- } |
44 |
|
|
- if (*fmt == '%') { |
45 |
|
|
- if (*(++fmt) == 's') { |
46 |
|
|
- while ((cp = strpbrk(s, set)) != NULL) { |
47 |
|
|
- len = (int)(cp - s); |
48 |
|
|
- memcpy(lbuf->buf + lbuf->len, s, len); |
49 |
|
|
- lbuf->len += len; |
50 |
|
|
- lbuf->buf[lbuf->len++] = '\\'; |
51 |
|
|
- lbuf->buf[lbuf->len++] = *cp; |
52 |
|
|
- s = cp + 1; |
53 |
|
|
- } |
54 |
|
|
- if (*s != '\0') { |
55 |
|
|
- len = strlen(s); |
56 |
|
|
- memcpy(lbuf->buf + lbuf->len, s, len); |
57 |
|
|
- lbuf->len += len; |
58 |
|
|
- } |
59 |
|
|
- fmt++; |
60 |
|
|
- continue; |
61 |
|
|
+ if ((s = va_arg(ap, char *)) == NULL) |
62 |
|
|
+ goto done; |
63 |
|
|
+ while ((cp = strpbrk(s, set)) != NULL) { |
64 |
|
|
+ len = (int)(cp - s); |
65 |
|
|
+ lbuf_expand(lbuf, len + 2); |
66 |
|
|
+ memcpy(lbuf->buf + lbuf->len, s, len); |
67 |
|
|
+ lbuf->len += len; |
68 |
|
|
+ lbuf->buf[lbuf->len++] = '\\'; |
69 |
|
|
+ lbuf->buf[lbuf->len++] = *cp; |
70 |
|
|
+ s = cp + 1; |
71 |
|
|
} |
72 |
|
|
+ if (*s != '\0') { |
73 |
|
|
+ len = strlen(s); |
74 |
|
|
+ lbuf_expand(lbuf, len); |
75 |
|
|
+ memcpy(lbuf->buf + lbuf->len, s, len); |
76 |
|
|
+ lbuf->len += len; |
77 |
|
|
+ } |
78 |
|
|
+ fmt += 2; |
79 |
|
|
+ continue; |
80 |
|
|
} |
81 |
|
|
+ lbuf_expand(lbuf, 2); |
82 |
|
|
if (strchr(set, *fmt) != NULL) |
83 |
|
|
lbuf->buf[lbuf->len++] = '\\'; |
84 |
|
|
lbuf->buf[lbuf->len++] = *fmt++; |
85 |
|
|
} |
86 |
|
|
- lbuf->buf[lbuf->len] = '\0'; |
87 |
|
|
+done: |
88 |
|
|
+ if (lbuf->size != 0) |
89 |
|
|
+ lbuf->buf[lbuf->len] = '\0'; |
90 |
|
|
va_end(ap); |
91 |
|
|
|
92 |
|
|
debug_return; |
93 |
|
|
@@ -140,33 +144,27 @@ lbuf_append(struct lbuf *lbuf, const cha |
94 |
|
|
{ |
95 |
|
|
va_list ap; |
96 |
|
|
int len; |
97 |
|
|
- char *s = NULL; |
98 |
|
|
+ char *s; |
99 |
|
|
debug_decl(lbuf_append, SUDO_DEBUG_UTIL) |
100 |
|
|
|
101 |
|
|
va_start(ap, fmt); |
102 |
|
|
while (*fmt != '\0') { |
103 |
|
|
- len = 1; |
104 |
|
|
if (fmt[0] == '%' && fmt[1] == 's') { |
105 |
|
|
- s = va_arg(ap, char *); |
106 |
|
|
+ if ((s = va_arg(ap, char *)) == NULL) |
107 |
|
|
+ goto done; |
108 |
|
|
len = strlen(s); |
109 |
|
|
+ lbuf_expand(lbuf, len); |
110 |
|
|
+ memcpy(lbuf->buf + lbuf->len, s, len); |
111 |
|
|
+ lbuf->len += len; |
112 |
|
|
+ fmt += 2; |
113 |
|
|
+ continue; |
114 |
|
|
} |
115 |
|
|
- if (lbuf->len + len + 1 >= lbuf->size) { |
116 |
|
|
- do { |
117 |
|
|
- lbuf->size += 256; |
118 |
|
|
- } while (lbuf->len + len + 1 >= lbuf->size); |
119 |
|
|
- lbuf->buf = erealloc(lbuf->buf, lbuf->size); |
120 |
|
|
- } |
121 |
|
|
- if (*fmt == '%') { |
122 |
|
|
- if (*(++fmt) == 's') { |
123 |
|
|
- memcpy(lbuf->buf + lbuf->len, s, len); |
124 |
|
|
- lbuf->len += len; |
125 |
|
|
- fmt++; |
126 |
|
|
- continue; |
127 |
|
|
- } |
128 |
|
|
- } |
129 |
|
|
+ lbuf_expand(lbuf, 1); |
130 |
|
|
lbuf->buf[lbuf->len++] = *fmt++; |
131 |
|
|
} |
132 |
|
|
- lbuf->buf[lbuf->len] = '\0'; |
133 |
|
|
+done: |
134 |
|
|
+ if (lbuf->size != 0) |
135 |
|
|
+ lbuf->buf[lbuf->len] = '\0'; |
136 |
|
|
va_end(ap); |
137 |
|
|
|
138 |
|
|
debug_return; |