Skip to content

Commit 6da0029

Browse files
committed
descriptor: add tests for calling with a too-short buffer
Use a freshly allocated buffer of the computed size to allocate into, so valgrind can warn us of any errant writes. Also add a testcase for a simple key push without CHECKSIG.
1 parent 7fb7b1e commit 6da0029

File tree

1 file changed

+46
-8
lines changed

1 file changed

+46
-8
lines changed

src/ctest/test_descriptor.c

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,12 @@ static const struct descriptor_test {
503503
* Miniscript: Randomly generated test set that covers the majority of type and node type combinations
504504
*/
505505
{
506+
"miniscript - A single key",
507+
"pk_k(key_1)",
508+
WALLY_NETWORK_NONE, 0, 0, 0, NULL, WALLY_MINISCRIPT_ONLY,
509+
"21038bc7431d9285a064b0328b6333f3a20b86664437b6de8f4e26e6bbdee258f048",
510+
"jyazm5lc"
511+
},{
506512
"miniscript - random 1",
507513
"lltvln:after(1231488000)",
508514
WALLY_NETWORK_NONE, 0, 0, 0, NULL, WALLY_MINISCRIPT_ONLY,
@@ -787,13 +793,13 @@ static const struct descriptor_test {
787793
* Miniscript: BOLT examples
788794
*/
789795
{
790-
"miniscript - A single key",
796+
"miniscript - A single key + CHECKSIG",
791797
"c:pk_k(key_1)",
792798
WALLY_NETWORK_NONE, 0, 0, 0, NULL, WALLY_MINISCRIPT_ONLY,
793799
"21038bc7431d9285a064b0328b6333f3a20b86664437b6de8f4e26e6bbdee258f048ac",
794800
""
795801
}, {
796-
"miniscript - A single key (2)",
802+
"miniscript - A single key + CHECKSIG (2)",
797803
"pk(key_1)",
798804
WALLY_NETWORK_NONE, 0, 0, 0, NULL, WALLY_MINISCRIPT_ONLY,
799805
"21038bc7431d9285a064b0328b6333f3a20b86664437b6de8f4e26e6bbdee258f048ac",
@@ -2062,9 +2068,8 @@ static const struct address_test {
20622068
static bool check_descriptor_to_script(const struct descriptor_test* test)
20632069
{
20642070
struct wally_descriptor *descriptor;
2065-
size_t written, max_written;
2066-
const size_t script_len = 520;
2067-
unsigned char *script = malloc(script_len);
2071+
size_t written, computed_written, max_written;
2072+
const size_t default_script_len = 520;
20682073
char *checksum, *canonical;
20692074
int expected_ret, ret, len_ret;
20702075
uint32_t multi_index = 0;
@@ -2094,25 +2099,53 @@ static bool check_descriptor_to_script(const struct descriptor_test* test)
20942099
if (!check_ret("descriptor_parse", ret, expected_ret))
20952100
return false;
20962101

2097-
if (expected_ret != WALLY_OK) {
2098-
free(script);
2102+
if (expected_ret != WALLY_OK)
20992103
return true;
2100-
}
21012104
}
21022105

2106+
computed_written = default_script_len;
2107+
if (expected_ret == WALLY_OK) {
2108+
/* Try the call with a too-short buffer.
2109+
* This returns a more exact required size for generation, although
2110+
* it may still overestimate by a few bytes for some descriptors.
2111+
*/
2112+
unsigned char *short_script = malloc(1);
2113+
ret = wally_descriptor_to_script(descriptor,
2114+
test->depth, test->index,
2115+
test->variant, multi_index,
2116+
child_num, 0,
2117+
short_script, 1, &computed_written);
2118+
free(short_script);
2119+
if (!check_ret("descriptor_to_script(short buffer)\n", ret, expected_ret))
2120+
return false;
2121+
}
2122+
2123+
const size_t script_len = computed_written ? computed_written : 1;
2124+
unsigned char *script = malloc(script_len);
21032125
ret = wally_descriptor_to_script(descriptor,
21042126
test->depth, test->index,
21052127
test->variant, multi_index,
21062128
child_num, 0,
21072129
script, script_len, &written);
2130+
if (ret == WALLY_OK && written > script_len) {
2131+
printf("descriptor_to_script: wrote more than computed length!\n");
2132+
return false;
2133+
}
21082134
if (!check_ret("descriptor_to_script", ret, expected_ret))
21092135
return false;
2136+
21102137
if (expected_ret != WALLY_OK) {
2138+
/* Failure case: stop testing here */
21112139
wally_descriptor_free(descriptor);
21122140
free(script);
21132141
return true;
21142142
}
21152143

2144+
if (computed_written < written) {
2145+
printf("descriptor_to_script: computed < written\n");
2146+
return false;
2147+
}
2148+
21162149
ret = wally_descriptor_get_features(descriptor, &features);
21172150
if (!check_ret("descriptor_get_features", ret, WALLY_OK))
21182151
return false;
@@ -2124,6 +2157,11 @@ static bool check_descriptor_to_script(const struct descriptor_test* test)
21242157
max_written < written)
21252158
return false;
21262159

2160+
if (computed_written > max_written) {
2161+
printf("descriptor_to_script: computed > max written\n");
2162+
return false;
2163+
}
2164+
21272165
ret = wally_descriptor_get_checksum(descriptor, 0, &checksum);
21282166
if (!check_ret("descriptor_get_checksum", ret, WALLY_OK))
21292167
return false;

0 commit comments

Comments
 (0)