diff options
author | Yann Collet <cyan@fb.com> | 2019-04-08 19:49:54 (GMT) |
---|---|---|
committer | Yann Collet <cyan@fb.com> | 2019-04-08 19:49:54 (GMT) |
commit | c198a39a663d5a36d2d564a2a4140485e42404bb (patch) | |
tree | 0bb8ed72f3a314eb106e339a1664bc9e7f9386fc /lib | |
parent | 34f0004a5ed5ca5a113432bf2e433966d545133e (diff) | |
download | lz4-c198a39a663d5a36d2d564a2a4140485e42404bb.zip lz4-c198a39a663d5a36d2d564a2a4140485e42404bb.tar.gz lz4-c198a39a663d5a36d2d564a2a4140485e42404bb.tar.bz2 |
LZ4_initStream() checks alignment restriction
updated associated documentation
Diffstat (limited to 'lib')
-rw-r--r-- | lib/lz4.c | 7 | ||||
-rw-r--r-- | lib/lz4.h | 17 |
2 files changed, 17 insertions, 7 deletions
@@ -1284,10 +1284,17 @@ LZ4_stream_t* LZ4_createStream(void) return lz4s; } +static size_t LZ4_stream_t_alignment(void) +{ + struct { char c; LZ4_stream_t t; } t_a; + return sizeof(t_a) - sizeof(t_a.t); +} + LZ4_stream_t* LZ4_initStream (void* buffer, size_t size) { DEBUGLOG(5, "LZ4_initStream"); if (size < sizeof(LZ4_stream_t)) return NULL; + if (((size_t)buffer) & (LZ4_stream_t_alignment() - 1)) return NULL; /* alignment check */ MEM_INIT(buffer, 0, sizeof(LZ4_stream_t)); return (LZ4_stream_t*)buffer; } @@ -528,13 +528,16 @@ union LZ4_stream_u { /*! LZ4_initStream() : * An LZ4_stream_t structure must be initialized at least once. - * While this is automatically done when invoking LZ4_createStream(), - * it's not when the structure is simply declared on stack (for example). - * Use this function to properly initialize a newly declared LZ4_stream_t. - * It can also accept any arbitrary buffer of sufficient size as input, - * and will return a pointer of proper type upon initialization. - * Note : initialization can fail if size < sizeof(LZ4_stream_t). - * In which case, the function will @return NULL. + * This is automatically done when invoking LZ4_createStream(), + * but it's not when the structure is simply declared on stack (for example). + * + * Use LZ4_initStream() to properly initialize a newly declared LZ4_stream_t. + * It can also initialize any arbitrary buffer of sufficient size, + * and will @return a pointer of proper type upon initialization. + * + * Note : initialization fails if size and alignment conditions are not respected. + * In which case, the function will @return NULL. + * Note2: An LZ4_stream_t structure guarantees correct alignment and size. */ LZ4LIB_API LZ4_stream_t* LZ4_initStream (void* buffer, size_t size); |