yangre BUGFIX in add_pattern()
Memory problems in case of error.
diff --git a/tools/lint/yl_schema_features.c b/tools/lint/yl_schema_features.c
index b452496..ceaff36 100644
--- a/tools/lint/yl_schema_features.c
+++ b/tools/lint/yl_schema_features.c
@@ -63,26 +63,21 @@
int
parse_features(const char *fstring, struct ly_set *fset)
{
- struct yl_schema_features *rec;
+ struct yl_schema_features *rec = NULL;
uint32_t count;
char *p, **fp;
rec = calloc(1, sizeof *rec);
if (!rec) {
YLMSG_E("Unable to allocate features information record (%s).\n", strerror(errno));
- return -1;
- }
- if (ly_set_add(fset, rec, 1, NULL)) {
- YLMSG_E("Unable to store features information (%s).\n", strerror(errno));
- free(rec);
- return -1;
+ goto error;
}
/* fill the record */
p = strchr(fstring, ':');
if (!p) {
YLMSG_E("Invalid format of the features specification (%s).\n", fstring);
- return -1;
+ goto error;
}
rec->mod_name = strndup(fstring, p - fstring);
@@ -103,7 +98,7 @@
fp = realloc(rec->features, (count + 1) * sizeof *rec->features);
if (!fp) {
YLMSG_E("Unable to store features list information (%s).\n", strerror(errno));
- return -1;
+ goto error;
}
rec->features = fp;
fp = &rec->features[count++]; /* array item to set */
@@ -115,12 +110,23 @@
fp = realloc(rec->features, (count + 1) * sizeof *rec->features);
if (!fp) {
YLMSG_E("Unable to store features list information (%s).\n", strerror(errno));
- return -1;
+ goto error;
}
rec->features = fp;
rec->features[count++] = NULL;
+ /* Store record to the output set. */
+ if (ly_set_add(fset, rec, 1, NULL)) {
+ YLMSG_E("Unable to store features information (%s).\n", strerror(errno));
+ goto error;
+ }
+ rec = NULL;
+
return 0;
+
+error:
+ yl_schema_features_free(rec);
+ return -1;
}
void
diff --git a/tools/re/main.c b/tools/re/main.c
index 2292b2a..9fb1d01 100644
--- a/tools/re/main.c
+++ b/tools/re/main.c
@@ -88,22 +88,38 @@
add_pattern(char ***patterns, int **inverts, int *counter, char *pattern)
{
void *reallocated1, *reallocated2;
+ int orig_counter;
- (*counter)++;
- reallocated1 = realloc(*patterns, *counter * sizeof **patterns);
- reallocated2 = realloc(*inverts, *counter * sizeof **inverts);
- if (!reallocated1 || !reallocated2) {
- fprintf(stderr, "yangre error: memory allocation error.\n");
- free(reallocated1);
- free(reallocated2);
- return EXIT_FAILURE;
+ /* Store the original number of items. */
+ orig_counter = *counter;
+
+ /* Reallocate 'patterns' memory with additional space. */
+ reallocated1 = realloc(*patterns, (orig_counter + 1) * sizeof **patterns);
+ if (!reallocated1) {
+ goto error;
}
(*patterns) = reallocated1;
- (*patterns)[*counter - 1] = strdup(pattern);
+ /* Allocated memory is now larger. */
+ (*counter)++;
+ /* Copy the pattern and store it to the additonal space. */
+ (*patterns)[orig_counter] = strdup(pattern);
+ if (!(*patterns)[orig_counter]) {
+ goto error;
+ }
+
+ /* Reallocate 'inverts' memory with additional space. */
+ reallocated2 = realloc(*inverts, (orig_counter + 1) * sizeof **inverts);
+ if (!reallocated2) {
+ goto error;
+ }
(*inverts) = reallocated2;
- (*inverts)[*counter - 1] = 0;
+ (*inverts)[orig_counter] = 0;
return EXIT_SUCCESS;
+
+error:
+ fprintf(stderr, "yangre error: memory allocation error.\n");
+ return EXIT_FAILURE;
}
int