From ccb7b43e9b2c72c596d65715222d2a990f99fa39 Mon Sep 17 00:00:00 2001 From: Luke Dashjr Date: Sun, 3 Jun 2018 22:06:13 +0000 Subject: [PATCH 1/2] tests: Check encoding with highest bit set Detects j dropping below 0 going unchecked. This bug was originally fixed in 2c6b7916b604b946904428466ff0271a35535297, but due to ssize_t being a POSIX extension, we are going to check it manually. --- Makefile.am | 1 + tests/encode-b58c-high.sh | 3 +++ 2 files changed, 4 insertions(+) create mode 100755 tests/encode-b58c-high.sh diff --git a/Makefile.am b/Makefile.am index f870182..f51e2c0 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-small.sh SH_LOG_COMPILER = /bin/sh 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 From 4aaeea028cba3c93b3d62063f07d94681af9791c Mon Sep 17 00:00:00 2001 From: Luke Dashjr Date: Sun, 3 Jun 2018 22:07:53 +0000 Subject: [PATCH 2/2] Avoid ssize_t type, since it is POSIX-specific Only j could become negative, so we simply check before it would --- base58.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/base58.c b/base58.c index 33566e0..810ebbe 100644 --- a/base58.c +++ b/base58.c @@ -15,7 +15,6 @@ #include #include #include -#include #include "libbase58.h" @@ -146,7 +145,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]) @@ -163,6 +162,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; + } } }