Skip to content

Commit 43bda99

Browse files
committed
Fix 32-bit portability issues in NIF code
1. Use uintptr_t instead of uint64_t for pointer tagging macros (IS_ENTRY_LIST, GET_ENTRY_LIST_POINTER, MAKE_ENTRY_LIST_POINTER). Fixes warnings on 32-bit: ``` gcc -O2 -flto=auto -ffat-lto-objects -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-U_FORTIFY_SOURCE,-D_FORTIFY_SOURCE=3 -Wp,-D_GLIBCXX_ASSERTIONS -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -fstack-protector-strong -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m32 -march=i686 -mtune=generic -msse2 -mfpmath=sse -mstackrealign -fasynchronous-unwind-tables -fstack-clash-protection -mtls-dialect=gnu -c -I/usr/lib/erlang/usr/include c_src/bitcask_nifs.c -o c_src/bitcask_nifs.o c_src/bitcask_nifs.c: In function ‘keydir_entry_hash’: c_src/bitcask_nifs.c:190:27: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast] 190 | #define IS_ENTRY_LIST(p) ((uint64_t)p&1) | ^ ``` 2. Use correct type for enif_get_ulong() - it expects unsigned long*, not size_t*, which may differ on some platforms. Signed-off-by: Peter Lemenkov <lemenkov@gmail.com> Assisted-by: Claude (Anthropic) <https://claude.ai>
1 parent 8c12bc5 commit 43bda99

1 file changed

Lines changed: 6 additions & 5 deletions

File tree

c_src/bitcask_nifs.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,9 @@ typedef struct
187187
// An entry pointer may be tagged to indicate it really points to an
188188
// list of entries with different timestamps. Those are created when
189189
// there are concurrent iterations and updates.
190-
#define IS_ENTRY_LIST(p) ((uint64_t)p&1)
191-
#define GET_ENTRY_LIST_POINTER(p) ((bitcask_keydir_entry_head*)((uint64_t)p&(uint64_t)~1))
192-
#define MAKE_ENTRY_LIST_POINTER(p) ((bitcask_keydir_entry*)((uint64_t)p|(uint64_t)1))
190+
#define IS_ENTRY_LIST(p) ((uintptr_t)p&1)
191+
#define GET_ENTRY_LIST_POINTER(p) ((bitcask_keydir_entry_head*)((uintptr_t)p&(uintptr_t)~1))
192+
#define MAKE_ENTRY_LIST_POINTER(p) ((bitcask_keydir_entry*)((uintptr_t)p|(uintptr_t)1))
193193

194194
// Holds values fetched from a regular entry or a snapshot from an entry list.
195195
typedef struct
@@ -2494,12 +2494,13 @@ ERL_NIF_TERM bitcask_nifs_file_pwrite(ErlNifEnv* env, int argc, const ERL_NIF_TE
24942494
ERL_NIF_TERM bitcask_nifs_file_read(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
24952495
{
24962496
bitcask_file_handle* handle;
2497-
size_t count;
2497+
unsigned long count_ul;
24982498

24992499
if (enif_get_resource(env, argv[0], bitcask_file_RESOURCE, (void**)&handle) &&
2500-
enif_get_ulong(env, argv[1], &count)) /* Count */
2500+
enif_get_ulong(env, argv[1], &count_ul)) /* Count */
25012501
{
25022502
ErlNifBinary bin;
2503+
size_t count = count_ul;
25032504
if (!enif_alloc_binary(count, &bin))
25042505
{
25052506
return enif_make_tuple2(env, ATOM_ERROR, ATOM_ALLOCATION_ERROR);

0 commit comments

Comments
 (0)