Skip to content

Instantly share code, notes, and snippets.

@bratsche
Created October 12, 2015 22:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bratsche/7b29b662e2c71571e7d7 to your computer and use it in GitHub Desktop.
Save bratsche/7b29b662e2c71571e7d7 to your computer and use it in GitHub Desktop.
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include <string.h>
#include <sys/stat.h>
#include <stdio.h>
/* Don't get link errors because mkdir is redefined to rpl_mkdir. */
#undef mkdir
#ifndef S_IRWXU
# define S_IRWXU 0700
#endif
/* FIXME: skip the run-test altogether on systems without getpagesize. */
#if ! HAVE_GETPAGESIZE
# define getpagesize() 0
#endif
/* This size is chosen to be larger than PATH_MAX (4k), yet smaller than
the 16kB pagesize on ia64 linux. Those conditions make the code below
trigger a bug in glibc's getcwd implementation before 2.4.90-10. */
#define TARGET_LEN (5 * 1024)
int
main ()
{
char const *dir_name = "confdir-14B---";
char *cwd;
size_t initial_cwd_len;
int fail = 0;
size_t desired_depth;
size_t d;
printf("Test");
/* The bug is triggered when PATH_MAX < getpagesize (), so skip
this relative expensive and invasive test if that's not true. */
#if 0
XXX - This section exits early when run on its own, but from within configure
the condition is not met and the 'infinite' subdirectories are created.
if (getpagesize () <= PATH_MAX) {
printf ("getpagesize() <= PATH_MAX\n");
return 0;
}
#endif
cwd = getcwd (NULL, 0);
if (cwd == NULL) {
printf ("cmd == NULL\n");
return 0;
}
initial_cwd_len = strlen (cwd);
free (cwd);
desired_depth = ((TARGET_LEN - 1 - initial_cwd_len)
/ (1 + strlen (dir_name)));
for (d = 0; d < desired_depth; d++)
{
if (mkdir (dir_name, S_IRWXU) < 0 || chdir (dir_name) < 0)
{
printf ("Unable to construct deep hierarchy, fail 3\n");
fail = 3; /* Unable to construct deep hierarchy. */
break;
}
}
/* If libc has the bug in question, this invocation of getcwd
results in a failed assertion. */
cwd = getcwd (NULL, 0);
if (cwd == NULL)
fail = 4; /* getcwd failed. This is ok, and expected. */
free (cwd);
/* Call rmdir first, in case the above chdir failed. */
rmdir (dir_name);
while (0 < d--)
{
if (chdir ("..") < 0)
break;
rmdir (dir_name);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment