Skip to content

Commit 59ed1cb

Browse files
committed
Let us be careful with overflows.
1 parent 895450b commit 59ed1cb

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/nbit_array.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,24 @@ class UInt12Array {
5353
memcpy(&word, data + firstBytePos, sizeof(uint32_t));
5454
return word >> ((index & 1) << 2);
5555
}
56+
5657
void bulkSet(uint16_t* source, size_t length) {
57-
for(size_t i = 0, j = 0; i < length;) {
58+
size_t volume_needed = (length / 2)*3 + (length % 1) * 2;
59+
assert(volume_needed <= byteCount);
60+
size_t i = 0, j = 0;
61+
for(; i + 1 < length;) {
5862
uint32_t a = source[i++];
5963
uint32_t b = source[i++];
6064
data[j++] = (uint8_t) (a);
6165
data[j++] = (uint8_t) ((a >> 8) | (b << 4));
6266
data[j++] = (uint8_t) (b >> 4);
6367
}
68+
if(i < length) {
69+
uint32_t a = source[i++];
70+
uint32_t b = 0; // duh
71+
data[j++] = (uint8_t) (a);
72+
data[j++] = (uint8_t) ((a >> 8) | (b << 4));
73+
}
6474
}
6575
inline void set(size_t index, uint32_t value) {
6676
size_t wordpos = (index >> 1) * 3;

0 commit comments

Comments
 (0)