Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions crcfactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#ifndef __CRCFACTORY_H__
#define __CRCFACTORY_H__

#include <limits.h>
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
Expand Down Expand Up @@ -101,10 +102,13 @@ CRCFACTORY_INLINE CRCFACTORY_CRC_TYPE _crcfactory_mask(int width, bool reflected
if (!reflected && width <= 8) {
return value & 0xff;
}
// The following if statement is due to a bug under x86_64:
// "1ULL << 64" evaluates to 0 (expected), but
// "1ULL << x" when x==64 evaluates to 1 (wrong).
if ((1ULL << width) == 1) return value;

// Avoid undefined behavior if a shift to generate the mask would exceed
// the size of the type. Just return the value unmasked, since the mask
// would cover all bits anyway. Strictly speaking we probably want a
// limits-based check here in case CRC_TYPE is signed, but signed types
// would likely break other things anyway.
if ((size_t)width >= (CHAR_BIT * sizeof(CRCFACTORY_CRC_TYPE))) return value;
return value & ((1ULL << width) - 1);
}

Expand Down
3 changes: 2 additions & 1 deletion test/crclist2test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
CFILE_TEMPLATE = r"""
/* NOTE: This file was auto-generated by crclist2test.py */
#include <stdio.h>
#include <inttypes.h>

#define CRCFACTORY_CRC_TYPE <<crc_datatype>>
#define CRCFACTORY_CRCTABLE_TYPE <<table_datatype>>
Expand All @@ -20,7 +21,7 @@
func##_t_init(crctable); \
uint64_t r = func((uint8_t *)TESTDATA, TESTDATA_LEN); \
uint64_t r_t = func##_t(crctable, (uint8_t *)TESTDATA, TESTDATA_LEN); \
printf("%-20s: %-16lx %-6s : %-16lx %-6s\n", desc, r, (r == check) ? "(OK)" : "(FAIL)", r_t, (r_t == check) ? "(OK)" : "(FAIL)"); \
printf("%-20s: %-16" PRIx64 " %-6s : %-16" PRIx64 " %-6s\n", desc, r, (r == check) ? "(OK)" : "(FAIL)", r_t, (r_t == check) ? "(OK)" : "(FAIL)"); \
test_count += 2; \
test_failures += (r == check) ? 0 : 1; \
test_failures += (r_t == check) ? 0 : 1; \
Expand Down