From 02e2c46c50f79c6991424c2a62d96d2cc7593af5 Mon Sep 17 00:00:00 2001 From: Mo Yi Date: Sun, 3 Mar 2024 02:08:26 +0800 Subject: [PATCH] Fix misaligned address asan (#70) --- src/streamvbyte_x64_encode.c | 2 +- tests/unit.c | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/streamvbyte_x64_encode.c b/src/streamvbyte_x64_encode.c index c06bfc8..f9ef01c 100644 --- a/src/streamvbyte_x64_encode.c +++ b/src/streamvbyte_x64_encode.c @@ -88,7 +88,7 @@ static size_t streamvbyte_encode_SSE41 (const uint32_t* in, uint32_t count, uint uint32_t dw = in[i]; uint32_t symbol = (dw > 0x000000FF) + (dw > 0x0000FFFF) + (dw > 0x00FFFFFF); key |= symbol << (i + i); - *((uint32_t*)dataPtr) = dw; + memcpy(dataPtr, &dw, 4); dataPtr += 1 + symbol; } memcpy(keyPtr, &key, ((count & 7) + 3) >> 2); diff --git a/tests/unit.c b/tests/unit.c index beaacd5..e3d23a4 100644 --- a/tests/unit.c +++ b/tests/unit.c @@ -2759,8 +2759,31 @@ static bool issue42(void) { return true; } + +static bool issue69(void) { + uint32_t N = 22; + uint32_t test_misaligned[22] = { + 431, 292, 979, 994, 761, 879, 672, 690, 296, + 931, 379, 98, 132, 105, 116, 841, 387, 831, + 335, 333, 557, 915 + }; + uint32_t* datain = malloc(N * sizeof(uint32_t)); + uint8_t* compressedbuffer = malloc(streamvbyte_max_compressedbytes(N)); + uint32_t* recovdata = malloc(N * sizeof(uint32_t)); + for (uint32_t k = 0; k < N; ++k) datain[k] = test_misaligned[k]; + size_t compsize = streamvbyte_encode(datain, N, compressedbuffer); // encoding + // here the result is stored in compressedbuffer using compsize bytes + size_t compsize2 = streamvbyte_decode(compressedbuffer, recovdata, N); // decoding (fast) + if (compsize != compsize2) return false; + free(datain); + free(compressedbuffer); + free(recovdata); + return true; +} + int main(void) { if (!issue42()) { printf("tests failed.\n"); return EXIT_FAILURE; } + if (!issue69()) { printf("tests failed.\n"); return EXIT_FAILURE; } if (zigzagtests() == -1) { printf("tests failed.\n"); return EXIT_FAILURE; } if (basictests() == -1) { printf("tests failed.\n"); return EXIT_FAILURE; } if (aqrittests() == -1) { printf("tests failed.\n"); return EXIT_FAILURE; }