bin2hex & bin2base64: return a null size on error

This might prevent applications that don't properly check return codes
from reusing previous data.
This commit is contained in:
Frank Denis 2017-08-09 22:23:16 +02:00
parent a6480aec44
commit 265bdcfe07
3 changed files with 10 additions and 3 deletions

View File

@ -86,6 +86,9 @@ sodium_hex2bin(unsigned char *const bin, const size_t bin_maxlen,
if (hex_end != NULL) {
*hex_end = &hex[hex_pos];
}
if (ret != 0) {
bin_pos = (size_t) 0U;
}
if (bin_len != NULL) {
*bin_len = bin_pos;
}
@ -208,6 +211,7 @@ sodium_bin2base64(char * const b64, const size_t b64_maxlen,
b64[b64_pos++] = (char) b64_byte_to_char((acc << (6 - acc_len)) & 0x3F);
}
}
assert(b64_pos <= b64_len);
while (b64_pos < b64_len) {
b64[b64_pos++] = '=';
}
@ -298,6 +302,9 @@ sodium_base642bin(unsigned char * const bin, const size_t bin_maxlen,
ret = _sodium_base642bin_skip_padding(b64, b64_len, &b64_pos, ignore,
acc_len / 2);
}
if (ret != 0) {
bin_pos = (size_t) 0U;
}
if (bin_len != NULL) {
*bin_len = bin_pos;
}

View File

@ -117,8 +117,8 @@ main(void)
b64 = "VGhpcyBpcyBhIGpvdXJu" "\n" "ZXkgaW50by" " " "Bzb3VuZA==";
memset(buf4, '*', sizeof buf4);
sodium_base642bin(buf4, sizeof buf4, b64, strlen(b64), "\n\r ", &bin_len,
&b64_end, sodium_base64_VARIANT_ORIGINAL);
assert(sodium_base642bin(buf4, sizeof buf4, b64, strlen(b64), "\n\r ", &bin_len,
&b64_end, sodium_base64_VARIANT_ORIGINAL) == -1);
buf4[bin_len] = 0;
printf("[%s]\n", (const char *) buf4);
printf("[%s]\n", b64_end);

View File

@ -23,7 +23,7 @@ YWJj
YQ
YWI
YWJj
[This]
[]
[BpcyBhIGpvdXJu
ZXkgaW50by Bzb3VuZA==]
[This is a journey into sound]