diff options
author | Jim Meyering <jim@meyering.net> | 2002-10-14 08:29:41 +0000 |
---|---|---|
committer | Jim Meyering <jim@meyering.net> | 2002-10-14 08:29:41 +0000 |
commit | bad450db5d35d67cb426a49a66b45c6c041cd401 (patch) | |
tree | 66ad62738a99f1f08b3b4397c602ce073d041f0a /src | |
parent | 8c11ff93d8019346b66cc88a09c742888b9d7c97 (diff) | |
download | coreutils-bad450db5d35d67cb426a49a66b45c6c041cd401.tar.xz |
Specifying a printf conversion specifer as nl's separator string
could cause nl to segfault.
(build_print_fmt): Don't include separator string
in the printf format; it might contain `%'.
Use a better bound on the length of the print_fmt buffer.
(print_lineno): Print the separator here instead.
Diffstat (limited to 'src')
-rw-r--r-- | src/nl.c | 16 |
1 files changed, 10 insertions, 6 deletions
@@ -234,18 +234,21 @@ FORMAT is one of:\n\ static void build_print_fmt (void) { - /* 12 = 10 chars for lineno_width, 1 for %, 1 for \0. */ - print_fmt = xmalloc (strlen (separator_str) + 12); + print_fmt = xmalloc ( 1 /* for `%' */ + + 1 /* for `-' or `0' */ + + INT_STRLEN_BOUND (lineno_width) + + 1 /* for `d' */ + + 1 /* for trailing NUL byte */ ); switch (lineno_format) { case FORMAT_RIGHT_NOLZ: - sprintf (print_fmt, "%%%dd%s", lineno_width, separator_str); + sprintf (print_fmt, "%%%dd", lineno_width); break; case FORMAT_RIGHT_LZ: - sprintf (print_fmt, "%%0%dd%s", lineno_width, separator_str); + sprintf (print_fmt, "%%0%dd", lineno_width); break; case FORMAT_LEFT: - sprintf (print_fmt, "%%-%dd%s", lineno_width, separator_str); + sprintf (print_fmt, "%%-%dd", lineno_width); break; } } @@ -286,12 +289,13 @@ build_type_arg (char **typep, struct re_pattern_buffer *regexp) return rval; } -/* Print and increment the line number. */ +/* Print the line number and separator; increment the line number. */ static void print_lineno (void) { printf (print_fmt, line_no); + fputs (separator_str, stdout); line_no += page_incr; } |