commit 33711baebe0cdd2f3b19f53671e9dd283e22e5df
Author: Jim Meyering <meyering(a)redhat.com>
Date: Wed Jun 15 19:36:25 2011 +0200
maint: simplify the attribute-handling code
* rest.c (proxy_put_attr): Simplify/unify the malloc/realloc cases,
as we consider how to handle zero-length attributes.
rest.c | 27 +++++++++++----------------
1 files changed, 11 insertions(+), 16 deletions(-)
---
diff --git a/rest.c b/rest.c
index 7a93d49..11b2971 100644
--- a/rest.c
+++ b/rest.c
@@ -659,22 +659,17 @@ proxy_put_attr (void *cctx, struct MHD_Connection *conn, const char
*url,
}
}
else if (*data_size) {
- if (ms->pipe.data_len) {
- ms->pipe.data_len += *data_size;
- char *p = realloc(ms->pipe.data_ptr,ms->pipe.data_len);
- if (!p) {
- return MHD_NO;
- }
- ms->pipe.data_ptr = p;
- }
- else {
- ms->pipe.data_len = *data_size + 1;
- ms->pipe.data_ptr = malloc(ms->pipe.data_len);
- if (!ms->pipe.data_ptr) {
- return MHD_NO;
- }
- ((char *)ms->pipe.data_ptr)[0] = '\0';
- }
+ /* For the first buffer (when ms->pipe.data_len == 0),
+ add room for a trailing NUL byte. */
+ bool first = ms->pipe.data_len == 0;
+ ms->pipe.data_len += *data_size + first;
+ char *p = realloc(ms->pipe.data_ptr,ms->pipe.data_len);
+ if (!p)
+ return MHD_NO;
+ if (first)
+ *p = '\0';
+ ms->pipe.data_ptr = p;
+
(void)strncat(ms->pipe.data_ptr,data,*data_size);
/* TBD: check return value */
*data_size = 0;
Show replies by date