diff options
| author | Simon Tatham <anakin@pobox.com> | 2007-01-11 09:26:09 +0000 |
|---|---|---|
| committer | Simon Tatham <anakin@pobox.com> | 2007-01-11 09:26:09 +0000 |
| commit | c06e371f55b97576421588d21d809c02c637584b (patch) | |
| tree | 1d402bfea24347fc3f1706f1ea4ed2ad34b8d13f | |
| parent | 7c59e7259f6e39e8254815afbc252e2bd4c15c97 (diff) | |
| download | puzzles-c06e371f55b97576421588d21d809c02c637584b.zip puzzles-c06e371f55b97576421588d21d809c02c637584b.tar.gz puzzles-c06e371f55b97576421588d21d809c02c637584b.tar.bz2 puzzles-c06e371f55b97576421588d21d809c02c637584b.tar.xz | |
Rather to my surprise given the amount of testing this code has had,
Kevin Lyles spotted a tree234 bug! copytree234() segfaulted when
asked to copy a tree containing no elements, due to failing to allow
for the case that t->root might be NULL. Fixed, and added a
regression test.
[originally from svn r7092]
| -rw-r--r-- | tree234.c | 17 |
1 files changed, 11 insertions, 6 deletions
@@ -35,6 +35,9 @@ #ifdef TEST #define LOG(x) (printf x) +#define smalloc malloc +#define srealloc realloc +#define sfree free #else #define LOG(x) #endif @@ -1438,8 +1441,11 @@ tree234 *copytree234(tree234 *t, copyfn234 copyfn, void *copyfnstate) { tree234 *t2; t2 = newtree234(t->cmp); - t2->root = copynode234(t->root, copyfn, copyfnstate); - t2->root->parent = NULL; + if (t->root) { + t2->root = copynode234(t->root, copyfn, copyfnstate); + t2->root->parent = NULL; + } else + t2->root = NULL; return t2; } @@ -1885,8 +1891,6 @@ int mycmp(void *av, void *bv) { return strcmp(a, b); } -#define lenof(x) ( sizeof((x)) / sizeof(*(x)) ) - char *strings[] = { "0", "2", "3", "I", "K", "d", "H", "J", "Q", "N", "n", "q", "j", "i", "7", "G", "F", "D", "b", "x", "g", "B", "e", "v", "V", "T", "f", "E", @@ -2120,11 +2124,12 @@ int main(void) { tree = newtree234(mycmp); cmp = mycmp; arraylen = 0; - for (i = 0; i < 16; i++) { - addtest(strings[i]); + for (i = 0; i < 17; i++) { tree2 = copytree234(tree, NULL, NULL); splittest(tree2, array, arraylen); freetree234(tree2); + if (i < 16) + addtest(strings[i]); } freetree234(tree); |