From 2888dbace9f593cf2588e33b344861a2600f0e0e Mon Sep 17 00:00:00 2001 From: Bruno Garcia Date: Wed, 2 Sep 2026 20:40:42 -0300 Subject: [PATCH 1/3] bech32: fix encoding a maximum length segwit address bech32_encode() allows an encoded address of up to 90 characters, as BIP-173 specifies, but wally_addr_segwit_from_bytes() encodes into a 90 byte stack buffer. When the address is exactly 90 characters long the NUL terminator is written one byte past the end of the buffer. Any 30 character hrp with a 32 byte version 0 program hits the boundary, as does any other combination whose encoded length is 90. Size the buffer for the maximum length plus the terminator, matching what the blech32 encoder already does, and add the boundary case to the bech32 test vectors so it is exercised in both directions. Found by bitcoinfuzz. --- src/bech32.c | 2 +- src/test/test_bech32.py | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/bech32.c b/src/bech32.c index bfbda6144..8600cea04 100644 --- a/src/bech32.c +++ b/src/bech32.c @@ -208,7 +208,7 @@ int wally_addr_segwit_from_bytes(const unsigned char *bytes, size_t bytes_len, const char *addr_family, uint32_t flags, char **output) { - char result[90]; + char result[90 + 1]; /* Max encoded length plus NUL */ size_t push_size; int ret; size_t witver; diff --git a/src/test/test_bech32.py b/src/test/test_bech32.py index 2aa77c408..0e51cb529 100755 --- a/src/test/test_bech32.py +++ b/src/test/test_bech32.py @@ -20,7 +20,10 @@ 'tb1pqqqqp399et2xygdj5xreqhjjvcmzhxw4aywxecjdzew6hylgvsesf3hn0c': ['tb', 1, '5120000000c4a5cad46221b2a187905e5266362b99d5e91c6ce24d165dab93e86433'], 'bc1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vqzk5jj0': - ['bc', 1, '512079be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798'] + ['bc', 1, '512079be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798'], + # Maximum length (90 character) address: 30 character hrp, 32 byte v0 program + 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq6nf3d': + ['a' * 30, 0, '0020' + '00' * 32], } invalid_cases = [ From 255673e682087a5c0ca7bfe9a031dc1bc301a18b Mon Sep 17 00:00:00 2001 From: Jon Griffiths Date: Thu, 3 Sep 2026 15:07:31 +1200 Subject: [PATCH 2/3] swig: fix apple python build by providing calloc() --- src/swig_python/swig.i | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/swig_python/swig.i b/src/swig_python/swig.i index 039c66292..8fec31322 100644 --- a/src/swig_python/swig.i +++ b/src/swig_python/swig.i @@ -43,8 +43,10 @@ del swig_import_helper #include "../internal.h" #undef malloc +#undef calloc #undef free #define malloc(size) wally_malloc(size) +#define calloc(n, size) wally_calloc((n) * (size)) #define free(ptr) wally_free(ptr) static int check_result(int result) From 374df231d05410a31d605b31c1cc5256581381d7 Mon Sep 17 00:00:00 2001 From: Jon Griffiths Date: Thu, 3 Sep 2026 15:19:58 +1200 Subject: [PATCH 3/3] swig: update c api calls removed in newer python versions --- src/swig_python/swig.i | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/swig_python/swig.i b/src/swig_python/swig.i index 8fec31322..2b2221cfd 100644 --- a/src/swig_python/swig.i +++ b/src/swig_python/swig.i @@ -184,7 +184,7 @@ static void destroy_words(PyObject *obj) { (void)obj; } } %typemap(argout) size_t* written { Py_DecRef($result); - $result = PyInt_FromSize_t(*$1); + $result = PyLong_FromSize_t(*$1); } %typemap(in, numinputs=0) uint32_t *value_out (uint32_t val) { @@ -211,7 +211,7 @@ static void destroy_words(PyObject *obj) { (void)obj; } %typemap(argout) char** { if (*$1 != NULL) { Py_DecRef($result); - $result = PyString_FromString(*$1); + $result = PyUnicode_FromString(*$1); wally_free_string(*$1); } } @@ -230,7 +230,7 @@ static void destroy_words(PyObject *obj) { (void)obj; } Py_DecRef($result); $result = PyList_New($2); for (i = 0; i < $2; i++) { - PyObject *s = PyString_FromString($1[i]); + PyObject *s = PyUnicode_FromString($1[i]); PyList_SetItem($result, i, s); wally_free_string($1[i]); }