Skip to content

fix-strnstr-segfault-v1

parent 05cfd1aed8b262e82f62acc2de2858d2d2b6679c
author Johannes Schindelin <johannes.schindelin@gmx.de> 1505316684 +0200
committer Johannes Schindelin <johannes.schindelin@gmx.de> 1505317348 +0200

As of f22054c94d (Modify strnstr.c., 2017-08-30), the strnstr()
implementation was replaced by a version that segfaults (at least
sometimes) on 64-bit systems.

The reason: the new implementation uses memmem(), and the prototype of
memmem() is missing because the _GNU_SOURCE constant is not defined
before including <string.h>. As a consequence its return type defaults
to int (and GCC spits out a warning).

On 64-bit systems, the int data type is too small, though, to hold a
full char *, hence the upper 32-bit are cut off and bad things happen
due to a bogus pointer being used to access memory.

Reported as https://github.com/Alexpux/MINGW-packages/issues/2879 in
the MSYS2 project.

Cc: Sichen Zhao <1473996754@qq.com>
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>

Submitted-As: https://public-inbox.org/git/54549d65d520d71e1d3038f2e8a1c2f8c0f1f70a.1505317436.git.johannes.schindelin@gmx.de
Assets 2