From 82a93c17943c510b0bf33904429d47fbb74d9fc2 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Sun, 14 Oct 2018 10:43:21 +0200 Subject: [PATCH] ext/sodium: sodium_pad(): do not copy any bytes if the string is empty Spotted by San Zhang, thanks! Backport from PECL libsodium-php 2.0.13 --- ext/sodium/libsodium.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/ext/sodium/libsodium.c b/ext/sodium/libsodium.c index 5462700f6c..b6a4925498 100644 --- a/ext/sodium/libsodium.c +++ b/ext/sodium/libsodium.c @@ -3402,12 +3402,17 @@ PHP_FUNCTION(sodium_pad) st = 1U; i = 0U; k = unpadded_len; - for (j = 0U; j <= xpadded_len; j++) { - ZSTR_VAL(padded)[j] = unpadded[i]; - k -= st; - st = (size_t) (~(((( (((uint64_t) k) >> 48) | (((uint64_t) k) >> 32) | - (k >> 16) | k) & 0xffff) - 1U) >> 16)) & 1U; - i += st; + if (unpadded_len > 0) { + st = 1U; + i = 0U; + k = unpadded_len; + for (j = 0U; j <= xpadded_len; j++) { + ZSTR_VAL(padded)[j] = unpadded[i]; + k -= st; + st = (size_t) (~(((( (((uint64_t) k) >> 48) | (((uint64_t) k) >> 32) | + (k >> 16) | k) & 0xffff) - 1U) >> 16)) & 1U; + i += st; + } } #if SODIUM_LIBRARY_VERSION_MAJOR > 9 || (SODIUM_LIBRARY_VERSION_MAJOR == 9 && SODIUM_LIBRARY_VERSION_MINOR >= 6) if (sodium_pad(NULL, (unsigned char *) ZSTR_VAL(padded), unpadded_len, -- 2.40.0