yangre BUGFIX in add_pattern()
Memory problems in case of error.
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