Skip to content

Commit 1ade7d8

Browse files
committed
xdiff: use unambiguous types in xdl_hash_record()
Convert the function signature and body to use unambiguous types. char is changed to uint8_t because this function processes bytes in memory. unsigned long to uint64_t so that the hash output is consistent across platforms. `flags` was changed from long to uint64_t to ensure the high order bits are not dropped on platforms that treat long as 32 bits. Signed-off-by: Ezekiel Newren <ezekielnewren@gmail.com>
1 parent 07e28aa commit 1ade7d8

4 files changed

Lines changed: 21 additions & 21 deletions

File tree

xdiff-interface.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ void xdiff_clear_find_func(xdemitconf_t *xecfg)
300300

301301
unsigned long xdiff_hash_string(const char *s, size_t len, long flags)
302302
{
303-
return xdl_hash_record(&s, s + len, flags);
303+
return xdl_hash_record((uint8_t const**)&s, (uint8_t const*)s + len, flags);
304304
}
305305

306306
int xdiff_compare_lines(const char *l1, long s1,

xdiff/xprepare.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ static void xdl_free_ctx(xdfile_t *xdf)
137137
static int xdl_prepare_ctx(unsigned int pass, mmfile_t *mf, long narec, xpparam_t const *xpp,
138138
xdlclassifier_t *cf, xdfile_t *xdf) {
139139
long bsize;
140-
unsigned long hav;
141-
char const *blk, *cur, *top, *prev;
140+
uint64_t hav;
141+
uint8_t const *blk, *cur, *top, *prev;
142142
xrecord_t *crec;
143143

144144
xdf->rindex = NULL;
@@ -156,7 +156,7 @@ static int xdl_prepare_ctx(unsigned int pass, mmfile_t *mf, long narec, xpparam_
156156
if (XDL_ALLOC_GROW(xdf->recs, xdf->nrec + 1, narec))
157157
goto abort;
158158
crec = &xdf->recs[xdf->nrec++];
159-
crec->ptr = (uint8_t const *)prev;
159+
crec->ptr = prev;
160160
crec->size = cur - prev;
161161
crec->ha = hav;
162162
if (xdl_classify_record(pass, cf, crec) < 0)

xdiff/xutils.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -249,11 +249,11 @@ int xdl_recmatch(const char *l1, long s1, const char *l2, long s2, long flags)
249249
return 1;
250250
}
251251

252-
unsigned long xdl_hash_record_with_whitespace(char const **data,
253-
char const *top, long flags) {
254-
unsigned long ha = 5381;
255-
char const *ptr = *data;
256-
int cr_at_eol_only = (flags & XDF_WHITESPACE_FLAGS) == XDF_IGNORE_CR_AT_EOL;
252+
uint64_t xdl_hash_record_with_whitespace(uint8_t const **data,
253+
uint8_t const *top, uint64_t flags) {
254+
uint64_t ha = 5381;
255+
uint8_t const *ptr = *data;
256+
bool cr_at_eol_only = (flags & XDF_WHITESPACE_FLAGS) == XDF_IGNORE_CR_AT_EOL;
257257

258258
for (; ptr < top && *ptr != '\n'; ptr++) {
259259
if (cr_at_eol_only) {
@@ -263,8 +263,8 @@ unsigned long xdl_hash_record_with_whitespace(char const **data,
263263
continue;
264264
}
265265
else if (XDL_ISSPACE(*ptr)) {
266-
const char *ptr2 = ptr;
267-
int at_eol;
266+
const uint8_t *ptr2 = ptr;
267+
bool at_eol;
268268
while (ptr + 1 < top && XDL_ISSPACE(ptr[1])
269269
&& ptr[1] != '\n')
270270
ptr++;
@@ -274,20 +274,20 @@ unsigned long xdl_hash_record_with_whitespace(char const **data,
274274
else if (flags & XDF_IGNORE_WHITESPACE_CHANGE
275275
&& !at_eol) {
276276
ha += (ha << 5);
277-
ha ^= (unsigned long) ' ';
277+
ha ^= (uint64_t) ' ';
278278
}
279279
else if (flags & XDF_IGNORE_WHITESPACE_AT_EOL
280280
&& !at_eol) {
281281
while (ptr2 != ptr + 1) {
282282
ha += (ha << 5);
283-
ha ^= (unsigned long) *ptr2;
283+
ha ^= (uint64_t) *ptr2;
284284
ptr2++;
285285
}
286286
}
287287
continue;
288288
}
289289
ha += (ha << 5);
290-
ha ^= (unsigned long) *ptr;
290+
ha ^= (uint64_t) *ptr;
291291
}
292292
*data = ptr < top ? ptr + 1: ptr;
293293

@@ -304,17 +304,17 @@ unsigned long xdl_hash_record_with_whitespace(char const **data,
304304
#define REASSOC_FENCE(x, y)
305305
#endif
306306

307-
unsigned long xdl_hash_record_verbatim(char const **data, char const *top) {
308-
unsigned long ha = 5381, c0, c1;
309-
char const *ptr = *data;
307+
uint64_t xdl_hash_record_verbatim(uint8_t const **data, uint8_t const *top) {
308+
uint64_t ha = 5381, c0, c1;
309+
uint8_t const *ptr = *data;
310310
#if 0
311311
/*
312312
* The baseline form of the optimized loop below. This is the djb2
313313
* hash (the above function uses a variant with XOR instead of ADD).
314314
*/
315315
for (; ptr < top && *ptr != '\n'; ptr++) {
316316
ha += (ha << 5);
317-
ha += (unsigned long) *ptr;
317+
ha += (uint64_t) *ptr;
318318
}
319319
*data = ptr < top ? ptr + 1: ptr;
320320
#else

xdiff/xutils.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ void *xdl_cha_alloc(chastore_t *cha);
3434
long xdl_guess_lines(mmfile_t *mf, long sample);
3535
int xdl_blankline(const char *line, long size, long flags);
3636
int xdl_recmatch(const char *l1, long s1, const char *l2, long s2, long flags);
37-
unsigned long xdl_hash_record_verbatim(char const **data, char const *top);
38-
unsigned long xdl_hash_record_with_whitespace(char const **data, char const *top, long flags);
39-
static inline unsigned long xdl_hash_record(char const **data, char const *top, long flags)
37+
uint64_t xdl_hash_record_verbatim(uint8_t const **data, uint8_t const *top);
38+
uint64_t xdl_hash_record_with_whitespace(uint8_t const **data, uint8_t const *top, uint64_t flags);
39+
static inline uint64_t xdl_hash_record(uint8_t const **data, uint8_t const *top, uint64_t flags)
4040
{
4141
if (flags & XDF_WHITESPACE_FLAGS)
4242
return xdl_hash_record_with_whitespace(data, top, flags);

0 commit comments

Comments
 (0)