1
0
Fork 0
mirror of https://git.tukaani.org/xz.git synced 2024-04-04 12:36:23 +02:00
xz-archive/src
Lasse Collin 596ed3de44 liblzma: Avoid memcpy(NULL, foo, 0) because it is undefined behavior.
I should have always known this but I didn't. Here is an example
as a reminder to myself:

    int mycopy(void *dest, void *src, size_t n)
    {
        memcpy(dest, src, n);
        return dest == NULL;
    }

In the example, a compiler may assume that dest != NULL because
passing NULL to memcpy() would be undefined behavior. Testing
with GCC 8.2.1, mycopy(NULL, NULL, 0) returns 1 with -O0 and -O1.
With -O2 the return value is 0 because the compiler infers that
dest cannot be NULL because it was already used with memcpy()
and thus the test for NULL gets optimized out.

In liblzma, if a null-pointer was passed to memcpy(), there were
no checks for NULL *after* the memcpy() call, so I cautiously
suspect that it shouldn't have caused bad behavior in practice,
but it's hard to be sure, and the problematic cases had to be
fixed anyway.

Thanks to Jeffrey Walton.
2019-07-13 17:56:28 +03:00
..
common tuklib_integer: New Intel C compiler needs immintrin.h. 2018-03-28 19:24:39 +03:00
liblzma liblzma: Avoid memcpy(NULL, foo, 0) because it is undefined behavior. 2019-07-13 17:56:28 +03:00
lzmainfo Build: Prepare to support Automake's subdir-objects. 2014-10-29 21:15:35 +02:00
scripts spelling 2019-07-13 17:53:33 +03:00
xz xz: Update xz man page date. 2019-07-13 17:54:52 +03:00
xzdec Update the home page URLs to HTTPS. 2018-03-28 19:16:06 +03:00
Makefile.am Build: Prepare to support Automake's subdir-objects. 2014-10-29 21:15:35 +02:00