diff options
| author | Garrett D'Amore <garrett@damore.org> | 2018-07-20 13:42:13 -0700 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2018-07-24 07:57:39 -0700 |
| commit | ccc24a8e508131a2226474642a038baaa2cbcc8c (patch) | |
| tree | 7029f7668fe3e1a9899da57bf6c1e60e0394bacb /src/supplemental | |
| parent | 9b9526e4a643d36d9c66f2254f00df7298e5562f (diff) | |
| download | nng-ccc24a8e508131a2226474642a038baaa2cbcc8c.tar.gz nng-ccc24a8e508131a2226474642a038baaa2cbcc8c.tar.bz2 nng-ccc24a8e508131a2226474642a038baaa2cbcc8c.zip | |
fixes #605 NNI_ALLOC_STRUCT/NNI_ALLOC_STRUCTS should zero memory
Diffstat (limited to 'src/supplemental')
| -rw-r--r-- | src/supplemental/http/http_msg.c | 36 | ||||
| -rw-r--r-- | src/supplemental/tls/mbedtls/tls.c | 10 |
2 files changed, 18 insertions, 28 deletions
diff --git a/src/supplemental/http/http_msg.c b/src/supplemental/http/http_msg.c index ff9764bf..d6ab862e 100644 --- a/src/supplemental/http/http_msg.c +++ b/src/supplemental/http/http_msg.c @@ -77,12 +77,8 @@ http_headers_reset(nni_list *hdrs) http_header *h; while ((h = nni_list_first(hdrs)) != NULL) { nni_list_remove(hdrs, h); - if (h->name != NULL) { - nni_strfree(h->name); - } - if (h->value != NULL) { - nni_free(h->value, strlen(h->value) + 1); - } + nni_strfree(h->name); + nni_strfree(h->value); NNI_FREE_STRUCT(h); } } @@ -181,13 +177,11 @@ http_set_header(nni_list *hdrs, const char *key, const char *val) http_header *h; NNI_LIST_FOREACH (hdrs, h) { if (nni_strcasecmp(key, h->name) == 0) { - char * news; - size_t len = strlen(val) + 1; - if ((news = nni_alloc(len)) == NULL) { + char *news; + if ((news = nni_strdup(val)) == NULL) { return (NNG_ENOMEM); } - snprintf(news, len, "%s", val); - nni_free(h->value, strlen(h->value) + 1); + nni_strfree(h->value); h->value = news; return (0); } @@ -200,12 +194,11 @@ http_set_header(nni_list *hdrs, const char *key, const char *val) NNI_FREE_STRUCT(h); return (NNG_ENOMEM); } - if ((h->value = nni_alloc(strlen(val) + 1)) == NULL) { + if ((h->value = nni_strdup(val)) == NULL) { nni_strfree(h->name); NNI_FREE_STRUCT(h); return (NNG_ENOMEM); } - strncpy(h->value, val, strlen(val) + 1); nni_list_append(hdrs, h); return (0); } @@ -228,13 +221,13 @@ http_add_header(nni_list *hdrs, const char *key, const char *val) http_header *h; NNI_LIST_FOREACH (hdrs, h) { if (nni_strcasecmp(key, h->name) == 0) { - char * news; - size_t len = strlen(h->value) + strlen(val) + 3; - if ((news = nni_alloc(len)) == NULL) { - return (NNG_ENOMEM); + char *news; + int rv; + rv = nni_asprintf(&news, "%s, %s", h->value, val); + if (rv != 0) { + return (rv); } - snprintf(news, len, "%s, %s", h->value, val); - nni_free(h->value, strlen(h->value) + 1); + nni_strfree(h->value); h->value = news; return (0); } @@ -247,12 +240,11 @@ http_add_header(nni_list *hdrs, const char *key, const char *val) NNI_FREE_STRUCT(h); return (NNG_ENOMEM); } - if ((h->value = nni_alloc(strlen(val) + 1)) == NULL) { + if ((h->value = nni_strdup(val)) == NULL) { nni_strfree(h->name); NNI_FREE_STRUCT(h); return (NNG_ENOMEM); } - strncpy(h->value, val, strlen(val) + 1); nni_list_append(hdrs, h); return (0); } @@ -310,7 +302,7 @@ static int http_entity_alloc_data(nni_http_entity *entity, size_t size) { void *newdata; - if ((newdata = nni_alloc(size)) == NULL) { + if ((newdata = nni_zalloc(size)) == NULL) { return (NNG_ENOMEM); } http_entity_set_data(entity, newdata, size); diff --git a/src/supplemental/tls/mbedtls/tls.c b/src/supplemental/tls/mbedtls/tls.c index 0f4f67cc..42333783 100644 --- a/src/supplemental/tls/mbedtls/tls.c +++ b/src/supplemental/tls/mbedtls/tls.c @@ -319,11 +319,11 @@ nni_tls_init(nni_tls **tpp, nng_tls_config *cfg, nni_tcp_conn *tcp) if ((tp = NNI_ALLOC_STRUCT(tp)) == NULL) { return (NNG_ENOMEM); } - if ((tp->recvbuf = nni_alloc(NNG_TLS_MAX_RECV_SIZE)) == NULL) { + if ((tp->recvbuf = nni_zalloc(NNG_TLS_MAX_RECV_SIZE)) == NULL) { NNI_FREE_STRUCT(tp); return (NNG_ENOMEM); } - if ((tp->sendbuf = nni_alloc(NNG_TLS_MAX_SEND_SIZE)) == NULL) { + if ((tp->sendbuf = nni_zalloc(NNG_TLS_MAX_SEND_SIZE)) == NULL) { nni_free(tp->sendbuf, NNG_TLS_MAX_RECV_SIZE); NNI_FREE_STRUCT(tp); return (NNG_ENOMEM); @@ -957,12 +957,11 @@ nng_tls_config_ca_file(nng_tls_config *cfg, const char *path) if ((rv = nni_file_get(path, &fdata, &fsize)) != 0) { return (rv); } - if ((pem = nni_alloc(fsize + 1)) == NULL) { + if ((pem = nni_zalloc(fsize + 1)) == NULL) { nni_free(fdata, fsize); return (NNG_ENOMEM); } memcpy(pem, fdata, fsize); - pem[fsize] = '\0'; nni_free(fdata, fsize); if (strstr(pem, "-----BEGIN X509 CRL-----") != NULL) { rv = nng_tls_config_ca_chain(cfg, pem, pem); @@ -992,12 +991,11 @@ nng_tls_config_cert_key_file( if ((rv = nni_file_get(path, &fdata, &fsize)) != 0) { return (rv); } - if ((pem = nni_alloc(fsize + 1)) == NULL) { + if ((pem = nni_zalloc(fsize + 1)) == NULL) { nni_free(fdata, fsize); return (NNG_ENOMEM); } memcpy(pem, fdata, fsize); - pem[fsize] = '\0'; nni_free(fdata, fsize); rv = nng_tls_config_own_cert(cfg, pem, pem, pass); nni_free(pem, fsize + 1); |
