tskit-dev / kastore

The key-array store for read-only numerical data.
MIT License
2 stars 7 forks source link

undefined behavior sanitizer dislikes (1 << 31) #153

Closed bhaller closed 2 years ago

bhaller commented 2 years ago

UndefinedBehaviorSanitizer complains:

kastore.c:654:46: runtime error: left shift of 1 by 31 places cannot be represented in type 'int'

The relevant line:

if (self->file != NULL && (self->flags & OWN_FILE)) {

The relevant define:

#define OWN_FILE (1 << 31)

The 1 here should be unsigned, I guess, or a shift of less than 31 should be used. I didn't realize that this was undefined behavior in C, but apparently it is?

jeromekelleher commented 2 years ago

Huh, thanks @bhaller. Ah, the compiler is right here, 1<<31 is INT32_MAX + 1. Doh. So, this is probably a bug on 32 bit platforms.

jeromekelleher commented 2 years ago

Simple solution is just use 1<<30, no big deal.

bhaller commented 2 years ago

Yeah, it shifts into the sign bit. Probably unsigned should be used for all flag values, since one never really wants to be carrying flags around in signed ints...

jeromekelleher commented 2 years ago

Yeah, that's true.