OPENSSL_strndup should not return NULL given {NULL, 0}.

The NUL-terminated representation of the empty string is a non-NULL
one-byte array, not NULL. This fills in the last of the empty string
cases in https://boringssl-review.googlesource.com/c/boringssl/+/49006/

Change-Id: I66c09dc3223f762b708612987b26c90e41e27c4a
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/49009
Reviewed-by: Adam Langley <agl@google.com>
This commit is contained in:
David Benjamin 2021-08-24 16:03:34 -04:00 committed by Adam Langley
parent b27438e126
commit 5984cfe8e4
2 changed files with 9 additions and 9 deletions

View File

@ -20,6 +20,7 @@
#include <openssl/base.h>
#include <openssl/crypto.h>
#include <openssl/cipher.h>
#include <openssl/mem.h>
#include <gtest/gtest.h>
@ -35,6 +36,12 @@ TEST(CryptoTest, Version) {
std::string(OPENSSL_VERSION_TEXT).substr(0, strlen(expected)));
}
TEST(CryptoTest, Strndup) {
bssl::UniquePtr<char> str(OPENSSL_strndup(nullptr, 0));
EXPECT_TRUE(str);
EXPECT_STREQ("", str.get());
}
#if defined(BORINGSSL_FIPS_COUNTERS)
TEST(CryptoTest, FIPSCountersEVP) {
constexpr struct {

View File

@ -328,22 +328,15 @@ int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args) {
}
char *OPENSSL_strndup(const char *str, size_t size) {
char *ret;
size_t alloc_size;
if (str == NULL) {
return NULL;
}
size = OPENSSL_strnlen(str, size);
alloc_size = size + 1;
size_t alloc_size = size + 1;
if (alloc_size < size) {
// overflow
OPENSSL_PUT_ERROR(CRYPTO, ERR_R_MALLOC_FAILURE);
return NULL;
}
ret = OPENSSL_malloc(alloc_size);
char *ret = OPENSSL_malloc(alloc_size);
if (ret == NULL) {
OPENSSL_PUT_ERROR(CRYPTO, ERR_R_MALLOC_FAILURE);
return NULL;