Skip to content

Commit

Permalink
Allow bases=NULL for fsm_union_array()
Browse files Browse the repository at this point in the history
  • Loading branch information
katef committed Apr 26, 2024
1 parent 993888e commit d80644a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
2 changes: 1 addition & 1 deletion include/fsm/bool.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fsm_union(struct fsm *a, struct fsm *b,
* starting states and (if used) capture bases.
*
* bases[] is expected to have the same count as fsms[], and
* will be initialized by the function.
* will be initialized by the function. May be NULL.
*
* On success, returns the unioned fsm and updates bases[].
* On failure, returns NULL; all fsms are freed. */
Expand Down
19 changes: 14 additions & 5 deletions src/libfsm/union.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ fsm_union_array(size_t fsm_count,
struct fsm *res = fsms[0];

fsms[0] = NULL;
memset(bases, 0x00, fsm_count * sizeof(bases[0]));
if (bases != NULL) {
memset(bases, 0x00, fsm_count * sizeof(bases[0]));
}

for (i = 1; i < fsm_count; i++) {
struct fsm_combine_info ci;
Expand All @@ -117,9 +119,14 @@ fsm_union_array(size_t fsm_count,
return NULL;
}

res = combined;

if (bases == NULL) {
continue;
}

bases[i].state = ci.base_b;
bases[i].capture = ci.capture_base_b;
res = combined;

/* If the first argument didn't get its states put first
* in the union, then shift the bases for everything
Expand All @@ -134,9 +141,11 @@ fsm_union_array(size_t fsm_count,
}

#if LOG_UNION_ARRAY
for (i = 0; i < fsm_count; i++) {
fprintf(stderr, "union_array: bases %u: %zu, %zu\n",
i, bases[i].state, bases[i].capture);
if (bases != NULL) {
for (i = 0; i < fsm_count; i++) {
fprintf(stderr, "union_array: bases %u: %zu, %zu\n",
i, bases[i].state, bases[i].capture);
}
}
#endif

Expand Down

0 comments on commit d80644a

Please sign in to comment.