diff options
author | Tim Rühsen <tim.ruehsen@gmx.de> | 2019-01-29 16:10:59 +0100 |
---|---|---|
committer | Tim Rühsen <tim.ruehsen@gmx.de> | 2019-02-14 10:29:25 +0100 |
commit | 95dd4be4e7461dd07cc64c6ae5aaa8d5cc553935 (patch) | |
tree | 0ff477d2d24e7e29a4d64cab9eb85f5827f857df | |
parent | 1b8703fb1a748a973f6c7c60269632218ab77596 (diff) | |
download | gnutls-95dd4be4e7461dd07cc64c6ae5aaa8d5cc553935.tar.gz |
Fix uninitialized warning in pkcs11.c
Signed-off-by: Tim Rühsen <tim.ruehsen@gmx.de>
-rw-r--r-- | lib/pkcs11.c | 42 | ||||
-rw-r--r-- | tests/pkcs11/pkcs11-token-raw.c | 30 |
2 files changed, 46 insertions, 26 deletions
diff --git a/lib/pkcs11.c b/lib/pkcs11.c index 39e2799b1e..80d7c57b1f 100644 --- a/lib/pkcs11.c +++ b/lib/pkcs11.c @@ -2475,7 +2475,6 @@ gnutls_pkcs11_token_get_info(const char *url, { struct p11_kit_uri *info = NULL; const uint8_t *str; - size_t str_max; char *temp_str = NULL; size_t len; int ret; @@ -2491,19 +2490,19 @@ gnutls_pkcs11_token_get_info(const char *url, switch (ttype) { case GNUTLS_PKCS11_TOKEN_LABEL: str = p11_kit_uri_get_token_info(info)->label; - str_max = 32; + len = p11_kit_space_strlen(str, 32); break; case GNUTLS_PKCS11_TOKEN_SERIAL: str = p11_kit_uri_get_token_info(info)->serial_number; - str_max = 16; + len = p11_kit_space_strlen(str, 16); break; case GNUTLS_PKCS11_TOKEN_MANUFACTURER: str = p11_kit_uri_get_token_info(info)->manufacturer_id; - str_max = 32; + len = p11_kit_space_strlen(str, 32); break; case GNUTLS_PKCS11_TOKEN_MODEL: str = p11_kit_uri_get_token_info(info)->model; - str_max = 16; + len = p11_kit_space_strlen(str, 16); break; case GNUTLS_PKCS11_TOKEN_MODNAME: { struct find_token_modname tn; @@ -2518,11 +2517,12 @@ gnutls_pkcs11_token_get_info(const char *url, } temp_str = tn.modname; - if (temp_str == NULL) { - gnutls_assert(); - str_max = 0; - } else { + if (temp_str) { str = (uint8_t *)temp_str; + len = strlen(temp_str); + } else { + gnutls_assert(); + len = 0; } break; } @@ -2532,27 +2532,17 @@ gnutls_pkcs11_token_get_info(const char *url, goto cleanup; } - if (temp_str) - len = strlen(temp_str); - else if (str_max == 0) - len = 0; - else - len = p11_kit_space_strlen(str, str_max); - - if (len + 1 > *output_size) { + if (len < *output_size) { + if (len) + memcpy(output, str, len); + ((char *) output)[len] = '\0'; + *output_size = len; + ret = 0; + } else { *output_size = len + 1; ret = GNUTLS_E_SHORT_MEMORY_BUFFER; - goto cleanup; } - if (len) - memcpy(output, str, len); - ((char *) output)[len] = '\0'; - - *output_size = len; - - ret = 0; - cleanup: free(temp_str); p11_kit_uri_free(info); diff --git a/tests/pkcs11/pkcs11-token-raw.c b/tests/pkcs11/pkcs11-token-raw.c index bbcb23eb81..605c96ce1d 100644 --- a/tests/pkcs11/pkcs11-token-raw.c +++ b/tests/pkcs11/pkcs11-token-raw.c @@ -93,6 +93,36 @@ void doit(void) exit(1); } + { + static const char url[] = "pkcs11:token=whatever"; + + /* Testing a too small buffer */ + size_t size = 1; + char *buf = gnutls_malloc(size); + ret = gnutls_pkcs11_token_get_info(url, + GNUTLS_PKCS11_TOKEN_LABEL, + buf, &size); + assert(ret == GNUTLS_E_SHORT_MEMORY_BUFFER); + + /* Testing a too small buffer by one */ + size -= 1; + buf = gnutls_realloc(buf, size); + ret = gnutls_pkcs11_token_get_info(url, + GNUTLS_PKCS11_TOKEN_LABEL, + buf, &size); + assert(ret == GNUTLS_E_SHORT_MEMORY_BUFFER); + + /* Testing an exactly fitting buffer */ + buf = gnutls_realloc(buf, size); + ret = gnutls_pkcs11_token_get_info(url, + GNUTLS_PKCS11_TOKEN_LABEL, + buf, &size); + assert(ret == 0); + assert(strcmp(buf, "whatever") == 0); + + gnutls_free(buf); + } + ret = gnutls_pkcs11_token_get_ptr("pkcs11:token=invalid", (void**)&mod, &slot_id, 0); assert(ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE); |