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>
This commit is contained in:
Denys Vlasenko 2018-02-22 11:03:23 +01:00
parent 427ae18348
commit 2709acbbda

View File

@ -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 */