diff options
author | Doug Hogan <[email protected]> | 2019-01-03 18:55:31 -0800 |
---|---|---|
committer | Doug Hogan <[email protected]> | 2019-01-08 00:08:22 -0800 |
commit | c41e0bb6d6981ab6fcf33365efa6ddd194fddd39 (patch) | |
tree | 526c206e63bdf13323c10c3fbf4d100a86391955 /lib | |
parent | 7ad28fd1bd0b6504c78c5d76903878cebd5cd631 (diff) | |
download | otp-c41e0bb6d6981ab6fcf33365efa6ddd194fddd39.tar.gz otp-c41e0bb6d6981ab6fcf33365efa6ddd194fddd39.tar.bz2 otp-c41e0bb6d6981ab6fcf33365efa6ddd194fddd39.zip |
Revamp change_basename()
* Change the parameter from int to size_t.
- Only caller doesn't need to change since it was already passing sizeof().
* Add unsigned wrapping checks.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/crypto/c_src/info.c | 21 | ||||
-rw-r--r-- | lib/crypto/c_src/info.h | 2 |
2 files changed, 16 insertions, 7 deletions
diff --git a/lib/crypto/c_src/info.c b/lib/crypto/c_src/info.c index 3f3194081d..d4e230dffd 100644 --- a/lib/crypto/c_src/info.c +++ b/lib/crypto/c_src/info.c @@ -30,21 +30,30 @@ char *crypto_callback_name = "crypto_callback.valgrind"; char *crypto_callback_name = "crypto_callback"; # endif -int change_basename(ErlNifBinary* bin, char* buf, int bufsz, const char* newfile) +int change_basename(ErlNifBinary* bin, char* buf, size_t bufsz, const char* newfile) { - int i; + size_t i; + size_t newlen; for (i = bin->size; i > 0; i--) { if (bin->data[i-1] == '/') break; } - if (i + strlen(newfile) >= bufsz) { - PRINTF_ERR0("CRYPTO: lib name too long"); - return 0; - } + + newlen = strlen(newfile); + if (i > SIZE_MAX - newlen) + goto err; + + if (i + newlen >= bufsz) + goto err; + memcpy(buf, bin->data, i); strcpy(buf+i, newfile); + return 1; + + err: + return 0; } void error_handler(void* null, const char* errstr) diff --git a/lib/crypto/c_src/info.h b/lib/crypto/c_src/info.h index 4f8822ddd7..67690625c9 100644 --- a/lib/crypto/c_src/info.h +++ b/lib/crypto/c_src/info.h @@ -26,7 +26,7 @@ #ifdef HAVE_DYNAMIC_CRYPTO_LIB extern char *crypto_callback_name; -int change_basename(ErlNifBinary* bin, char* buf, int bufsz, const char* newfile); +int change_basename(ErlNifBinary* bin, char* buf, size_t bufsz, const char* newfile); void error_handler(void* null, const char* errstr); #endif |