142 |
142 |
143 int cx_vsprintf_a(CxAllocator *alloc, char **str, size_t len, const char *fmt, va_list ap) { |
143 int cx_vsprintf_a(CxAllocator *alloc, char **str, size_t len, const char *fmt, va_list ap) { |
144 va_list ap2; |
144 va_list ap2; |
145 va_copy(ap2, ap); |
145 va_copy(ap2, ap); |
146 int ret = vsnprintf(*str, len, fmt, ap); |
146 int ret = vsnprintf(*str, len, fmt, ap); |
147 if (ret < 0 || ((unsigned)ret) < len) { |
147 if ((unsigned) ret >= len) { |
148 va_end(ap2); |
|
149 return ret; |
|
150 } else { |
|
151 unsigned newlen = ret + 1; |
148 unsigned newlen = ret + 1; |
152 char *ptr = cxRealloc(alloc, *str, newlen); |
149 char *ptr = cxRealloc(alloc, *str, newlen); |
153 if (ptr) { |
150 if (ptr) { |
154 int newret = vsnprintf(ptr, newlen, fmt, ap2); |
151 int newret = vsnprintf(ptr, newlen, fmt, ap2); |
155 va_end(ap2); |
|
156 if (newret < 0) { |
152 if (newret < 0) { |
157 cxFree(alloc, ptr); |
153 cxFree(alloc, ptr); |
158 return ret; |
|
159 } else { |
154 } else { |
160 *str = ptr; |
155 *str = ptr; |
161 return newret; |
156 ret = newret; |
162 } |
157 } |
163 } else { |
|
164 va_end(ap2); |
|
165 return ret; |
|
166 } |
158 } |
167 } |
159 } |
|
160 va_end(ap2); |
|
161 return ret; |
168 } |
162 } |
169 |
163 |
170 int cx_sprintf_sa(CxAllocator *alloc, char *buf, size_t len, char **str, const char *fmt, ... ) { |
164 int cx_sprintf_sa(CxAllocator *alloc, char *buf, size_t len, char **str, const char *fmt, ... ) { |
171 va_list ap; |
165 va_list ap; |
172 va_start(ap, fmt); |
166 va_start(ap, fmt); |
178 int cx_vsprintf_sa(CxAllocator *alloc, char *buf, size_t len, char **str, const char *fmt, va_list ap) { |
172 int cx_vsprintf_sa(CxAllocator *alloc, char *buf, size_t len, char **str, const char *fmt, va_list ap) { |
179 va_list ap2; |
173 va_list ap2; |
180 va_copy(ap2, ap); |
174 va_copy(ap2, ap); |
181 int ret = vsnprintf(buf, len, fmt, ap); |
175 int ret = vsnprintf(buf, len, fmt, ap); |
182 *str = buf; |
176 *str = buf; |
183 if (ret < 0 || ((unsigned)ret) < len) { |
177 if ((unsigned) ret >= len) { |
184 va_end(ap2); |
|
185 return ret; |
|
186 } else { |
|
187 unsigned newlen = ret + 1; |
178 unsigned newlen = ret + 1; |
188 char *ptr = cxMalloc(alloc, newlen); |
179 char *ptr = cxMalloc(alloc, newlen); |
189 if (ptr) { |
180 if (ptr) { |
190 int newret = vsnprintf(ptr, newlen, fmt, ap2); |
181 int newret = vsnprintf(ptr, newlen, fmt, ap2); |
191 va_end(ap2); |
|
192 if (newret < 0) { |
182 if (newret < 0) { |
193 cxFree(alloc, ptr); |
183 cxFree(alloc, ptr); |
194 return ret; |
|
195 } else { |
184 } else { |
196 *str = ptr; |
185 *str = ptr; |
197 return newret; |
186 ret = newret; |
198 } |
187 } |
199 } else { |
|
200 va_end(ap2); |
|
201 return ret; |
|
202 } |
188 } |
203 } |
189 } |
|
190 va_end(ap2); |
|
191 return ret; |
204 } |
192 } |