lemire / streamvbyte

Fast integer compression in C using the StreamVByte codec
Apache License 2.0
374 stars 37 forks source link

runtime error: store to misaligned address #69

Closed mmooyyii closed 7 months ago

mmooyyii commented 7 months ago

#include <stdio.h>
#include <stdlib.h>

#include <cassert>
#include <vector>

#include "thirdparty/streamvbyte/include/streamvbyte.h"

auto main(int argc, char* argv[]) -> int {
    auto test = std::vector<uint32_t>{431, 292, 979, 994, 761, 879, 672, 690, 296, 931, 379,
                                   98,  132, 105, 116, 841, 387, 831, 335, 333, 557, 915};

    // streamvbyte/examples/example.c
    uint32_t N = test.size();
    uint32_t* datain = static_cast<uint32_t*>(malloc(N * sizeof(uint32_t)));
    uint8_t* compressedbuffer = static_cast<uint8_t*>(malloc(streamvbyte_max_compressedbytes(N)));
    uint32_t* recovdata = static_cast<uint32_t*>(malloc(N * sizeof(uint32_t)));
    for (uint32_t k = 0; k < N; ++k) datain[k] = test[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)
    assert(compsize == compsize2);
    free(datain);
    free(compressedbuffer);
    free(recovdata);
    printf("Compressed %d integers down to %d bytes.\n", N, (int)compsize);
}
/home/moyi/thirdparty/streamvbyte/src/streamvbyte_x64_encode.c:91:3: runtime error: store to misaligned address 0x50b000001032 for type 'uint32_t' (aka 'unsigned int'), which requires 4 byte alignment
0x50b000001032: note: pointer points here
 69 74  49 03 00 00 00 00 00 00  00 00 00 00 00 be be be  be be be be be be be be  be be be be be be
              ^ 
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /home/moyi/thirdparty/streamvbyte/src/streamvbyte_x64_encode.c:91:3 in 
mmooyyii commented 7 months ago

https://github.com/lemire/streamvbyte/blob/5529af4f6f2bf44747af728fc62f9f08585c6c5e/src/streamvbyte_x64_encode.c#L91

maybe it should use memcpy?

memcpy(dataPtr, &dw, 4);
// *((uint32_t*)dataPtr) = dw;
lemire commented 7 months ago

Pull request invited.