Skip to content

Commit 9a8dfa0

Browse files
committed
fix: save_compatibility_test failing on big-endian systems
save_compatibility_test was failing on big-endian systems, as it was written and tested on a little-endian system and savedata is not endianness portable[1]. [1] #2693
1 parent 86f5e55 commit 9a8dfa0

File tree

4 files changed

+20
-7
lines changed

4 files changed

+20
-7
lines changed

auto_tests/Makefile.inc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ endif
240240

241241

242242
EXTRA_DIST += \
243-
$(top_srcdir)/auto_tests/data/save.tox \
243+
$(top_srcdir)/auto_tests/data/save.tox.big \
244+
$(top_srcdir)/auto_tests/data/save.tox.little \
244245
$(top_srcdir)/auto_tests/check_compat.h \
245246
$(top_srcdir)/auto_tests/auto_test_support.h
Binary file not shown.

auto_tests/data/save.tox.little

4.44 KB
Binary file not shown.

auto_tests/save_compatibility_test.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
#include <stdlib.h>
1010
#include <string.h>
1111

12-
#define LOADED_SAVE_FILE "../auto_tests/data/save.tox"
12+
#define LOADED_SAVE_FILE_BIG "../auto_tests/data/save.tox.big"
13+
#define LOADED_SAVE_FILE_LITTLE "../auto_tests/data/save.tox.little"
1314

1415
// Information from the save file
1516
#define EXPECTED_NAME "name"
@@ -18,7 +19,7 @@
1819
#define EXPECTED_STATUS_MESSAGE_SIZE strlen(EXPECTED_STATUS_MESSAGE)
1920
#define EXPECTED_NUM_FRIENDS 1
2021
#define EXPECTED_NOSPAM "4C762C7D"
21-
#define EXPECTED_TOX_ID "B70E97D41F69B7F4C42A5BC7BD7A76B95B8030BE1B7C0E9E6FC19FC4ABEB195B4C762C7D800B"
22+
#define EXPECTED_TOX_ID "E776E9A993EE3CAE04F5946D9AC66FBBAA8C63A95A94B41942353C6DC1739B4B4C762C7DA7B9"
2223

2324
static size_t get_file_size(const char *save_path)
2425
{
@@ -135,6 +136,12 @@ static void test_save_compatibility(const char *save_path)
135136
tox_kill(tox);
136137
}
137138

139+
static bool is_little_endian(void)
140+
{
141+
uint16_t x = 1;
142+
return ((uint8_t *)&x)[0] == 1;
143+
}
144+
138145
// cppcheck-suppress constParameter
139146
int main(int argc, char *argv[])
140147
{
@@ -153,10 +160,15 @@ int main(int argc, char *argv[])
153160
base_path[strrchr(base_path, '/') - base_path] = '\0';
154161
}
155162

156-
char save_path[4096 + sizeof(LOADED_SAVE_FILE)];
157-
snprintf(save_path, sizeof(save_path), "%s/%s", base_path, LOADED_SAVE_FILE);
158-
159-
test_save_compatibility(save_path);
163+
if (is_little_endian()) {
164+
char save_path[4096 + sizeof(LOADED_SAVE_FILE_LITTLE)];
165+
snprintf(save_path, sizeof(save_path), "%s/%s", base_path, LOADED_SAVE_FILE_LITTLE);
166+
test_save_compatibility(save_path);
167+
} else {
168+
char save_path[4096 + sizeof(LOADED_SAVE_FILE_BIG)];
169+
snprintf(save_path, sizeof(save_path), "%s/%s", base_path, LOADED_SAVE_FILE_BIG);
170+
test_save_compatibility(save_path);
171+
}
160172

161173
return 0;
162174
}

0 commit comments

Comments
 (0)