diff --git a/src/core/channel/call_op_string.c b/src/core/channel/call_op_string.c index 789913901ad..d36d51e7896 100644 --- a/src/core/channel/call_op_string.c +++ b/src/core/channel/call_op_string.c @@ -41,110 +41,86 @@ #include #include -#define MAX_APPEND 1024 +static void put_metadata(gpr_strvec *b, grpc_mdelem *md) { + gpr_strvec_add(b, gpr_strdup(" key=")); + gpr_strvec_add(b, gpr_hexdump((char *)GPR_SLICE_START_PTR(md->key->slice), + GPR_SLICE_LENGTH(md->key->slice), GPR_HEXDUMP_PLAINTEXT)); -typedef struct { - size_t cap; - size_t len; - char *buffer; -} buf; - -static void bprintf(buf *b, const char *fmt, ...) { - va_list arg; - if (b->len + MAX_APPEND > b->cap) { - b->cap = GPR_MAX(b->len + MAX_APPEND, b->cap * 3 / 2); - b->buffer = gpr_realloc(b->buffer, b->cap); - } - va_start(arg, fmt); - b->len += vsprintf(b->buffer + b->len, fmt, arg); - va_end(arg); -} - -static void bputs(buf *b, const char *s) { - size_t slen = strlen(s); - if (b->len + slen + 1 > b->cap) { - b->cap = GPR_MAX(b->len + slen + 1, b->cap * 3 / 2); - b->buffer = gpr_realloc(b->buffer, b->cap); - } - strcat(b->buffer, s); - b->len += slen; -} - -static void put_metadata(buf *b, grpc_mdelem *md) { - char *txt; - - txt = gpr_hexdump((char *)GPR_SLICE_START_PTR(md->key->slice), - GPR_SLICE_LENGTH(md->key->slice), GPR_HEXDUMP_PLAINTEXT); - bputs(b, " key="); - bputs(b, txt); - gpr_free(txt); - - txt = gpr_hexdump((char *)GPR_SLICE_START_PTR(md->value->slice), - GPR_SLICE_LENGTH(md->value->slice), GPR_HEXDUMP_PLAINTEXT); - bputs(b, " value="); - bputs(b, txt); - gpr_free(txt); + gpr_strvec_add(b, gpr_strdup(" value=")); + gpr_strvec_add(b, gpr_hexdump((char *)GPR_SLICE_START_PTR(md->value->slice), + GPR_SLICE_LENGTH(md->value->slice), GPR_HEXDUMP_PLAINTEXT)); } char *grpc_call_op_string(grpc_call_op *op) { - buf b = {0, 0, 0}; + char *tmp; + char *out; + + gpr_strvec b; + gpr_strvec_init(&b); switch (op->dir) { case GRPC_CALL_DOWN: - bprintf(&b, ">"); + gpr_strvec_add(&b, gpr_strdup(">")); break; case GRPC_CALL_UP: - bprintf(&b, "<"); + gpr_strvec_add(&b, gpr_strdup("<")); break; } switch (op->type) { case GRPC_SEND_METADATA: - bprintf(&b, "SEND_METADATA"); + gpr_strvec_add(&b, gpr_strdup("SEND_METADATA")); put_metadata(&b, op->data.metadata); break; case GRPC_SEND_DEADLINE: - bprintf(&b, "SEND_DEADLINE %d.%09d", op->data.deadline.tv_sec, + gpr_asprintf(&tmp, "SEND_DEADLINE %d.%09d", op->data.deadline.tv_sec, op->data.deadline.tv_nsec); + gpr_strvec_add(&b, tmp); break; case GRPC_SEND_START: - bprintf(&b, "SEND_START pollset=%p", op->data.start.pollset); + gpr_asprintf(&tmp, "SEND_START pollset=%p", op->data.start.pollset); + gpr_strvec_add(&b, tmp); break; case GRPC_SEND_MESSAGE: - bprintf(&b, "SEND_MESSAGE"); + gpr_strvec_add(&b, gpr_strdup("SEND_MESSAGE")); break; case GRPC_SEND_FINISH: - bprintf(&b, "SEND_FINISH"); + gpr_strvec_add(&b, gpr_strdup("SEND_FINISH")); break; case GRPC_REQUEST_DATA: - bprintf(&b, "REQUEST_DATA"); + gpr_strvec_add(&b, gpr_strdup("REQUEST_DATA")); break; case GRPC_RECV_METADATA: - bprintf(&b, "RECV_METADATA"); + gpr_strvec_add(&b, gpr_strdup("RECV_METADATA")); put_metadata(&b, op->data.metadata); break; case GRPC_RECV_DEADLINE: - bprintf(&b, "RECV_DEADLINE %d.%09d", op->data.deadline.tv_sec, + gpr_asprintf(&tmp, "RECV_DEADLINE %d.%09d", op->data.deadline.tv_sec, op->data.deadline.tv_nsec); + gpr_strvec_add(&b, tmp); break; case GRPC_RECV_END_OF_INITIAL_METADATA: - bprintf(&b, "RECV_END_OF_INITIAL_METADATA"); + gpr_strvec_add(&b, gpr_strdup("RECV_END_OF_INITIAL_METADATA")); break; case GRPC_RECV_MESSAGE: - bprintf(&b, "RECV_MESSAGE"); + gpr_strvec_add(&b, gpr_strdup("RECV_MESSAGE")); break; case GRPC_RECV_HALF_CLOSE: - bprintf(&b, "RECV_HALF_CLOSE"); + gpr_strvec_add(&b, gpr_strdup("RECV_HALF_CLOSE")); break; case GRPC_RECV_FINISH: - bprintf(&b, "RECV_FINISH"); + gpr_strvec_add(&b, gpr_strdup("RECV_FINISH")); break; case GRPC_CANCEL_OP: - bprintf(&b, "CANCEL_OP"); + gpr_strvec_add(&b, gpr_strdup("CANCEL_OP")); break; } - bprintf(&b, " flags=0x%08x", op->flags); + gpr_asprintf(&tmp, " flags=0x%08x", op->flags); + gpr_strvec_add(&b, tmp); + + out = gpr_strvec_flatten(&b, NULL); + gpr_strvec_destroy(&b); - return b.buffer; + return out; } void grpc_call_log_op(char *file, int line, gpr_log_severity severity, diff --git a/src/core/channel/connected_channel.c b/src/core/channel/connected_channel.c index 47c0ed3b882..55486ed5c34 100644 --- a/src/core/channel/connected_channel.c +++ b/src/core/channel/connected_channel.c @@ -450,6 +450,7 @@ static void recv_batch(void *user_data, grpc_transport *transport, (int)calld->incoming_message.length, (int)calld->incoming_message_length); recv_error(chand, calld, __LINE__, message); + gpr_free(message); } call_op.type = GRPC_RECV_HALF_CLOSE; call_op.dir = GRPC_CALL_UP; diff --git a/src/core/httpcli/format_request.c b/src/core/httpcli/format_request.c index 7a44f1266f1..58bb7c740ea 100644 --- a/src/core/httpcli/format_request.c +++ b/src/core/httpcli/format_request.c @@ -37,67 +37,57 @@ #include #include +#include "src/core/support/string.h" #include #include #include -typedef struct { - size_t length; - size_t capacity; - char *data; -} sbuf; - -static void sbuf_append(sbuf *buf, const char *bytes, size_t len) { - if (buf->length + len > buf->capacity) { - buf->capacity = GPR_MAX(buf->length + len, buf->capacity * 3 / 2); - buf->data = gpr_realloc(buf->data, buf->capacity); - } - memcpy(buf->data + buf->length, bytes, len); - buf->length += len; -} - -static void sbprintf(sbuf *buf, const char *fmt, ...) { - char temp[GRPC_HTTPCLI_MAX_HEADER_LENGTH]; - size_t len; - va_list args; - - va_start(args, fmt); - len = vsprintf(temp, fmt, args); - va_end(args); - - sbuf_append(buf, temp, len); -} - -static void fill_common_header(const grpc_httpcli_request *request, sbuf *buf) { +static void fill_common_header(const grpc_httpcli_request *request, gpr_strvec *buf) { size_t i; - sbprintf(buf, "%s HTTP/1.0\r\n", request->path); + gpr_strvec_add(buf, gpr_strdup(request->path)); + gpr_strvec_add(buf, gpr_strdup(" HTTP/1.0\r\n")); /* just in case some crazy server really expects HTTP/1.1 */ - sbprintf(buf, "Host: %s\r\n", request->host); - sbprintf(buf, "Connection: close\r\n"); - sbprintf(buf, "User-Agent: %s\r\n", GRPC_HTTPCLI_USER_AGENT); + gpr_strvec_add(buf, gpr_strdup("Host: ")); + gpr_strvec_add(buf, gpr_strdup(request->host)); + gpr_strvec_add(buf, gpr_strdup("\r\n")); + gpr_strvec_add(buf, gpr_strdup("Connection: close\r\n")); + gpr_strvec_add(buf, gpr_strdup("User-Agent: "GRPC_HTTPCLI_USER_AGENT"\r\n")); /* user supplied headers */ for (i = 0; i < request->hdr_count; i++) { - sbprintf(buf, "%s: %s\r\n", request->hdrs[i].key, request->hdrs[i].value); + gpr_strvec_add(buf, gpr_strdup(request->hdrs[i].key)); + gpr_strvec_add(buf, gpr_strdup(": ")); + gpr_strvec_add(buf, gpr_strdup(request->hdrs[i].value)); + gpr_strvec_add(buf, gpr_strdup("\r\n")); } } gpr_slice grpc_httpcli_format_get_request(const grpc_httpcli_request *request) { - sbuf out = {0, 0, NULL}; + gpr_strvec out; + char *flat; + size_t flat_len; - sbprintf(&out, "GET "); + gpr_strvec_init(&out); + gpr_strvec_add(&out, gpr_strdup("GET ")); fill_common_header(request, &out); - sbprintf(&out, "\r\n"); + gpr_strvec_add(&out, gpr_strdup("\r\n")); - return gpr_slice_new(out.data, out.length, gpr_free); + flat = gpr_strvec_flatten(&out, &flat_len); + gpr_strvec_destroy(&out); + + return gpr_slice_new(flat, flat_len, gpr_free); } gpr_slice grpc_httpcli_format_post_request(const grpc_httpcli_request *request, const char *body_bytes, size_t body_size) { - sbuf out = {0, 0, NULL}; + gpr_strvec out; + char *tmp; + size_t out_len; size_t i; - sbprintf(&out, "POST "); + gpr_strvec_init(&out); + + gpr_strvec_add(&out, gpr_strdup("POST ")); fill_common_header(request, &out); if (body_bytes) { gpr_uint8 has_content_type = 0; @@ -108,14 +98,18 @@ gpr_slice grpc_httpcli_format_post_request(const grpc_httpcli_request *request, } } if (!has_content_type) { - sbprintf(&out, "Content-Type: text/plain\r\n"); + gpr_strvec_add(&out, gpr_strdup("Content-Type: text/plain\r\n")); } - sbprintf(&out, "Content-Length: %lu\r\n", (unsigned long)body_size); + gpr_asprintf(&tmp, "Content-Length: %lu\r\n", (unsigned long)body_size); + gpr_strvec_add(&out, tmp); } - sbprintf(&out, "\r\n"); + gpr_strvec_add(&out, gpr_strdup("\r\n")); + tmp = gpr_strvec_flatten(&out, &out_len); if (body_bytes) { - sbuf_append(&out, body_bytes, body_size); + tmp = gpr_realloc(tmp, out_len + body_size); + memcpy(tmp + out_len, body_bytes, body_size); + out_len += body_size; } - return gpr_slice_new(out.data, out.length, gpr_free); + return gpr_slice_new(tmp, out_len, gpr_free); } diff --git a/src/core/support/string.c b/src/core/support/string.c index 9b5cac75966..97bce60f94c 100644 --- a/src/core/support/string.c +++ b/src/core/support/string.c @@ -14,7 +14,7 @@ * in the documentation and/or other materials provided with the * distribution. * * Neither the name of Google Inc. nor the names of its - * contributors may be used to endorse or promote products derived from + * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS @@ -152,3 +152,49 @@ int gpr_ltoa(long value, char *string) { string[i] = 0; return i; } + +char *gpr_strjoin(const char **strs, size_t nstrs, size_t *final_length) { + size_t out_length = 0; + size_t i; + char *out; + for (i = 0; i < nstrs; i++) { + out_length += strlen(strs[i]); + } + out_length += 1; /* null terminator */ + out = gpr_malloc(out_length); + out_length = 0; + for (i = 0; i < nstrs; i++) { + size_t slen = strlen(strs[i]); + memcpy(out + out_length, strs[i], slen); + out_length += slen; + } + out[out_length] = 0; + if (final_length != NULL) { + *final_length = out_length; + } + return out; +} + +void gpr_strvec_init(gpr_strvec *sv) { + memset(sv, 0, sizeof(*sv)); +} + +void gpr_strvec_destroy(gpr_strvec *sv) { + size_t i; + for (i = 0; i < sv->count; i++) { + gpr_free(sv->strs[i]); + } + gpr_free(sv->strs); +} + +void gpr_strvec_add(gpr_strvec *sv, char *str) { + if (sv->count == sv->capacity) { + sv->capacity = GPR_MAX(sv->capacity + 8, sv->capacity * 2); + sv->strs = gpr_realloc(sv->strs, sizeof(char*) * sv->capacity); + } + sv->strs[sv->count++] = str; +} + +char *gpr_strvec_flatten(gpr_strvec *sv, size_t *final_length) { + return gpr_strjoin((const char**)sv->strs, sv->count, final_length); +} diff --git a/src/core/support/string.h b/src/core/support/string.h index 28b7029ecd6..64e06d3b6aa 100644 --- a/src/core/support/string.h +++ b/src/core/support/string.h @@ -81,6 +81,27 @@ void gpr_reverse_bytes(char *str, int len); the result is undefined. */ int gpr_asprintf(char **strp, const char *format, ...); +/* Join a set of strings, returning the resulting string. + Total combined length (excluding null terminator) is returned in total_length + if it is non-null. */ +char *gpr_strjoin(const char **strs, size_t nstrs, size_t *total_length); + +/* A vector of strings... for building up a final string one piece at a time */ +typedef struct { + char **strs; + size_t count; + size_t capacity; +} gpr_strvec; + +/* Initialize/destroy */ +void gpr_strvec_init(gpr_strvec *strs); +void gpr_strvec_destroy(gpr_strvec *strs); +/* Add a string to a strvec, takes ownership of the string */ +void gpr_strvec_add(gpr_strvec *strs, char *add); +/* Return a joined string with all added substrings, optionally setting + total_length as per gpr_strjoin */ +char *gpr_strvec_flatten(gpr_strvec *strs, size_t *total_length); + #ifdef __cplusplus } #endif diff --git a/src/core/surface/event_string.c b/src/core/surface/event_string.c index e38ef06c9ff..8975d312eec 100644 --- a/src/core/surface/event_string.c +++ b/src/core/surface/event_string.c @@ -38,8 +38,10 @@ #include "src/core/support/string.h" #include -static size_t addhdr(char *p, grpc_event *ev) { - return sprintf(p, "tag:%p call:%p", ev->tag, (void *)ev->call); +static void addhdr(gpr_strvec *buf, grpc_event *ev) { + char *tmp; + gpr_asprintf(&tmp, "tag:%p call:%p", ev->tag, (void *)ev->call); + gpr_strvec_add(buf, tmp); } static const char *errstr(grpc_op_error err) { @@ -52,72 +54,84 @@ static const char *errstr(grpc_op_error err) { return "UNKNOWN_UNKNOWN"; } -static size_t adderr(char *p, grpc_op_error err) { - return sprintf(p, " err=%s", errstr(err)); +static void adderr(gpr_strvec *buf, grpc_op_error err) { + char *tmp; + gpr_asprintf(&tmp, " err=%s", errstr(err)); + gpr_strvec_add(buf, tmp); } char *grpc_event_string(grpc_event *ev) { - char buffer[1024]; - char *p = buffer; + char *out; + char *tmp; + gpr_strvec buf; if (ev == NULL) return gpr_strdup("null"); + gpr_strvec_init(&buf); + switch (ev->type) { case GRPC_SERVER_SHUTDOWN: - p += sprintf(p, "SERVER_SHUTDOWN"); + gpr_strvec_add(&buf, gpr_strdup("SERVER_SHUTDOWN")); break; case GRPC_QUEUE_SHUTDOWN: - p += sprintf(p, "QUEUE_SHUTDOWN"); + gpr_strvec_add(&buf, gpr_strdup("QUEUE_SHUTDOWN")); break; case GRPC_READ: - p += sprintf(p, "READ: "); - p += addhdr(p, ev); + gpr_strvec_add(&buf, gpr_strdup("READ: ")); + addhdr(&buf, ev); if (ev->data.read) { - p += sprintf(p, " %d bytes", + gpr_asprintf(&tmp, " %d bytes", (int)grpc_byte_buffer_length(ev->data.read)); + gpr_strvec_add(&buf, tmp); } else { - p += sprintf(p, " end-of-stream"); + gpr_strvec_add(&buf, gpr_strdup(" end-of-stream")); } break; case GRPC_INVOKE_ACCEPTED: - p += sprintf(p, "INVOKE_ACCEPTED: "); - p += addhdr(p, ev); - p += adderr(p, ev->data.invoke_accepted); + gpr_strvec_add(&buf, gpr_strdup("INVOKE_ACCEPTED: ")); + addhdr(&buf, ev); + adderr(&buf, ev->data.invoke_accepted); break; case GRPC_WRITE_ACCEPTED: - p += sprintf(p, "WRITE_ACCEPTED: "); - p += addhdr(p, ev); - p += adderr(p, ev->data.write_accepted); + gpr_strvec_add(&buf, gpr_strdup("WRITE_ACCEPTED: ")); + addhdr(&buf, ev); + adderr(&buf, ev->data.write_accepted); break; case GRPC_FINISH_ACCEPTED: - p += sprintf(p, "FINISH_ACCEPTED: "); - p += addhdr(p, ev); - p += adderr(p, ev->data.write_accepted); + gpr_strvec_add(&buf, gpr_strdup("FINISH_ACCEPTED: ")); + addhdr(&buf, ev); + adderr(&buf, ev->data.write_accepted); break; case GRPC_CLIENT_METADATA_READ: - p += sprintf(p, "CLIENT_METADATA_READ: "); - p += addhdr(p, ev); - p += sprintf(p, " %d elements", (int)ev->data.client_metadata_read.count); + gpr_strvec_add(&buf, gpr_strdup("CLIENT_METADATA_READ: ")); + addhdr(&buf, ev); + gpr_asprintf(&tmp, " %d elements", + (int)ev->data.client_metadata_read.count); + gpr_strvec_add(&buf, tmp); break; case GRPC_FINISHED: - p += sprintf(p, "FINISHED: "); - p += addhdr(p, ev); - p += sprintf(p, " status=%d details='%s' %d metadata elements", + gpr_strvec_add(&buf, gpr_strdup("FINISHED: ")); + addhdr(&buf, ev); + gpr_asprintf(&tmp, " status=%d details='%s' %d metadata elements", ev->data.finished.status, ev->data.finished.details, (int)ev->data.finished.metadata_count); + gpr_strvec_add(&buf, tmp); break; case GRPC_SERVER_RPC_NEW: - p += sprintf(p, "SERVER_RPC_NEW: "); - p += addhdr(p, ev); - p += sprintf(p, " method='%s' host='%s' %d metadata elements", + gpr_strvec_add(&buf, gpr_strdup("SERVER_RPC_NEW: ")); + addhdr(&buf, ev); + gpr_asprintf(&tmp, " method='%s' host='%s' %d metadata elements", ev->data.server_rpc_new.method, ev->data.server_rpc_new.host, (int)ev->data.server_rpc_new.metadata_count); + gpr_strvec_add(&buf, tmp); break; case GRPC_COMPLETION_DO_NOT_USE: - p += sprintf(p, "DO_NOT_USE (this is a bug)"); - p += addhdr(p, ev); + gpr_strvec_add(&buf, gpr_strdup("DO_NOT_USE (this is a bug)")); + addhdr(&buf, ev); break; } - return gpr_strdup(buffer); + out = gpr_strvec_flatten(&buf, NULL); + gpr_strvec_destroy(&buf); + return out; } diff --git a/test/core/channel/metadata_buffer_test.c b/test/core/channel/metadata_buffer_test.c index aa2399272f7..d2bc30cc3c0 100644 --- a/test/core/channel/metadata_buffer_test.c +++ b/test/core/channel/metadata_buffer_test.c @@ -32,6 +32,7 @@ */ #include "src/core/channel/metadata_buffer.h" +#include "src/core/support/string.h" #include #include #include "test/core/util/test_config.h" @@ -42,12 +43,12 @@ /* construct a buffer with some prefix followed by an integer converted to a string */ static gpr_slice construct_buffer(size_t prefix_length, size_t index) { - gpr_slice buffer = gpr_slice_malloc(prefix_length + 32); + gpr_slice buffer = gpr_slice_malloc(prefix_length + GPR_LTOA_MIN_BUFSIZE); memset(GPR_SLICE_START_PTR(buffer), 'a', prefix_length); GPR_SLICE_SET_LENGTH( - buffer, prefix_length + - sprintf((char *)GPR_SLICE_START_PTR(buffer) + prefix_length, - "%d", (int)index)); + buffer, + prefix_length + + gpr_ltoa(index, (char *)GPR_SLICE_START_PTR(buffer) + prefix_length)); return buffer; } diff --git a/test/core/end2end/cq_verifier.c b/test/core/end2end/cq_verifier.c index 49b131c2362..287f83eebca 100644 --- a/test/core/end2end/cq_verifier.c +++ b/test/core/end2end/cq_verifier.c @@ -231,100 +231,91 @@ static void verify_matches(expectation *e, grpc_event *ev) { } } -static char *metadata_expectation_string(metadata *md) { - size_t len; +static void metadata_expectation(gpr_strvec *buf, metadata *md) { size_t i; - char *out; - char *p; - - if (!md) return gpr_strdup("nil"); - - for (len = 0, i = 0; i < md->count; i++) { - len += strlen(md->keys[i]); - len += strlen(md->values[i]); - } - len += 3 + md->count; - - p = out = gpr_malloc(len); - *p++ = '{'; - for (i = 0; i < md->count; i++) { - if (i) *p++ = ','; - p += sprintf(p, "%s:%s", md->keys[i], md->values[i]); + char *tmp; + + if (!md) { + gpr_strvec_add(buf, gpr_strdup("nil")); + } else { + for (i = 0; i < md->count; i++) { + gpr_asprintf(&tmp, "%c%s:%s", i ? ',' : '{', md->keys[i], md->values[i]); + gpr_strvec_add(buf, tmp); + } + gpr_strvec_add(buf, gpr_strdup("}")); } - *p++ = '}'; - *p++ = 0; - return out; } -static size_t expectation_to_string(char *out, expectation *e) { +static void expectation_to_strvec(gpr_strvec *buf, expectation *e) { gpr_timespec timeout; - char *str = NULL; - size_t len; + char *tmp; switch (e->type) { case GRPC_FINISH_ACCEPTED: - return sprintf(out, "GRPC_FINISH_ACCEPTED result=%d", + gpr_asprintf(&tmp, "GRPC_FINISH_ACCEPTED result=%d", e->data.finish_accepted); + gpr_strvec_add(buf, tmp); + break; case GRPC_WRITE_ACCEPTED: - return sprintf(out, "GRPC_WRITE_ACCEPTED result=%d", + gpr_asprintf(&tmp, "GRPC_WRITE_ACCEPTED result=%d", e->data.write_accepted); + gpr_strvec_add(buf, tmp); + break; case GRPC_INVOKE_ACCEPTED: - return sprintf(out, "GRPC_INVOKE_ACCEPTED"); + gpr_strvec_add(buf, gpr_strdup("GRPC_INVOKE_ACCEPTED")); + break; case GRPC_SERVER_RPC_NEW: timeout = gpr_time_sub(e->data.server_rpc_new.deadline, gpr_now()); - return sprintf(out, "GRPC_SERVER_RPC_NEW method=%s host=%s timeout=%fsec", + gpr_asprintf(&tmp, "GRPC_SERVER_RPC_NEW method=%s host=%s timeout=%fsec", e->data.server_rpc_new.method, e->data.server_rpc_new.host, timeout.tv_sec + 1e-9 * timeout.tv_nsec); + gpr_strvec_add(buf, tmp); + break; case GRPC_CLIENT_METADATA_READ: - str = metadata_expectation_string(e->data.client_metadata_read); - len = sprintf(out, "GRPC_CLIENT_METADATA_READ %s", str); - gpr_free(str); - return len; + gpr_strvec_add(buf, gpr_strdup("GRPC_CLIENT_METADATA_READ ")); + metadata_expectation(buf, e->data.client_metadata_read); + break; case GRPC_FINISHED: - str = metadata_expectation_string(e->data.finished.metadata); - len = sprintf(out, "GRPC_FINISHED status=%d details=%s %s", - e->data.finished.status, e->data.finished.details, str); - gpr_free(str); - return len; + gpr_asprintf(&tmp, "GRPC_FINISHED status=%d details=%s ", + e->data.finished.status, e->data.finished.details); + gpr_strvec_add(buf, tmp); + metadata_expectation(buf, e->data.finished.metadata); + break; case GRPC_READ: - if (e->data.read) { - str = - gpr_hexdump((char *)GPR_SLICE_START_PTR(*e->data.read), - GPR_SLICE_LENGTH(*e->data.read), GPR_HEXDUMP_PLAINTEXT); - } - len = sprintf(out, "GRPC_READ data=%s", str); - gpr_free(str); - return len; + gpr_strvec_add(buf, gpr_strdup("GRPC_READ data=")); + gpr_strvec_add(buf, gpr_hexdump((char *)GPR_SLICE_START_PTR(*e->data.read), + GPR_SLICE_LENGTH(*e->data.read), GPR_HEXDUMP_PLAINTEXT)); + break; case GRPC_SERVER_SHUTDOWN: - return sprintf(out, "GRPC_SERVER_SHUTDOWN"); + gpr_strvec_add(buf, gpr_strdup("GRPC_SERVER_SHUTDOWN")); + break; case GRPC_COMPLETION_DO_NOT_USE: case GRPC_QUEUE_SHUTDOWN: gpr_log(GPR_ERROR, "not implemented"); abort(); break; } - return 0; } -static char *expectations_to_string(cq_verifier *v) { - /* allocate a large buffer: we're about to crash anyway */ - char *buffer = gpr_malloc(32 * 1024 * 1024); - char *p = buffer; +static void expectations_to_strvec(gpr_strvec *buf, cq_verifier *v) { expectation *e; for (e = v->expect.next; e != &v->expect; e = e->next) { - p += expectation_to_string(p, e); - *p++ = '\n'; + expectation_to_strvec(buf, e); + gpr_strvec_add(buf, gpr_strdup("\n")); } - - *p = 0; - return buffer; } static void fail_no_event_received(cq_verifier *v) { - char *expectations = expectations_to_string(v); - gpr_log(GPR_ERROR, "no event received, but expected:\n%s", expectations); - gpr_free(expectations); + gpr_strvec buf; + char *msg; + gpr_strvec_init(&buf); + gpr_strvec_add(&buf, gpr_strdup("no event received, but expected:\n")); + expectations_to_strvec(&buf, v); + msg = gpr_strvec_flatten(&buf, NULL); + gpr_log(GPR_ERROR, "%s", msg); + gpr_strvec_destroy(&buf); + gpr_free(msg); abort(); } @@ -333,9 +324,10 @@ void cq_verify(cq_verifier *v) { gpr_time_add(gpr_now(), gpr_time_from_micros(10 * GPR_US_PER_SEC)); grpc_event *ev; expectation *e; + char *s; + gpr_strvec have_tags; - char have_tags[512] = {0}; - char *phave = have_tags; + gpr_strvec_init(&have_tags); while (v->expect.next != &v->expect) { ev = grpc_completion_queue_next(v->cq, deadline); @@ -344,7 +336,8 @@ void cq_verify(cq_verifier *v) { } for (e = v->expect.next; e != &v->expect; e = e->next) { - phave += sprintf(phave, " %p", e->tag); + gpr_asprintf(&s, " %p", e->tag); + gpr_strvec_add(&have_tags, s); if (e->tag == ev->tag) { verify_matches(e, ev); e->next->prev = e->prev; @@ -354,15 +347,20 @@ void cq_verify(cq_verifier *v) { } } if (e == &v->expect) { - char *s = grpc_event_string(ev); + s = grpc_event_string(ev); gpr_log(GPR_ERROR, "event not found: %s", s); - gpr_log(GPR_ERROR, "have tags:%s", have_tags); gpr_free(s); + s = gpr_strvec_flatten(&have_tags, NULL); + gpr_log(GPR_ERROR, "have tags:%s", s); + gpr_free(s); + gpr_strvec_destroy(&have_tags); abort(); } grpc_event_finish(ev); } + + gpr_strvec_destroy(&have_tags); } void cq_verify_empty(cq_verifier *v) { diff --git a/test/core/end2end/tests/census_simple_request.c b/test/core/end2end/tests/census_simple_request.c index 719f0fe6623..86cef437be8 100644 --- a/test/core/end2end/tests/census_simple_request.c +++ b/test/core/end2end/tests/census_simple_request.c @@ -37,6 +37,7 @@ #include #include +#include "src/core/support/string.h" #include #include #include @@ -145,7 +146,7 @@ static void test_body(grpc_end2end_test_fixture f) { static void test_invoke_request_with_census( grpc_end2end_test_config config, const char *name, void (*body)(grpc_end2end_test_fixture f)) { - char fullname[64]; + char *fullname; grpc_end2end_test_fixture f; grpc_arg client_arg, server_arg; grpc_channel_args client_args, server_args; @@ -163,11 +164,12 @@ static void test_invoke_request_with_census( server_args.num_args = 1; server_args.args = &server_arg; - sprintf(fullname, "%s/%s", __FUNCTION__, name); + gpr_asprintf(&fullname, "%s/%s", __FUNCTION__, name); f = begin_test(config, fullname, &client_args, &server_args); body(f); end_test(&f); config.tear_down_data(&f); + gpr_free(fullname); } void grpc_end2end_tests(grpc_end2end_test_config config) { diff --git a/test/core/end2end/tests/simple_request.c b/test/core/end2end/tests/simple_request.c index 23fc201d843..f8894a8ba92 100644 --- a/test/core/end2end/tests/simple_request.c +++ b/test/core/end2end/tests/simple_request.c @@ -37,6 +37,7 @@ #include #include +#include "src/core/support/string.h" #include #include #include @@ -198,15 +199,16 @@ static void simple_request_body2(grpc_end2end_test_fixture f) { static void test_invoke_simple_request( grpc_end2end_test_config config, const char *name, void (*body)(grpc_end2end_test_fixture f)) { - char fullname[64]; + char *fullname; grpc_end2end_test_fixture f; - sprintf(fullname, "%s/%s", __FUNCTION__, name); + gpr_asprintf(&fullname, "%s/%s", __FUNCTION__, name); f = begin_test(config, fullname, NULL, NULL); body(f); end_test(&f); config.tear_down_data(&f); + gpr_free(fullname); } static void test_invoke_10_simple_requests(grpc_end2end_test_config config) { diff --git a/test/core/security/credentials_test.c b/test/core/security/credentials_test.c index ec21e0d42f7..1c83cc80590 100644 --- a/test/core/security/credentials_test.c +++ b/test/core/security/credentials_test.c @@ -498,10 +498,8 @@ static void validate_service_account_http_request( char *expected_body = NULL; GPR_ASSERT(body != NULL); GPR_ASSERT(body_size != 0); - expected_body = gpr_malloc(strlen(expected_service_account_http_body_prefix) + - strlen(test_signed_jwt) + 1); - sprintf(expected_body, "%s%s", expected_service_account_http_body_prefix, - test_signed_jwt); + gpr_asprintf(&expected_body, "%s%s", + expected_service_account_http_body_prefix, test_signed_jwt); GPR_ASSERT(strlen(expected_body) == body_size); GPR_ASSERT(!memcmp(expected_body, body, body_size)); gpr_free(expected_body); diff --git a/test/core/statistics/hash_table_test.c b/test/core/statistics/hash_table_test.c index ebfc2a2a9a0..f8df2574a42 100644 --- a/test/core/statistics/hash_table_test.c +++ b/test/core/statistics/hash_table_test.c @@ -38,6 +38,7 @@ #include "src/core/statistics/hash_table.h" #include "src/core/support/murmur_hash.h" +#include "src/core/support/string.h" #include #include #include @@ -187,15 +188,15 @@ static void test_insertion_and_deletion_with_high_collision_rate(void) { census_ht_option opt = {CENSUS_HT_POINTER, 13, &force_collision, &cmp_str_keys, NULL, NULL}; census_ht* ht = census_ht_create(&opt); - char key_str[1000][10]; + char key_str[1000][GPR_LTOA_MIN_BUFSIZE]; gpr_uint64 val = 0; int i = 0; for (i = 0; i < 1000; i++) { census_ht_key key; key.ptr = key_str[i]; - sprintf(key_str[i], "%d", i); + gpr_ltoa(i, key_str[i]); census_ht_insert(ht, key, (void*)(&val)); - printf("%d\n", i); + gpr_log(GPR_INFO, "%d\n", i); GPR_ASSERT(census_ht_get_size(ht) == (i + 1)); } for (i = 0; i < 1000; i++) { diff --git a/test/core/transport/chttp2/hpack_table_test.c b/test/core/transport/chttp2/hpack_table_test.c index 1576a30c1b0..d155dee9dc2 100644 --- a/test/core/transport/chttp2/hpack_table_test.c +++ b/test/core/transport/chttp2/hpack_table_test.c @@ -36,6 +36,7 @@ #include #include +#include "src/core/support/string.h" #include #include #include "test/core/util/test_config.h" @@ -131,8 +132,8 @@ static void test_static_lookup(void) { static void test_many_additions(void) { grpc_chttp2_hptbl tbl; int i; - char key[32]; - char value[32]; + char *key; + char *value; grpc_mdctx *mdctx; LOG_TEST(); @@ -141,14 +142,18 @@ static void test_many_additions(void) { grpc_chttp2_hptbl_init(&tbl, mdctx); for (i = 0; i < 1000000; i++) { - sprintf(key, "K:%d", i); - sprintf(value, "VALUE:%d", i); + gpr_asprintf(&key, "K:%d", i); + gpr_asprintf(&value, "VALUE:%d", i); grpc_chttp2_hptbl_add(&tbl, grpc_mdelem_from_strings(mdctx, key, value)); assert_index(&tbl, 1 + GRPC_CHTTP2_LAST_STATIC_ENTRY, key, value); + gpr_free(key); + gpr_free(value); if (i) { - sprintf(key, "K:%d", i - 1); - sprintf(value, "VALUE:%d", i - 1); + gpr_asprintf(&key, "K:%d", i - 1); + gpr_asprintf(&value, "VALUE:%d", i - 1); assert_index(&tbl, 2 + GRPC_CHTTP2_LAST_STATIC_ENTRY, key, value); + gpr_free(key); + gpr_free(value); } } @@ -226,7 +231,7 @@ static void test_find(void) { /* overflow the string buffer, check find still works */ for (i = 0; i < 10000; i++) { - sprintf(buffer, "%d", i); + gpr_ltoa(i, buffer); grpc_chttp2_hptbl_add(&tbl, grpc_mdelem_from_strings(mdctx, "test", buffer)); } @@ -245,7 +250,7 @@ static void test_find(void) { for (i = 0; i < tbl.num_ents; i++) { int expect = 9999 - i; - sprintf(buffer, "%d", expect); + gpr_ltoa(expect, buffer); r = find_simple(&tbl, "test", buffer); GPR_ASSERT(r.index == i + 1 + GRPC_CHTTP2_LAST_STATIC_ENTRY); diff --git a/test/core/transport/chttp2/stream_encoder_test.c b/test/core/transport/chttp2/stream_encoder_test.c index eb0f688f58e..5e8ec0a1af8 100644 --- a/test/core/transport/chttp2/stream_encoder_test.c +++ b/test/core/transport/chttp2/stream_encoder_test.c @@ -186,7 +186,7 @@ static void encode_int_to_str(int i, char *p) { static void test_decode_table_overflow(void) { int i; char key[3], value[3]; - char expect[128]; + char *expect; for (i = 0; i < 114; i++) { if (i > 0) { @@ -197,18 +197,21 @@ static void test_decode_table_overflow(void) { encode_int_to_str(i + 1, value); if (i + 61 >= 127) { - sprintf(expect, "000009 0104 deadbeef ff%02x 40 02%02x%02x 02%02x%02x", - i + 61 - 127, key[0], key[1], value[0], value[1]); + gpr_asprintf(&expect, + "000009 0104 deadbeef ff%02x 40 02%02x%02x 02%02x%02x", + i + 61 - 127, key[0], key[1], value[0], value[1]); } else if (i > 0) { - sprintf(expect, "000008 0104 deadbeef %02x 40 02%02x%02x 02%02x%02x", - 0x80 + 61 + i, key[0], key[1], value[0], value[1]); + gpr_asprintf(&expect, + "000008 0104 deadbeef %02x 40 02%02x%02x 02%02x%02x", + 0x80 + 61 + i, key[0], key[1], value[0], value[1]); } else { - sprintf(expect, "000007 0104 deadbeef 40 02%02x%02x 02%02x%02x", key[0], - key[1], value[0], value[1]); + gpr_asprintf(&expect, "000007 0104 deadbeef 40 02%02x%02x 02%02x%02x", + key[0], key[1], value[0], value[1]); } add_sopb_header(key, value); verify_sopb(0, 0, 0, expect); + gpr_free(expect); } /* if the above passes, then we must have just knocked this pair out of the diff --git a/test/core/transport/chttp2/timeout_encoding_test.c b/test/core/transport/chttp2/timeout_encoding_test.c index ffa0070e341..0ad90dbcef8 100644 --- a/test/core/transport/chttp2/timeout_encoding_test.c +++ b/test/core/transport/chttp2/timeout_encoding_test.c @@ -36,6 +36,8 @@ #include #include +#include "src/core/support/string.h" +#include #include #include #include "test/core/util/test_config.h" @@ -93,16 +95,23 @@ void decode_suite(char ext, gpr_timespec (*answer)(long x)) { 1234567, 12345678, 123456789, 98765432, 9876543, 987654, 98765, 9876, 987, 98, 9}; int i; - char input[32]; + char *input; for (i = 0; i < GPR_ARRAY_SIZE(test_vals); i++) { - sprintf(input, "%ld%c", test_vals[i], ext); + gpr_asprintf(&input, "%ld%c", test_vals[i], ext); assert_decodes_as(input, answer(test_vals[i])); - sprintf(input, " %ld%c", test_vals[i], ext); + gpr_free(input); + + gpr_asprintf(&input, " %ld%c", test_vals[i], ext); assert_decodes_as(input, answer(test_vals[i])); - sprintf(input, "%ld %c", test_vals[i], ext); + gpr_free(input); + + gpr_asprintf(&input, "%ld %c", test_vals[i], ext); assert_decodes_as(input, answer(test_vals[i])); - sprintf(input, "%ld %c ", test_vals[i], ext); + gpr_free(input); + + gpr_asprintf(&input, "%ld %c ", test_vals[i], ext); assert_decodes_as(input, answer(test_vals[i])); + gpr_free(input); } } diff --git a/test/core/transport/metadata_test.c b/test/core/transport/metadata_test.c index 804096d0e1e..b23db894bee 100644 --- a/test/core/transport/metadata_test.c +++ b/test/core/transport/metadata_test.c @@ -35,6 +35,7 @@ #include +#include "src/core/support/string.h" #include "src/core/transport/chttp2/bin_encoder.h" #include #include @@ -99,7 +100,7 @@ static void test_create_metadata(void) { static void test_create_many_ephemeral_metadata(void) { grpc_mdctx *ctx; - char buffer[256]; + char buffer[GPR_LTOA_MIN_BUFSIZE]; long i; size_t mdtab_capacity_before; @@ -109,7 +110,7 @@ static void test_create_many_ephemeral_metadata(void) { mdtab_capacity_before = grpc_mdctx_get_mdtab_capacity_test_only(ctx); /* add, and immediately delete a bunch of different elements */ for (i = 0; i < MANY; i++) { - sprintf(buffer, "%ld", i); + gpr_ltoa(i, buffer); grpc_mdelem_unref(grpc_mdelem_from_strings(ctx, "a", buffer)); } /* capacity should not grow */ @@ -120,7 +121,7 @@ static void test_create_many_ephemeral_metadata(void) { static void test_create_many_persistant_metadata(void) { grpc_mdctx *ctx; - char buffer[256]; + char buffer[GPR_LTOA_MIN_BUFSIZE]; long i; grpc_mdelem **created = gpr_malloc(sizeof(grpc_mdelem *) * MANY); grpc_mdelem *md; @@ -130,12 +131,12 @@ static void test_create_many_persistant_metadata(void) { ctx = grpc_mdctx_create(); /* add phase */ for (i = 0; i < MANY; i++) { - sprintf(buffer, "%ld", i); + gpr_ltoa(i, buffer); created[i] = grpc_mdelem_from_strings(ctx, "a", buffer); } /* verify phase */ for (i = 0; i < MANY; i++) { - sprintf(buffer, "%ld", i); + gpr_ltoa(i, buffer); md = grpc_mdelem_from_strings(ctx, "a", buffer); GPR_ASSERT(md == created[i]); grpc_mdelem_unref(md); @@ -176,7 +177,7 @@ static void test_spin_creating_the_same_thing(void) { static void test_things_stick_around(void) { grpc_mdctx *ctx; int i, j; - char buffer[64]; + char *buffer; int nstrs = 10000; grpc_mdstr **strs = gpr_malloc(sizeof(grpc_mdstr *) * nstrs); int *shuf = gpr_malloc(sizeof(int) * nstrs); @@ -187,9 +188,10 @@ static void test_things_stick_around(void) { ctx = grpc_mdctx_create(); for (i = 0; i < nstrs; i++) { - sprintf(buffer, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%dx", i); + gpr_asprintf(&buffer, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%dx", i); strs[i] = grpc_mdstr_from_string(ctx, buffer); shuf[i] = i; + gpr_free(buffer); } for (i = 0; i < nstrs; i++) { @@ -208,10 +210,11 @@ static void test_things_stick_around(void) { for (i = 0; i < nstrs; i++) { grpc_mdstr_unref(strs[shuf[i]]); for (j = i + 1; j < nstrs; j++) { - sprintf(buffer, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%dx", shuf[j]); + gpr_asprintf(&buffer, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%dx", shuf[j]); test = grpc_mdstr_from_string(ctx, buffer); GPR_ASSERT(test == strs[shuf[j]]); grpc_mdstr_unref(test); + gpr_free(buffer); } }