Skip to content

Commit 1004292

Browse files
committed
Sanitize bloom filter macros
1 parent a913a20 commit 1004292

2 files changed

Lines changed: 36 additions & 14 deletions

File tree

‎Objects/stringlib/fastsearch.h‎

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,27 @@
1818
#define FAST_SEARCH 1
1919
#define FAST_RSEARCH 2
2020

21-
#define BLOOM_ADD(mask, ch) ((mask |= (1 << ((ch) & 0x1F))))
22-
#define BLOOM(mask, ch) ((mask & (1 << ((ch) & 0x1F))))
21+
#if LONG_BIT >= 128
22+
#define STRINGLIB_BLOOM_WIDTH 128
23+
#elif LONG_BIT >= 64
24+
#define STRINGLIB_BLOOM_WIDTH 64
25+
#elif LONG_BIT >= 32
26+
#define STRINGLIB_BLOOM_WIDTH 32
27+
#else
28+
#error "LONG_BIT is smaller than 32"
29+
#endif
30+
31+
#define STRINGLIB_BLOOM_ADD(mask, ch) \
32+
((mask |= (1UL << ((ch) & (STRINGLIB_BLOOM_WIDTH -1)))))
33+
#define STRINGLIB_BLOOM(mask, ch) \
34+
((mask & (1UL << ((ch) & (STRINGLIB_BLOOM_WIDTH -1)))))
2335

2436
Py_LOCAL_INLINE(Py_ssize_t)
2537
fastsearch(const STRINGLIB_CHAR* s, Py_ssize_t n,
2638
const STRINGLIB_CHAR* p, Py_ssize_t m,
2739
Py_ssize_t maxcount, int mode)
2840
{
29-
long mask;
41+
unsigned long mask;
3042
Py_ssize_t skip, count = 0;
3143
Py_ssize_t i, j, mlast, w;
3244

@@ -70,12 +82,12 @@ fastsearch(const STRINGLIB_CHAR* s, Py_ssize_t n,
7082

7183
/* process pattern[:-1] */
7284
for (i = 0; i < mlast; i++) {
73-
BLOOM_ADD(mask, p[i]);
85+
STRINGLIB_BLOOM_ADD(mask, p[i]);
7486
if (p[i] == p[mlast])
7587
skip = mlast - i - 1;
7688
}
7789
/* process pattern[-1] outside the loop */
78-
BLOOM_ADD(mask, p[mlast]);
90+
STRINGLIB_BLOOM_ADD(mask, p[mlast]);
7991

8092
for (i = 0; i <= w; i++) {
8193
/* note: using mlast in the skip path slows things down on x86 */
@@ -95,13 +107,13 @@ fastsearch(const STRINGLIB_CHAR* s, Py_ssize_t n,
95107
continue;
96108
}
97109
/* miss: check if next character is part of pattern */
98-
if (!BLOOM(mask, s[i+m]))
110+
if (!STRINGLIB_BLOOM(mask, s[i+m]))
99111
i = i + m;
100112
else
101113
i = i + skip;
102114
} else {
103115
/* skip: check if next character is part of pattern */
104-
if (!BLOOM(mask, s[i+m]))
116+
if (!STRINGLIB_BLOOM(mask, s[i+m]))
105117
i = i + m;
106118
}
107119
}
@@ -110,10 +122,10 @@ fastsearch(const STRINGLIB_CHAR* s, Py_ssize_t n,
110122
/* create compressed boyer-moore delta 1 table */
111123

112124
/* process pattern[0] outside the loop */
113-
BLOOM_ADD(mask, p[0]);
125+
STRINGLIB_BLOOM_ADD(mask, p[0]);
114126
/* process pattern[:0:-1] */
115127
for (i = mlast; i > 0; i--) {
116-
BLOOM_ADD(mask, p[i]);
128+
STRINGLIB_BLOOM_ADD(mask, p[i]);
117129
if (p[i] == p[0])
118130
skip = i - 1;
119131
}
@@ -128,13 +140,13 @@ fastsearch(const STRINGLIB_CHAR* s, Py_ssize_t n,
128140
/* got a match! */
129141
return i;
130142
/* miss: check if previous character is part of pattern */
131-
if (!BLOOM(mask, s[i-1]))
143+
if (!STRINGLIB_BLOOM(mask, s[i-1]))
132144
i = i - m;
133145
else
134146
i = i - skip;
135147
} else {
136148
/* skip: check if previous character is part of pattern */
137-
if (!BLOOM(mask, s[i-1]))
149+
if (!STRINGLIB_BLOOM(mask, s[i-1]))
138150
i = i - m;
139151
}
140152
}

‎Objects/unicodeobject.c‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,12 +190,22 @@ PyUnicode_GetMax(void)
190190

191191
/* the linebreak mask is set up by Unicode_Init below */
192192

193+
#if LONG_BIT >= 128
194+
#define BLOOM_WIDTH 128
195+
#elif LONG_BIT >= 64
196+
#define BLOOM_WIDTH 64
197+
#elif LONG_BIT >= 32
198+
#define BLOOM_WIDTH 32
199+
#else
200+
#error "LONG_BIT is smaller than 32"
201+
#endif
202+
193203
#define BLOOM_MASK unsigned long
194204

195205
static BLOOM_MASK bloom_linebreak;
196206

197-
#define BLOOM_ADD(mask, ch) ((mask |= (1 << ((ch) & (LONG_BIT - 1)))))
198-
#define BLOOM(mask, ch) ((mask & (1 << ((ch) & (LONG_BIT - 1)))))
207+
#define BLOOM_ADD(mask, ch) ((mask |= (1UL << ((ch) & (BLOOM_WIDTH - 1)))))
208+
#define BLOOM(mask, ch) ((mask & (1UL << ((ch) & (BLOOM_WIDTH - 1)))))
199209

200210
#define BLOOM_LINEBREAK(ch) \
201211
((ch) < 128U ? ascii_linebreak[(ch)] : \
@@ -205,7 +215,7 @@ Py_LOCAL_INLINE(BLOOM_MASK) make_bloom_mask(Py_UNICODE* ptr, Py_ssize_t len)
205215
{
206216
/* calculate simple bloom-style bitmask for a given unicode string */
207217

208-
long mask;
218+
BLOOM_MASK mask;
209219
Py_ssize_t i;
210220

211221
mask = 0;

0 commit comments

Comments
 (0)