Skip to content

Commit

Permalink
bxs*: fix overflows and add assertions
Browse files Browse the repository at this point in the history
first = -1 overflows
  • Loading branch information
rurban committed Mar 31, 2024
1 parent ffa42bc commit 92a79ee
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 22 deletions.
6 changes: 3 additions & 3 deletions source/algocfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,9 @@ const struct algocfg ALGOCFGS[] = {
[_BSDM6] = {_BSDM6, GOOD, ASAN, UNSATISFIABLE, 6, 0, 256, 256},
[_BSDM7] = {_BSDM7, GOOD, ASAN, VFY_TIMEOUT, 7, 0, 256, 256},
[_BSDM8] = {_BSDM8, GOOD, ASAN, UNSATISFIABLE, 8, 0, 256, 256},
[_BXS] = {_BXS, FAIL, FAIL, VFY_TIMEOUT, 0, 0, 256, 256},
[_BXS1] = {_BXS1, GOOD, ASAN, VFY_PASS, 1, 0, 256, 256},
[_BXS2] = {_BXS2, GOOD, ASAN, VFY_PASS, 2, 0, 256, 256},
[_BXS] = {_BXS, FAIL, FAIL, VFY_TIMEOUT, 2, 0, 256, 256},
[_BXS1] = {_BXS1, GOOD, ASAN, VFY_PASS, 0, 0, 256, 256},
[_BXS2] = {_BXS2, GOOD, ASAN, VFY_PASS, 3, 0, 256, 256},
[_BXS3] = {_BXS3, GOOD, ASAN, VFY_PASS, 3, 0, 256, 256},
[_BXS4] = {_BXS4, GOOD, ASAN, VFY_PASS, 4, 0, 256, 256},
[_BXS6] = {_BXS6, GOOD, ASAN, VFY_PASS, 6, 0, 256, 256},
Expand Down
6 changes: 3 additions & 3 deletions source/algorithms.h
Original file line number Diff line number Diff line change
Expand Up @@ -405,9 +405,9 @@ const struct algo ALGOS[] = {
[_BSDM6] = {_BSDM6, OK, "bsdm6", "Backward SNR DAWG Matching (m>=6)", 6, 0},
[_BSDM7] = {_BSDM7, OK, "bsdm7", "Backward SNR DAWG Matching (m>=7)", 7, 0},
[_BSDM8] = {_BSDM8, OK, "bsdm8", "Backward SNR DAWG Matching (m>=8)", 8, 0},
[_BXS] = {_BXS, FAIL, "bxs", "BNDMq with eXtended Shift", 0, 0}, // m>=2
[_BXS1] = {_BXS1, OK, "bxs1", "BXS with q-grams limit", 1, 0},
[_BXS2] = {_BXS2, OK, "bxs2", "BXS with q-grams limit", 2, 0},
[_BXS] = {_BXS, FAIL, "bxs", "BNDMq with eXtended Shift", 2, 0}, // m>=2
[_BXS1] = {_BXS1, OK, "bxs1", "BXS with q-grams limit", 0, 0},
[_BXS2] = {_BXS2, OK, "bxs2", "BXS with q-grams limit", 3, 0},
[_BXS3] = {_BXS3, OK, "bxs3", "BXS with q-grams limit", 3, 0},
[_BXS4] = {_BXS4, OK, "bxs4", "BXS with q-grams limit", 4, 0},
[_BXS6] = {_BXS6, OK, "bxs6", "BXS with q-grams limit", 6, 0},
Expand Down
14 changes: 9 additions & 5 deletions source/algos/bxs.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
* Note: Original crashed with m=2: bin/asan/bxs aa 2 aa 2
*/

#define MIN_M 2
#include <assert.h>
#include "include/define.h"
#include "include/main.h"
#include "include/search_small.h"
Expand All @@ -34,7 +36,7 @@
int search(unsigned char *x, int m, unsigned char *y, int n) {
unsigned int B[SIGMA] = {0}, D, set;
int i, j, first, k, count;
if (m <= 2)
if (m <= Q)
return search_small(x, m, y, n);
int len = m;
if (m > WORD)
Expand All @@ -57,11 +59,13 @@ int search(unsigned char *x, int m, unsigned char *y, int n) {

BEGIN_SEARCHING
/* Searching */
for (i = mq1; i < nq1; i += mq1) {
for (i = mq1 - 1; i <= nq1; i += mq1) {
assert(i >= 0);
assert(i <= n);
D = B[y[i]];
if (D) {
j = i;
first = i - mq1;
first = i - mq1; // -1
do {
j--;
if (D >= mask) {
Expand All @@ -73,9 +77,9 @@ int search(unsigned char *x, int m, unsigned char *y, int n) {
if (k == 0)
OUTPUT(first + 1);
}
D = ((D << 1) | 1) & B[y[j]];
D = ((D << 1) | 1) & B[y[j + 1]];
} else
D = (D << 1) & B[y[j]];
D = (D << 1) & B[y[j + 1]];
} while (D && j > first);
}
}
Expand Down
8 changes: 4 additions & 4 deletions source/algos/bxs1.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* International Symposium on Experimental Algorithms (SEA 2010)
* Q is the dimension of q-grams
*
* Constraints: requires m>=1. inexact m>32
* Constraints: requires m>=1.
*/

#include "include/define.h"
Expand Down Expand Up @@ -68,13 +68,13 @@ int search(unsigned char *x, int m, unsigned char *y, int n) {
do {
j--;
if (D >= mask) {
if (j - first)
if (j > first)
i = j;
else {
for (k = m; y[first + k] == x[k - 1] && (k); k--)
for (k = m; k && (y[first + k] == x[k - 1]); k--)
;
if (k == 0)
count++;
OUTPUT(first);
}
D = ((D << 1) | 1) & B[y[j]];
} else
Expand Down
3 changes: 2 additions & 1 deletion source/algos/bxs2.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
* Note: Original crashed with m=2: bin/asan/bxs2 aa 2 aa 2
*/

#define MIN_M 3
#include "include/define.h"
#include "include/main.h"
#include "include/search_small.h"
Expand Down Expand Up @@ -69,7 +70,7 @@ int search(unsigned char *x, int m, unsigned char *y, int n) {
do {
j--;
if (D >= mask) {
if (j - first)
if (j > first)
i = j;
else {
//assert(first + len < n);
Expand Down
7 changes: 4 additions & 3 deletions source/algos/bxs3.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* Constraints: requires m>=3. inexact m>32
*/

#define MIN_M 3
#include "include/define.h"
#include "include/main.h"
#include "include/search_small.h"
Expand Down Expand Up @@ -68,13 +69,13 @@ int search(unsigned char *x, int m, unsigned char *y, int n) {
do {
j--;
if (D >= mask) {
if (j - first)
if (j > first)
i = j;
else {
for (k = m; y[first + k] == x[k - 1] && (k); k--)
for (k = m; k && (y[k + first] == x[k - 1]); k--)
;
if (k == 0)
count++;
OUTPUT(first + 1);
}
D = ((D << 1) | 1) & B[y[j]];
} else
Expand Down
3 changes: 2 additions & 1 deletion source/algos/bxs4.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* Constraints: requires m>=4.
*/

#define MIN_M 4
#include "include/define.h"
#include "include/main.h"
#include "include/search_small.h"
Expand Down Expand Up @@ -70,7 +71,7 @@ int search(unsigned char *x, int m, unsigned char *y, int n) {
do {
j--;
if (D >= mask) {
if (j - first)
if (j > first)
i = j;
else {
for (k = len; k && (y[first + k] == x[k - 1]); k--)
Expand Down
3 changes: 2 additions & 1 deletion source/algos/bxs6.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* Constraints: requires m>=6
*/

#define MIN_M 6
#include "include/define.h"
#include "include/main.h"
#include "include/search_small.h"
Expand Down Expand Up @@ -73,7 +74,7 @@ int search(unsigned char *x, int m, unsigned char *y, int n) {
do {
j--;
if (D >= mask) {
if (j - first)
if (j > first)
i = j;
else {
for (k = len; k && (y[first + k] == x[k - 1]); k--)
Expand Down
4 changes: 3 additions & 1 deletion source/algos/bxs8.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
* Constraints: requires m>=8
*/

#define MIN_M 8
#include <assert.h>
#include "include/define.h"
#include "include/main.h"
#include "include/search_small.h"
Expand Down Expand Up @@ -75,7 +77,7 @@ int search(unsigned char *x, int m, unsigned char *y, int n) {
do {
j--;
if (D >= mask) {
if (j - first)
if (j > first)
i = j;
else {
for (k = len; k && (y[first + k] == x[k - 1]); k--)
Expand Down

0 comments on commit 92a79ee

Please sign in to comment.