Skip to content

Commit

Permalink
Debug 24-bit int endianess
Browse files Browse the repository at this point in the history
  • Loading branch information
chances committed Jul 3, 2024
1 parent fcda27f commit 8040587
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/dbpf/types.d
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ align(1):

///
int value() inout @property {
int val = *cast(int*)&payload & 0xFFFFFF;
if (val & 0x800000)
val |= 0xFF000000;
int val;
version(BigEndian) val = (payload[0] << 16) | (payload[1] << 8) | payload[2];
else val = (payload[2] << 16) | (payload[1] << 8) | payload[0];
return val;
}

Expand Down Expand Up @@ -103,22 +103,24 @@ static assert(int24.sizeof == 3);
static assert(int24.alignof == 1);

unittest {
import std.conv : text;

int24[3] a;
assert(a.sizeof == 9);

// Max value
a[1] = 8_388_607;
assert(a[1] == 8_388_607);
// Test for buffer overflow:
assert(a[0] == 0);
assert(a[2] == 0);
assert(a[0] == 0, a[0].text);
assert(a[2] == 0, a[2].text);

// Overflow
a[1] = 8_388_608;
assert(a[1] == -8_388_608);
assert(a[1] == -8_388_608, a[1].text);
// Test for buffer overflow:
assert(a[0] == 0);
assert(a[2] == 0);
assert(a[0] == 0, a[0].text);
assert(a[2] == 0, a[2].text);

// Negative value
a[1] = -1;
Expand Down

0 comments on commit 8040587

Please sign in to comment.