~ubuntu-branches/ubuntu/utopic/coreutils/utopic-proposed

« back to all changes in this revision

Viewing changes to m4/getcwd-abort-bug.m4

  • Committer: Colin Watson
  • Date: 2013-10-30 15:48:33 UTC
  • mfrom: (8.3.5 sid)
  • Revision ID: cjwatson@canonical.com-20131030154833-xdt6e1yfffqom1c4
merge from Debian 8.21-1

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
# name is unusually large.  Any length between 4k and 16k trigger the bug
4
4
# when using glibc-2.4.90-9 or older.
5
5
 
6
 
# Copyright (C) 2006, 2009-2012 Free Software Foundation, Inc.
 
6
# Copyright (C) 2006, 2009-2013 Free Software Foundation, Inc.
7
7
# This file is free software; the Free Software Foundation
8
8
# gives unlimited permission to copy and/or distribute it,
9
9
# with or without modifications, as long as this notice is preserved.
58
58
int
59
59
main ()
60
60
{
61
 
  char const *dir_name = "confdir-14B---";
62
61
  char *cwd;
63
62
  size_t initial_cwd_len;
64
63
  int fail = 0;
65
 
  size_t desired_depth;
66
 
  size_t d;
67
64
 
68
65
  /* The bug is triggered when PATH_MAX < getpagesize (), so skip
69
66
     this relatively expensive and invasive test if that's not true.  */
70
 
  if (getpagesize () <= PATH_MAX)
 
67
#ifdef PATH_MAX
 
68
  int bug_possible = PATH_MAX < getpagesize ();
 
69
#else
 
70
  int bug_possible = 0;
 
71
#endif
 
72
  if (! bug_possible)
71
73
    return 0;
72
74
 
73
75
  cwd = getcwd (NULL, 0);
76
78
 
77
79
  initial_cwd_len = strlen (cwd);
78
80
  free (cwd);
79
 
  desired_depth = ((TARGET_LEN - 1 - initial_cwd_len)
80
 
                   / (1 + strlen (dir_name)));
81
 
  for (d = 0; d < desired_depth; d++)
82
 
    {
83
 
      if (mkdir (dir_name, S_IRWXU) < 0 || chdir (dir_name) < 0)
84
 
        {
85
 
          fail = 3; /* Unable to construct deep hierarchy.  */
86
 
          break;
87
 
        }
88
 
    }
89
 
 
90
 
  /* If libc has the bug in question, this invocation of getcwd
91
 
     results in a failed assertion.  */
92
 
  cwd = getcwd (NULL, 0);
93
 
  if (cwd == NULL)
94
 
    fail = 4; /* getcwd failed: it refuses to return a string longer
95
 
                 than PATH_MAX.  */
96
 
  free (cwd);
97
 
 
98
 
  /* Call rmdir first, in case the above chdir failed.  */
99
 
  rmdir (dir_name);
100
 
  while (0 < d--)
101
 
    {
102
 
      if (chdir ("..") < 0)
103
 
        {
104
 
          fail = 5;
105
 
          break;
106
 
        }
 
81
 
 
82
  if (1)
 
83
    {
 
84
      static char const dir_name[] = "confdir-14B---";
 
85
      size_t desired_depth = ((TARGET_LEN - 1 - initial_cwd_len)
 
86
                              / sizeof dir_name);
 
87
      size_t d;
 
88
      for (d = 0; d < desired_depth; d++)
 
89
        {
 
90
          if (mkdir (dir_name, S_IRWXU) < 0 || chdir (dir_name) < 0)
 
91
            {
 
92
              if (! (errno == ERANGE || errno == ENAMETOOLONG
 
93
                     || errno == ENOENT))
 
94
                fail = 3; /* Unable to construct deep hierarchy.  */
 
95
              break;
 
96
            }
 
97
        }
 
98
 
 
99
      /* If libc has the bug in question, this invocation of getcwd
 
100
         results in a failed assertion.  */
 
101
      cwd = getcwd (NULL, 0);
 
102
      if (cwd == NULL)
 
103
        fail = 4; /* getcwd didn't assert, but it failed for a long name
 
104
                     where the answer could have been learned.  */
 
105
      free (cwd);
 
106
 
 
107
      /* Call rmdir first, in case the above chdir failed.  */
107
108
      rmdir (dir_name);
 
109
      while (0 < d--)
 
110
        {
 
111
          if (chdir ("..") < 0)
 
112
            {
 
113
              fail = 5;
 
114
              break;
 
115
            }
 
116
          rmdir (dir_name);
 
117
        }
108
118
    }
109
119
 
110
120
  return fail;