diff --git a/Makefile.am b/Makefile.am index 89b2c85..985fbe0 100644 --- a/Makefile.am +++ b/Makefile.am @@ -33,6 +33,7 @@ TESTS = \ tests/decode-zero.sh \ tests/encode.sh \ tests/encode-b58c.sh \ + tests/encode-b58c-high.sh \ tests/encode-fail.sh \ tests/encode-neg-index.sh \ tests/encode-small.sh diff --git a/base58.c b/base58.c index 71e687f..6265741 100644 --- a/base58.c +++ b/base58.c @@ -15,7 +15,6 @@ #include #include #include -#include #include "libbase58.h" @@ -147,7 +146,7 @@ bool b58enc(char *b58, size_t *b58sz, const void *data, size_t binsz) { const uint8_t *bin = data; int carry; - ssize_t i, j, high, zcount = 0; + size_t i, j, high, zcount = 0; size_t size; while (zcount < binsz && !bin[zcount]) @@ -164,6 +163,10 @@ bool b58enc(char *b58, size_t *b58sz, const void *data, size_t binsz) carry += 256 * buf[j]; buf[j] = carry % 58; carry /= 58; + if (!j) { + // Otherwise j wraps to maxint which is > high + break; + } } } diff --git a/tests/encode-b58c-high.sh b/tests/encode-b58c-high.sh new file mode 100755 index 0000000..6c788f6 --- /dev/null +++ b/tests/encode-b58c-high.sh @@ -0,0 +1,3 @@ +#!/bin/sh +b58=$(echo 'ff5a1fc5dd9e6f03819fca94a2d89669469667f9a0' | xxd -r -p | base58 -c) +test x$b58 = x2mkQLxaN3Y4CwN5E9rdMWNgsXX7VS6UnfeT