From 5b512e1121b25b0e1652fb812932676b830ab531 Mon Sep 17 00:00:00 2001 From: Chet Ramey Date: Wed, 4 Jan 2023 09:18:59 -0500 Subject: [PATCH] fourth set of ANSI C changes: size_t; rest globskipdots to enabled default in subshells --- CWRU/CWRU.chlog | 29 +++++++++++++++++++++++++++++ alias.c | 1 - bashline.c | 4 ++-- braces.c | 10 ++++++---- builtins/bind.def | 5 +++-- builtins/shopt.def | 1 + builtins/type.def | 2 -- execute_cmd.c | 4 ++-- externs.h | 18 +++++++++--------- input.h | 4 ++-- lib/sh/stringlist.c | 6 +++--- lib/sh/stringvec.c | 12 ++++++------ parse.y | 28 ++++++++++++++-------------- tests/shopt.right | 4 ---- variables.c | 15 ++++++++------- variables.h | 4 ++-- 16 files changed, 87 insertions(+), 60 deletions(-) diff --git a/CWRU/CWRU.chlog b/CWRU/CWRU.chlog index 92084e09..bad18c8c 100644 --- a/CWRU/CWRU.chlog +++ b/CWRU/CWRU.chlog @@ -4902,3 +4902,32 @@ subst.c,findcmd.c arrayfunc.h,variables.h,findcmd.h,bashline.h,trap.h,pathexp.h,externs.h, pcomplete.h,bashhist.h,assoc.h,general.h,subst.h - more `const' changes, remove `register' + + 1/3 + --- +parse.y + - more `const' changes, remove `register' + +variables.h + - VARLIST: list_len and list_size now size_t + +variables.c + - vlist_alloc: take size_t parameter, changed callers + - vlist_realloc: take size_t parameter, changed callers + +lib/sh/stringvec.c,externs.h + - functions that take int size params now take size_t + - strvec_len: returns size_t + +lib/sh/stringlist.c,externs.h + - STRINGLIST: list_size and list_len now size_t + - functions that take int size params now take size_t + +variables.c,bashline.c,builtins/bind.def + - change strvec_ int parameters to size_t where it makes sense + - change strlist_ int parameters to size_t where it makes sense + +builtins/shopt.def + - reset_shopt_options: set glob_always_skip_dot_and_dotdot to 1, + since that's the current default in lib/glob/glob.c + diff --git a/alias.c b/alias.c index a01496a1..1c2a1303 100644 --- a/alias.c +++ b/alias.c @@ -464,7 +464,6 @@ char * alias_expand (char *string) { - register int i, j, start; char *line, *token; int line_len, tl, real_start, expand_next, expand_this_token; diff --git a/bashline.c b/bashline.c index cdff17c1..45046aee 100644 --- a/bashline.c +++ b/bashline.c @@ -753,10 +753,10 @@ display_shell_version (int count, int c) static char **hostname_list = (char **)NULL; /* The physical size of the above list. */ -static int hostname_list_size; +static size_t hostname_list_size; /* The number of hostnames in the above list. */ -static int hostname_list_length; +static size_t hostname_list_length; /* Whether or not HOSTNAME_LIST has been initialized. */ int hostname_list_initialized = 0; diff --git a/braces.c b/braces.c index 761406d9..e21160c1 100644 --- a/braces.c +++ b/braces.c @@ -1,6 +1,6 @@ /* braces.c -- code for doing word expansion in curly braces. */ -/* Copyright (C) 1987-2020,2022 Free Software Foundation, Inc. +/* Copyright (C) 1987-2020,2022-2023 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. @@ -304,7 +304,8 @@ expand_amble (char *text, size_t tlen, int flags) result = partial; else { - register int lr, lp, j; + size_t lr, lp; + int j; lr = strvec_len (result); lp = strvec_len (partial); @@ -711,8 +712,9 @@ comsub: static char ** array_concat (char **arr1, char **arr2) { - register int i, j, len, len1, len2; - register char **result; + int i, j, len; + size_t len1, len2; + char **result; if (arr1 == 0) return (arr2); /* XXX - see if we can get away without copying? */ diff --git a/builtins/bind.def b/builtins/bind.def index 367ef86a..99dd1fa9 100644 --- a/builtins/bind.def +++ b/builtins/bind.def @@ -1,7 +1,7 @@ This file is bind.def, from which is created bind.c. It implements the builtin "bind" in Bash. -Copyright (C) 1987-2022 Free Software Foundation, Inc. +Copyright (C) 1987-2023 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. @@ -276,7 +276,8 @@ bind_builtin (WORD_LIST *list) /* Process the rest of the arguments as binding specifications. */ while (list) { - int olen, nlen, d, i; + size_t olen, nlen; + int d, i; char **obindings, **nbindings; obindings = rl_invoking_keyseqs (bash_execute_unix_command); diff --git a/builtins/shopt.def b/builtins/shopt.def index cf1187f3..8ecc5615 100644 --- a/builtins/shopt.def +++ b/builtins/shopt.def @@ -383,6 +383,7 @@ reset_shopt_options (void) #if defined (EXTENDED_GLOB) extended_glob = extglob_flag = EXTGLOB_DEFAULT; #endif + glob_always_skip_dot_and_dotdot = 1; /* new default as of bash-5.2 */ #if defined (ARRAY_VARS) expand_once_flag = assoc_expand_once = 0; diff --git a/builtins/type.def b/builtins/type.def index 1e0fb457..ad67a938 100644 --- a/builtins/type.def +++ b/builtins/type.def @@ -76,8 +76,6 @@ $END #include "common.h" #include "bashgetopt.h" -extern int find_reserved_word (char *); - /* For each word in LIST, find out what the shell is going to do with it as a simple command. i.e., which file would this shell use to execve, or if it is a builtin command, or an alias. Possible flag diff --git a/execute_cmd.c b/execute_cmd.c index 7592d900..61ceb61b 100644 --- a/execute_cmd.c +++ b/execute_cmd.c @@ -5836,9 +5836,9 @@ initialize_subshell (void) int shell_execve (char *command, char **args, char **env) { - int larray, i, fd; + int i, fd, sample_len; char sample[HASH_BANG_BUFSIZ]; - int sample_len; + size_t larray; SETOSTYPE (0); /* Some systems use for USG/POSIX semantics */ execve (command, args, env); diff --git a/externs.h b/externs.h index eded48bb..db287a41 100644 --- a/externs.h +++ b/externs.h @@ -387,14 +387,14 @@ extern size_t strftime (char *, size_t, const char *, const struct tm *); /* This is a general-purpose argv-style array struct. */ typedef struct _list_of_strings { char **list; - int list_size; - int list_len; + size_t list_size; + size_t list_len; } STRINGLIST; typedef int sh_strlist_map_func_t (char *); -extern STRINGLIST *strlist_create (int); -extern STRINGLIST *strlist_resize (STRINGLIST *, int); +extern STRINGLIST *strlist_create (size_t); +extern STRINGLIST *strlist_resize (STRINGLIST *, size_t); extern void strlist_flush (STRINGLIST *); extern void strlist_dispose (STRINGLIST *); extern int strlist_remove (STRINGLIST *, const char *); @@ -408,14 +408,14 @@ extern void strlist_sort (STRINGLIST *); /* declarations for functions defined in lib/sh/stringvec.c */ -extern char **strvec_create (int); -extern char **strvec_resize (char **, int); -extern char **strvec_mcreate (int); -extern char **strvec_mresize (char **, int); +extern char **strvec_create (size_t); +extern char **strvec_resize (char **, size_t); +extern char **strvec_mcreate (size_t); +extern char **strvec_mresize (char **, size_t); extern void strvec_flush (char **); extern void strvec_dispose (char **); extern int strvec_remove (char **, const char *); -extern int strvec_len (char * const *); +extern size_t strvec_len (char * const *); extern int strvec_search (char **, const char *); extern char **strvec_copy (char * const *); extern int strvec_posixcmp (char **, char **); diff --git a/input.h b/input.h index 5e9d9275..5fcfa743 100644 --- a/input.h +++ b/input.h @@ -102,9 +102,9 @@ extern void push_stream (int); extern void pop_stream (void); extern int stream_on_stack (enum stream_type); extern char *read_secondary_line (int); -extern int find_reserved_word (char *); +extern int find_reserved_word (const char *); extern void gather_here_documents (void); -extern void execute_variable_command (char *, char *); +extern void execute_variable_command (const char *, const char *); extern int *save_token_state (void); extern void restore_token_state (int *); diff --git a/lib/sh/stringlist.c b/lib/sh/stringlist.c index da31ab89..6cc2fac7 100644 --- a/lib/sh/stringlist.c +++ b/lib/sh/stringlist.c @@ -1,6 +1,6 @@ /* stringlist.c - functions to handle a generic `list of strings' structure */ -/* Copyright (C) 2000-2019, 2022 Free Software Foundation, Inc. +/* Copyright (C) 2000-2019, 2022-2023 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. @@ -37,7 +37,7 @@ /* Allocate a new STRINGLIST, with room for N strings. */ STRINGLIST * -strlist_create (int n) +strlist_create (size_t n) { STRINGLIST *ret; register int i; @@ -60,7 +60,7 @@ strlist_create (int n) } STRINGLIST * -strlist_resize (STRINGLIST *sl, int n) +strlist_resize (STRINGLIST *sl, size_t n) { register int i; diff --git a/lib/sh/stringvec.c b/lib/sh/stringvec.c index c06690ca..9738ef46 100644 --- a/lib/sh/stringvec.c +++ b/lib/sh/stringvec.c @@ -1,6 +1,6 @@ /* stringvec.c - functions for managing arrays of strings. */ -/* Copyright (C) 2000-2002,2022 Free Software Foundation, Inc. +/* Copyright (C) 2000-2002,2022-2023 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. @@ -34,32 +34,32 @@ /* Allocate an array of strings with room for N members. */ char ** -strvec_create (int n) +strvec_create (size_t n) { return ((char **)xmalloc ((n) * sizeof (char *))); } /* Allocate an array of strings with room for N members. */ char ** -strvec_mcreate (int n) +strvec_mcreate (size_t n) { return ((char **)malloc ((n) * sizeof (char *))); } char ** -strvec_resize (char **array, int nsize) +strvec_resize (char **array, size_t nsize) { return ((char **)xrealloc (array, nsize * sizeof (char *))); } char ** -strvec_mresize (char **array, int nsize) +strvec_mresize (char **array, size_t nsize) { return ((char **)realloc (array, nsize * sizeof (char *))); } /* Return the length of ARRAY, a NULL terminated array of char *. */ -int +size_t strvec_len (char * const *array) { register int i; diff --git a/parse.y b/parse.y index 8f330897..2fffd2b4 100644 --- a/parse.y +++ b/parse.y @@ -174,10 +174,10 @@ static int reserved_word_acceptable (int); static int yylex (void); static void push_heredoc (REDIRECT *); -static char *mk_alexpansion (char *); -static int alias_expand_token (char *); +static char *mk_alexpansion (const char *); +static int alias_expand_token (const char *); static int time_command_acceptable (void); -static int special_case_tokens (char *); +static int special_case_tokens (const char *); static int read_token (int); static char *parse_matched_pair (int, int, int, int *, int); static char *parse_comsub (int, int, int, int *, int); @@ -198,7 +198,7 @@ static int cond_skip_newlines (void); static COMMAND *parse_cond_command (void); #endif #if defined (ARRAY_VARS) -static int token_is_assignment (char *, int); +static int token_is_assignment (const char *, int); static int token_is_ident (char *, int); #endif static int read_token_word (int); @@ -207,7 +207,7 @@ static void discard_parser_constructs (int); static char *error_token_from_token (int); static char *error_token_from_text (void); static void print_offending_line (void); -static void report_syntax_error (char *); +static void report_syntax_error (const char *); static void handle_eof_input_unit (void); static void prompt_again (int); @@ -1622,8 +1622,8 @@ with_input_from_stdin (void) static int yy_string_get (void) { - register char *string; - register unsigned char c; + char *string; + unsigned char c; string = bash_input.location.string; @@ -2805,7 +2805,7 @@ discard_until (int character) } void -execute_variable_command (char *command, char *vname) +execute_variable_command (const char *command, const char *vname) { char *last_lastarg; sh_parser_state_t ps; @@ -3013,7 +3013,7 @@ static int open_brace_count; In a pattern list in a case statement (parser_state & PST_CASEPAT). */ static char * -mk_alexpansion (char *s) +mk_alexpansion (const char *s) { int l; char *r; @@ -3035,7 +3035,7 @@ mk_alexpansion (char *s) } static int -alias_expand_token (char *tokstr) +alias_expand_token (const char *tokstr) { char *expanded; alias_t *ap; @@ -3150,7 +3150,7 @@ time_command_acceptable (void) */ static int -special_case_tokens (char *tokstr) +special_case_tokens (const char *tokstr) { /* Posix grammar rule 6 */ if ((last_read_token == WORD) && @@ -4768,7 +4768,7 @@ parse_cond_command (void) substitution that will reallocate atoken. We don't want to write beyond the end of an allocated buffer. */ static int -token_is_assignment (char *t, int i) +token_is_assignment (const char *t, int i) { int r; char *atoken; @@ -5367,7 +5367,7 @@ reserved_word_acceptable (int toksym) /* Return the index of TOKEN in the alist of reserved words, or -1 if TOKEN is not a shell reserved word. */ int -find_reserved_word (char *tokstr) +find_reserved_word (const char *tokstr) { int i; for (i = 0; word_token_alist[i].word; i++) @@ -6172,7 +6172,7 @@ print_offending_line (void) then place it in MESSAGE, otherwise pass NULL and this will figure out an appropriate message for you. */ static void -report_syntax_error (char *message) +report_syntax_error (const char *message) { char *msg, *p; diff --git a/tests/shopt.right b/tests/shopt.right index d617c1d7..640f75d2 100644 --- a/tests/shopt.right +++ b/tests/shopt.right @@ -304,9 +304,5 @@ xtrace off -- ./shopt.tests: line 106: shopt: xyz1: invalid shell option name ./shopt.tests: line 107: shopt: xyz1: invalid option name -28c28 -< globskipdots off ---- -> globskipdots on expand_aliases on expand_aliases on diff --git a/variables.c b/variables.c index 24f3ef8c..8f112807 100644 --- a/variables.c +++ b/variables.c @@ -175,7 +175,7 @@ int shell_level = 0; shell variables that are marked for export. */ char **export_env = (char **)NULL; static int export_env_index; -static int export_env_size; +static size_t export_env_size; #if defined (READLINE) static int winsize_assignment; /* currently assigning to LINES or COLUMNS */ @@ -284,8 +284,8 @@ static SHELL_VAR *bind_variable_internal (const char *, const char *, HASH_TABLE static void dispose_variable_value (SHELL_VAR *); static void free_variable_hash_data (PTR_T); -static VARLIST *vlist_alloc (int); -static VARLIST *vlist_realloc (VARLIST *, int); +static VARLIST *vlist_alloc (size_t); +static VARLIST *vlist_realloc (VARLIST *, size_t); static void vlist_add (VARLIST *, SHELL_VAR *, int); static void flatten (HASH_TABLE *, sh_var_map_func_t *, VARLIST *, int); @@ -4037,7 +4037,7 @@ set_func_auto_export (const char *name) /* **************************************************************** */ static VARLIST * -vlist_alloc (int nentries) +vlist_alloc (size_t nentries) { VARLIST *vlist; @@ -4051,7 +4051,7 @@ vlist_alloc (int nentries) } static VARLIST * -vlist_realloc (VARLIST *vlist, int n) +vlist_realloc (VARLIST *vlist, size_t n) { if (vlist == 0) return (vlist = vlist_alloc (n)); @@ -4090,7 +4090,7 @@ map_over (sh_var_map_func_t *function, VAR_CONTEXT *vc) VAR_CONTEXT *v; VARLIST *vlist; SHELL_VAR **ret; - int nentries; + size_t nentries; for (nentries = 0, v = vc; v; v = v->down) nentries += HASH_ENTRIES (v->table); @@ -4334,7 +4334,8 @@ all_variables_matching_prefix (const char *prefix) { SHELL_VAR **varlist; char **rlist; - int vind, rind, plen; + int rind, plen; + size_t vind; plen = STRLEN (prefix); varlist = all_visible_variables (); diff --git a/variables.h b/variables.h index 63779fd7..1616d455 100644 --- a/variables.h +++ b/variables.h @@ -95,8 +95,8 @@ typedef struct variable { typedef struct _vlist { SHELL_VAR **list; - int list_size; /* allocated size */ - int list_len; /* current number of entries */ + size_t list_size; /* allocated size */ + size_t list_len; /* current number of entries */ } VARLIST; /* The various attributes that a given variable can have. */