summaryrefslogtreecommitdiff
path: root/src/pr.c
diff options
context:
space:
mode:
authorJim Meyering <jim@meyering.net>2005-03-10 08:51:49 +0000
committerJim Meyering <jim@meyering.net>2005-03-10 08:51:49 +0000
commit82477b8efb984a578f93351c0618e793b2a017dc (patch)
tree2b6a1bdd702cad49066c3e4155c4951f102c2ee3 /src/pr.c
parent6eb7488b32564ec200d20335b121ccabc4298f05 (diff)
downloadcoreutils-82477b8efb984a578f93351c0618e793b2a017dc.tar.xz
Don't segfault for a long header date string, e.g.,
echo a|pr -D +%9999999A (init_header): Use x2nrealloc, rather than alloca. Don't bother with fixed-sized initial buffer; always use x*alloc.
Diffstat (limited to 'src/pr.c')
-rw-r--r--src/pr.c19
1 files changed, 10 insertions, 9 deletions
diff --git a/src/pr.c b/src/pr.c
index dc7115533..00fcf1865 100644
--- a/src/pr.c
+++ b/src/pr.c
@@ -1657,8 +1657,7 @@ print_files (int number_of_files, char **av)
static void
init_header (char *filename, int desc)
{
- char *buf;
- char initbuf[MAX (256, INT_BUFSIZE_BOUND (long int))];
+ char *buf = NULL;
struct stat st;
struct tm *tm;
@@ -1668,25 +1667,27 @@ init_header (char *filename, int desc)
if (desc < 0 || fstat (desc, &st) != 0)
st.st_mtime = time (NULL);
- buf = initbuf;
tm = localtime (&st.st_mtime);
- if (! tm)
- sprintf (buf, "%ld", (long int) st.st_mtime);
+ if (tm == NULL)
+ {
+ buf = xmalloc (INT_BUFSIZE_BOUND (long int));
+ sprintf (buf, "%ld", (long int) st.st_mtime);
+ }
else
{
- size_t bufsize = sizeof initbuf;
+ size_t bufsize = 0;
for (;;)
{
+ buf = x2nrealloc (buf, &bufsize, sizeof *buf);
*buf = '\1';
- if (strftime (buf, bufsize, date_format, tm) || ! *buf)
+ if (strftime (buf, bufsize, date_format, tm) || *buf == '\0')
break;
- buf = alloca (bufsize *= 2);
}
}
if (date_text)
free (date_text);
- date_text = xstrdup (buf);
+ date_text = buf;
file_text = custom_header ? custom_header : desc < 0 ? "" : filename;
header_width_available = (chars_per_line
- mbswidth (date_text, 0)