diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2018-02-22 11:03:23 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2018-02-22 11:03:23 +0100 |
commit | 2709acbbda8a82ccd18abfe191dcb9dd530e4c57 (patch) | |
tree | 384b57d4a93f8d3a4d4a09a3f9223f71991c76f9 /coreutils | |
parent | 427ae18348a908719ff60383b33041bce5e5393e (diff) | |
download | busybox-2709acbbda8a82ccd18abfe191dcb9dd530e4c57.tar.gz |
sort: fix potentially buggy use of OPT_STR
This also makes OPT_STR reused:
text data bss dec hex filename
930979 481 6852 938312 e5148 busybox_old
930954 481 6852 938287 e512f busybox_unstripped
^^^^^^
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'coreutils')
-rw-r--r-- | coreutils/sort.c | 27 |
1 files changed, 15 insertions, 12 deletions
diff --git a/coreutils/sort.c b/coreutils/sort.c index c24b62681..b39297a26 100644 --- a/coreutils/sort.c +++ b/coreutils/sort.c @@ -85,16 +85,7 @@ #include "libbb.h" -/* This is a NOEXEC applet. Be very careful! */ - - -/* - sort [-m][-o output][-bdfinru][-t char][-k keydef]... [file...] - sort -c [-bdfinru][-t char][-k keydef][file] -*/ - /* These are sort types */ -#define OPT_STR "ngMucszbrdfimS:T:o:k:*t:" enum { FLAG_n = 1, /* Numeric sort */ FLAG_g = 2, /* Sort using strtod() */ @@ -120,6 +111,15 @@ enum { FLAG_no_tie_break = 0x40000000, }; +static const char sort_opt_str[] ALIGN1 = "^" + "ngMucszbrdfimS:T:o:k:*t:" + "\0" "o--o:t--t"/*-t, -o: at most one of each*/; +/* + * OPT_STR must not be string literal, needs to have stable address: + * code uses "strchr(OPT_STR,c) - OPT_STR" idiom. + */ +#define OPT_STR (sort_opt_str + 1) + #if ENABLE_FEATURE_SORT_BIG static char key_separator; @@ -129,6 +129,10 @@ static struct sort_key { unsigned flags; } *key_list; + +/* This is a NOEXEC applet. Be very careful! */ + + static char *get_key(char *str, struct sort_key *key, int flags) { int start = start; /* for compiler */ @@ -404,9 +408,8 @@ int sort_main(int argc UNUSED_PARAM, char **argv) xfunc_error_retval = 2; /* Parse command line options */ - opts = getopt32(argv, "^" - OPT_STR - "\0" "o--o:t--t"/*-t, -o: at most one of each*/, + opts = getopt32(argv, + sort_opt_str, &str_ignored, &str_ignored, &str_o, &lst_k, &str_t ); /* global b strips leading and trailing spaces */ |