Skip to content

Commit 968e8f5

Browse files
committed
data tree BUGFIX avoid memcpy with NULL
1 parent bd7fee8 commit 968e8f5

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/tree_data.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7054,13 +7054,18 @@ ly_set_dup(const struct ly_set *set)
70547054
return NULL;
70557055
}
70567056

7057-
new = malloc(sizeof *new);
7057+
new = calloc(1, sizeof *new);
70587058
LY_CHECK_ERR_RETURN(!new, LOGMEM(NULL), NULL);
70597059
new->number = set->number;
70607060
new->size = set->size;
7061-
new->set.g = malloc(new->size * sizeof *(new->set.g));
7062-
LY_CHECK_ERR_RETURN(!new->set.g, LOGMEM(NULL); free(new), NULL);
7063-
memcpy(new->set.g, set->set.g, new->size * sizeof *(new->set.g));
7061+
7062+
/* is there anything to copy? */
7063+
if (set->size) {
7064+
new->set.g = malloc(new->size * sizeof *(new->set.g));
7065+
LY_CHECK_ERR_RETURN(!new->set.g, LOGMEM(NULL); free(new), NULL);
7066+
assert(set->set.g);
7067+
memcpy(new->set.g, set->set.g, new->size * sizeof *(new->set.g));
7068+
}
70647069

70657070
return new;
70667071
}

0 commit comments

Comments
 (0)