Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Avoid outputting to TTY after an expected memory corruption in testcase
Protect TTY against an expected memory corruption from testcase
tst-malloc-backtrace, which is expected to SIGABRT after a forced memory
corruption.
  • Loading branch information
Tulio Magno Quites Machado Filho committed Jun 5, 2015
1 parent 7fe9e2e commit 0224244
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
5 changes: 5 additions & 0 deletions ChangeLog
@@ -1,3 +1,8 @@
2015-06-05 Tulio Magno Quites Machado Filho <tuliom@linux.vnet.ibm.com>

* malloc/tst-malloc-backtrace.c (do_test): Redirect libc fatal
errors to stderr.

2015-06-05 Florian Weimer <fweimer@redhat.com>

[BZ #15661]
Expand Down
15 changes: 10 additions & 5 deletions malloc/tst-malloc-backtrace.c
Expand Up @@ -30,21 +30,26 @@ call_free (void *ptr)
*(size_t *)(ptr - sizeof (size_t)) = 1;
}

int do_test (void);

#define TEST_FUNCTION do_test ()
#define EXPECTED_SIGNAL SIGABRT

#include "../test-skeleton.c"

int
do_test (void)
{
void *ptr1 = malloc (SIZE);
void *ptr2 = malloc (SIZE);

/* Avoid unwanted output to TTY after an expected memory corruption. */
ignore_stderr();

call_free (ptr1);
ptr1 = malloc (SIZE);

/* Not reached. The return statement is to put ptr2 into use so that gcc
doesn't optimize out that malloc call. */
return (ptr1 == ptr2);
}

#define TEST_FUNCTION do_test ()
#define EXPECTED_SIGNAL SIGABRT

#include "../test-skeleton.c"
28 changes: 17 additions & 11 deletions test-skeleton.c
Expand Up @@ -218,6 +218,22 @@ signal_handler (int sig __attribute__ ((unused)))
exit (1);
}

/* Avoid all the buffer overflow messages on stderr. */
static void
__attribute__ ((unused))
ignore_stderr (void)
{
int fd = open (_PATH_DEVNULL, O_WRONLY);
if (fd == -1)
close (STDERR_FILENO);
else
{
dup2 (fd, STDERR_FILENO);
close (fd);
}
setenv ("LIBC_FATAL_STDERR_", "1", 1);
}

/* Set fortification error handler. Used when tests want to verify that bad
code is caught by the library. */
static void
Expand All @@ -231,17 +247,7 @@ set_fortify_handler (void (*handler) (int sig))
sigemptyset (&sa.sa_mask);

sigaction (SIGABRT, &sa, NULL);

/* Avoid all the buffer overflow messages on stderr. */
int fd = open (_PATH_DEVNULL, O_WRONLY);
if (fd == -1)
close (STDERR_FILENO);
else
{
dup2 (fd, STDERR_FILENO);
close (fd);
}
setenv ("LIBC_FATAL_STDERR_", "1", 1);
ignore_stderr ();
}

/* We provide the entry point here. */
Expand Down

0 comments on commit 0224244

Please sign in to comment.