Skip to content

Commit e014a7a

Browse files
committed
[endedness] Fix problem on bigendians with 'capture_parameter'
On bigendian architectures a value with fewer bytes, if stored in a value, or as in this case a union, are stored towards the upper end of the larger value. This is in contrast to little-endians which will store them at the lower end.
1 parent a338906 commit e014a7a

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/constraint.c

+14-2
Original file line numberDiff line numberDiff line change
@@ -601,15 +601,27 @@ static void execute_sideeffect(Constraint *constraint, const char *function, Cgr
601601
(constraint->side_effect_callback)(constraint->side_effect_data);
602602
}
603603

604+
#define IS_BIG_ENDIAN (!*(unsigned char *)&(uint16_t){1})
605+
static bool bigendian(void) { return IS_BIG_ENDIAN; }
606+
604607
static void capture_parameter(Constraint *constraint, const char *function, CgreenValue actual,
605608
const char *test_file, int test_line, TestReporter *reporter) {
606609
(void)function;
607610
(void)test_file;
608611
(void)test_line;
609612
(void)reporter;
610613

611-
memmove(constraint->expected_value.value.pointer_value, &actual.value, constraint->size_of_expected_value);
612-
}
614+
if ((sizeof(intptr_t) != constraint->size_of_expected_value) && bigendian()) {
615+
// Then the beginning of a smaller value is not stored at the beginning of the actual.value union
616+
size_t offset = sizeof(intptr_t) - constraint->size_of_expected_value;
617+
// Offset is in bytes so we need to cast &actual.value to that before adding the offset
618+
void *start_address = (unsigned char *)&actual.value + offset;
619+
memmove(constraint->expected_value.value.pointer_value, start_address, constraint->size_of_expected_value);
620+
621+
} else
622+
memmove(constraint->expected_value.value.pointer_value, &actual.value,
623+
constraint->size_of_expected_value);
624+
}
613625

614626

615627
void test_want(Constraint *constraint, const char *function, CgreenValue actual,

tests/mocks_tests.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -440,9 +440,9 @@ Ensure(Mocks, can_capture_parameter) {
440440
expect(simple_mocked_function,
441441
will_capture_parameter(first, captured_first),
442442
will_capture_parameter(second, captured_second));
443-
simple_mocked_function(1, 2);
444-
assert_that(captured_first, is_equal_to(1));
445-
assert_that(captured_second, is_equal_to(2));
443+
simple_mocked_function(0x12345678, 0x76543210);
444+
assert_that(captured_first, is_equal_to_hex(0x12345678));
445+
assert_that(captured_second, is_equal_to_hex(0x76543210));
446446
}
447447

448448
static int changed_by_sideeffect = 1;

0 commit comments

Comments
 (0)