diff --git a/system/libbase58/base58.c b/system/libbase58/base58.c index 4c1f5751ab4..874b92b758b 100644 --- a/system/libbase58/base58.c +++ b/system/libbase58/base58.c @@ -137,9 +137,9 @@ int b58check(const void *bin, size_t binsz, const char *base58str, size_t b58sz) return -1; // Check number of zeros is correct AFTER verifying checksum (to avoid possibility of accessing base58str beyond the end) - for (i = 0; binc[i] == '\0' && base58str[i] == '1'; ++i) + for (i = 0; i < binsz && binc[i] == '\0' && i < b58sz && base58str[i] == '1'; ++i) {} // Just finding the end of zeros, nothing to do in loop - if (binc[i] == '\0' || base58str[i] == '1') + if (i >= binsz || binc[i] == '\0' || i >= b58sz || base58str[i] == '1') return -3; return binc[0];