commit 44cf49504c18b4bb2edd3518b6e1192b5e4f4416 from: Neels Hofmeyr date: Sun Sep 20 14:13:07 2020 UTC patience: error handling from diff_atoms_same() commit - ac2eeeffef0b2071f39513685c4b6d594a30416d commit + 44cf49504c18b4bb2edd3518b6e1192b5e4f4416 blob - 79fe3c4b8682eaf5665b62bb3c1e6d8ebb7bc745 blob + 60517c0a36ecf3978e948dc526f31cae3892f0b2 --- lib/diff_patience.c +++ lib/diff_patience.c @@ -29,7 +29,7 @@ #include "debug.h" /* Set unique_here = true for all atoms that exist exactly once in this list. */ -static void +static int diff_atoms_mark_unique(struct diff_data *d, unsigned int *unique_count) { struct diff_atom *i; @@ -49,7 +49,7 @@ diff_atoms_mark_unique(struct diff_data *d, unsigned i bool same; int r = diff_atom_same(&same, i, j); if (r) - abort(); // TODO: error handling + return r; if (!same) continue; if (i->patience.unique_here) { @@ -64,11 +64,12 @@ diff_atoms_mark_unique(struct diff_data *d, unsigned i } if (unique_count) *unique_count = count; + return 0; } /* Mark those lines as atom->patience.unique_in_both = true that appear exactly * once in each side. */ -static void +static int diff_atoms_mark_unique_in_both(struct diff_data *left, struct diff_data *right, unsigned int *unique_in_both_count) { @@ -76,9 +77,14 @@ diff_atoms_mark_unique_in_both(struct diff_data *left, * iteration. So this is just some optimiziation to save one iteration * in the end. */ unsigned int unique_in_both; + int r; - diff_atoms_mark_unique(left, &unique_in_both); - diff_atoms_mark_unique(right, NULL); + r = diff_atoms_mark_unique(left, &unique_in_both); + if (r) + return r; + r = diff_atoms_mark_unique(right, NULL); + if (r) + return r; debug("unique_in_both %u\n", unique_in_both); @@ -92,7 +98,7 @@ diff_atoms_mark_unique_in_both(struct diff_data *left, bool same; int r = diff_atom_same(&same, i, j); if (r) - abort(); // TODO: error handling + return r; if (!same) continue; if (!j->patience.unique_here) { @@ -129,7 +135,7 @@ diff_atoms_mark_unique_in_both(struct diff_data *left, continue; r = diff_atom_same(&same, i, j); if (r) - abort(); // TODO: error handling + return r; if (!same) continue; found_in_a = true; @@ -142,9 +148,10 @@ diff_atoms_mark_unique_in_both(struct diff_data *left, if (unique_in_both_count) *unique_in_both_count = unique_in_both; + return 0; } -static void +static int diff_atoms_swallow_identical_neighbors(struct diff_data *left, struct diff_data *right, unsigned int *unique_in_both_count) @@ -187,7 +194,7 @@ diff_atoms_swallow_identical_neighbors(struct diff_dat &left->atoms.head[identical_l.start - 1], &right->atoms.head[identical_r.start - 1]); if (r) - abort(); // TODO: error handling + return r; if (!same) break; } @@ -205,7 +212,7 @@ diff_atoms_swallow_identical_neighbors(struct diff_dat &left->atoms.head[identical_l.end], &right->atoms.head[identical_r.end]); if (r) - abort(); // TODO: error handling + return r; if (!same) break; l_end = &left->atoms.head[identical_l.end]; @@ -235,6 +242,7 @@ diff_atoms_swallow_identical_neighbors(struct diff_dat } debug("next_l_idx = %u\n", next_l_idx); } + return 0; } /* binary search to find the stack to put this atom "card" on. */ @@ -268,7 +276,7 @@ int diff_algo_patience(const struct diff_algo_config *algo_config, struct diff_state *state) { - int rc = ENOMEM; + int rc; struct diff_data *left = &state->left; struct diff_data *right = &state->right; @@ -279,7 +287,9 @@ diff_algo_patience(const struct diff_algo_config *algo /* Find those lines that appear exactly once in 'left' and exactly once * in 'right'. */ - diff_atoms_mark_unique_in_both(left, right, &unique_in_both_count); + rc = diff_atoms_mark_unique_in_both(left, right, &unique_in_both_count); + if (rc) + return rc; debug("unique_in_both_count %u\n", unique_in_both_count); debug("left:\n"); @@ -293,11 +303,15 @@ diff_algo_patience(const struct diff_algo_config *algo return DIFF_RC_USE_DIFF_ALGO_FALLBACK; } - diff_atoms_swallow_identical_neighbors(left, right, - &unique_in_both_count); + rc = diff_atoms_swallow_identical_neighbors(left, right, + &unique_in_both_count); + if (rc) + return rc; debug("After swallowing identical neighbors: unique_in_both = %u\n", unique_in_both_count); + rc = ENOMEM; + /* An array of Longest Common Sequence is the result of the below * subscope: */ unsigned int lcs_count = 0;