summaryrefslogtreecommitdiffstats
path: root/gl/m4/strncpy.m4
blob: 5787617133bc5c0d295bb1d3459e65e56959924f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# strncpy.m4
# serial 1
dnl Copyright (C) 2002-2004, 2009-2025 Free Software Foundation, Inc.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
dnl with or without modifications, as long as this notice is preserved.
dnl This file is offered as-is, without any warranty.

AC_DEFUN_ONCE([gl_FUNC_STRNCPY],
[
  AC_REQUIRE([gl_STRING_H_DEFAULTS])
  AC_REQUIRE([AC_PROG_CC])
  AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles

  dnl Check for prerequisites for memory fence checks.
  gl_FUNC_MMAP_ANON
  AC_CHECK_HEADERS_ONCE([sys/mman.h])
  AC_CHECK_FUNCS_ONCE([mprotect])

  dnl Detect bug in FreeBSD 15.0 on x86_64:
  dnl strncpy should not dereference more than n bytes, but always dereferences
  dnl n+1 bytes if the first n bytes don't contain a NUL byte.
  dnl Assume that strncpy works on platforms that lack mprotect.
  AC_CACHE_CHECK([whether strncpy works], [gl_cv_func_strncpy_works],
    [AC_RUN_IFELSE([AC_LANG_PROGRAM([[
#include <string.h>
#if HAVE_SYS_MMAN_H
# include <fcntl.h>
# include <unistd.h>
# include <sys/types.h>
# include <sys/mman.h>
#endif
]GL_MDA_DEFINES],
[[
  char *fence = NULL;
#if HAVE_SYS_MMAN_H && HAVE_MPROTECT
  {
    long int pagesize = sysconf (_SC_PAGESIZE);
    char *two_pages =
      (char *) mmap (NULL, 2 * pagesize, PROT_READ | PROT_WRITE,
                     MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
    if (two_pages != (char *)(-1)
        && mprotect (two_pages + pagesize, pagesize, PROT_NONE) == 0)
      fence = two_pages + pagesize;
  }
#endif
  if (fence)
    {
      char dest[8];

      dest[0] = 'a';
      dest[1] = 'b';
      dest[2] = 'c';
      dest[3] = 'd';
      dest[4] = 'e';
      dest[5] = 'f';
      dest[6] = 'g';

      *(fence - 3) = '7';
      *(fence - 2) = '2';
      *(fence - 1) = '9';

      if (strncpy (dest + 1, fence - 3, 3) != dest + 1)
        return 1;
      if (dest[0] != 'a')
        return 2;
      if (dest[1] != '7' || dest[2] != '2' || dest[3] != '9')
        return 3;
      if (dest[4] != 'e')
        return 4;
    }
  return 0;
]])], [gl_cv_func_strncpy_works=yes], [gl_cv_func_strncpy_works=no],
       [
        case "$host_os" in
                                 # Guess no on FreeBSD.
          freebsd* | dragonfly*) gl_cv_func_strncpy_works="guessing no" ;;
                                 # Guess yes on native Windows.
          mingw* | windows*)     gl_cv_func_strncpy_works="guessing yes" ;;
                                 # Guess yes otherwise.
          *)                     gl_cv_func_strncpy_works="guessing yes" ;;
        esac
       ])
    ])
  case "$gl_cv_func_strncpy_works" in
    *yes) ;;
    *) REPLACE_STRNCPY=1 ;;
  esac
])

# Prerequisites of lib/strncpy.c.
AC_DEFUN([gl_PREREQ_STRNCPY], [
  :
])