]> granicus.if.org Git - php/commitdiff
ext/sodium: throw an exception if only the prefix of a hex string is valid
authorFrank Denis <github@pureftpd.org>
Tue, 19 Sep 2017 12:37:21 +0000 (14:37 +0200)
committerFrank Denis <github@pureftpd.org>
Tue, 19 Sep 2017 12:37:21 +0000 (14:37 +0200)
ext/sodium/libsodium.c

index 2c48b4489ae113ba68f49036f3665ff743ea5fac..bb0aad46c50b13485aac9b1a86e4f4b37c371f36 100644 (file)
@@ -2568,6 +2568,7 @@ PHP_FUNCTION(sodium_bin2hex)
 PHP_FUNCTION(sodium_hex2bin)
 {
        zend_string   *bin;
+       const char    *end;
        char          *hex;
        char          *ignore = NULL;
        size_t         bin_real_len;
@@ -2584,8 +2585,13 @@ PHP_FUNCTION(sodium_hex2bin)
        bin_len = hex_len / 2;
        bin = zend_string_alloc(bin_len, 0);
        if (sodium_hex2bin((unsigned char *) ZSTR_VAL(bin), bin_len, hex, hex_len,
-                                          ignore, &bin_real_len, NULL) != 0 ||
-               bin_real_len >= SIZE_MAX || bin_real_len > bin_len) {
+                                          ignore, &bin_real_len, &end) != 0 ||
+               end != hex + hex_len) {
+               zend_string_free(bin);
+               zend_throw_exception(sodium_exception_ce, "invalid hex string", 0);
+               return;
+       }
+       if (bin_real_len >= SIZE_MAX || bin_real_len > bin_len) {
                zend_string_free(bin);
                zend_throw_exception(sodium_exception_ce, "arithmetic overflow", 0);
                return;