From b645803e9c200ee176432d59ffc0e27769621500 Mon Sep 17 00:00:00 2001 From: Roland McGrath Date: Wed, 30 Jun 2010 11:57:19 -0700 Subject: [PATCH] strings: Fix brainos in non-mmap case. --- src/ChangeLog | 7 +++++++ src/strings.c | 20 ++++++++++---------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/ChangeLog b/src/ChangeLog index 6b2188b..00fd42f 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,10 @@ +2010-06-30 Roland McGrath + + * strings.c (main): Use STDIN_FILENO, not STDOUT_FILENO. + Ignore st_size for a non-S_ISREG file descriptor. + (read_block): Move assert after no-mmap bail-out. + (read_block_no_mmap): Fix size calculations for moving buffer remnant. + 2010-06-22 Roland McGrath * readelf.c (print_debug_line_section): Fix braino in DW_LNS_set_isa. diff --git a/src/strings.c b/src/strings.c index b69f2ad..2138098 100644 --- a/src/strings.c +++ b/src/strings.c @@ -1,5 +1,5 @@ /* Print the strings of printable characters in files. - Copyright (C) 2005, 2006, 2007, 2009 Red Hat, Inc. + Copyright (C) 2005-2010 Red Hat, Inc. This file is part of Red Hat elfutils. Written by Ulrich Drepper , 2005. @@ -169,9 +169,9 @@ main (int argc, char *argv[]) if (remaining == argc) /* We read from standard input. This we cannot do for a structured file. */ - result = read_fd (STDOUT_FILENO, + result = read_fd (STDIN_FILENO, print_file_name ? "{standard input}" : NULL, - fstat64 (STDOUT_FILENO, &st) == 0 + (fstat64 (STDIN_FILENO, &st) == 0 && S_ISREG (st.st_mode)) ? st.st_size : INT64_C (0x7fffffffffffffff)); else do @@ -548,17 +548,17 @@ read_block_no_mmap (int fd, const char *fname, off64_t from, off64_t fdlen) size_t nb = (size_t) n + ntrailer; if (nb >= min_len_bytes) { - /* We only use complete charactesr. */ + /* We only use complete characters. */ nb &= ~(bytes_per_char - 1); process_chunk (fname, buf, from + nb, nb, &unprinted); - /* If the last bytes of the buffer (module the character + /* If the last bytes of the buffer (modulo the character size) have been printed we are not copying them. */ size_t to_keep = unprinted != NULL ? 0 : min_len_bytes; - memmove (buf, buf + nb - to_keep, to_keep + nb); - ntrailer = to_keep + nb; + memmove (buf, buf + nb - to_keep, to_keep); + ntrailer = to_keep; from += nb; } else @@ -578,8 +578,6 @@ read_block_no_mmap (int fd, const char *fname, off64_t from, off64_t fdlen) static int read_block (int fd, const char *fname, off64_t fdlen, off64_t from, off64_t to) { - assert ((off64_t) min_len_bytes < fdlen); - if (elfmap == NULL) { /* We need a completely new mapping. */ @@ -597,12 +595,14 @@ read_block (int fd, const char *fname, off64_t fdlen, off64_t from, off64_t to) read pointer. */ // XXX Eventually add flag which avoids this if the position // XXX is known to match. - if (lseek64 (fd, from, SEEK_SET) != from) + if (from != 0 && lseek64 (fd, from, SEEK_SET) != from) error (EXIT_FAILURE, errno, gettext ("lseek64 failed")); return read_block_no_mmap (fd, fname, from, to - from); } + assert ((off64_t) min_len_bytes < fdlen); + if (to < (off64_t) elfmap_off || from > (off64_t) (elfmap_off + elfmap_size)) { /* The existing mapping cannot fit at all. Map the new area. -- 2.7.4