summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorYann Collet <cyan@fb.com>2019-04-08 19:49:54 (GMT)
committerYann Collet <cyan@fb.com>2019-04-08 19:49:54 (GMT)
commitc198a39a663d5a36d2d564a2a4140485e42404bb (patch)
tree0bb8ed72f3a314eb106e339a1664bc9e7f9386fc /lib
parent34f0004a5ed5ca5a113432bf2e433966d545133e (diff)
downloadlz4-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.c7
-rw-r--r--lib/lz4.h17
2 files changed, 17 insertions, 7 deletions
diff --git a/lib/lz4.c b/lib/lz4.c
index e0fcf0f..a2e58c0 100644
--- a/lib/lz4.c
+++ b/lib/lz4.c
@@ -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;
}
diff --git a/lib/lz4.h b/lib/lz4.h
index 5aa4229..e55e1c6 100644
--- a/lib/lz4.h
+++ b/lib/lz4.h
@@ -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);