mirror of
https://github.com/sheumann/hush.git
synced 2024-06-02 21:41:40 +00:00
Address some link errors with various small changes.
The most significant change is to at use malloc instead of alloca in the code to evaluate shell arithmetic expressions in shell/math.c.
This commit is contained in:
parent
af402a28a4
commit
036b92595c
|
@ -483,7 +483,7 @@ static int equalf(const char *f1, const char *f2)
|
||||||
|
|
||||||
static enum token check_operator(const char *s)
|
static enum token check_operator(const char *s)
|
||||||
{
|
{
|
||||||
static const struct operator_t no_op = {
|
static /*const*/ struct operator_t no_op = {
|
||||||
/*.op_num =*/ -1,
|
/*.op_num =*/ -1,
|
||||||
/*.op_type =*/ -1
|
/*.op_type =*/ -1
|
||||||
};
|
};
|
||||||
|
|
|
@ -119,26 +119,6 @@ unsigned bb_strtou(const char *arg, char **endp, int base) FAST_FUNC;
|
||||||
int bb_strtoi(const char *arg, char **endp, int base) FAST_FUNC;
|
int bb_strtoi(const char *arg, char **endp, int base) FAST_FUNC;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
uint32_t BUG_bb_strtou32_unimplemented(void);
|
|
||||||
static ALWAYS_INLINE
|
|
||||||
uint32_t bb_strtou32(const char *arg, char **endp, int base)
|
|
||||||
{
|
|
||||||
if (sizeof(uint32_t) == sizeof(unsigned))
|
|
||||||
return bb_strtou(arg, endp, base);
|
|
||||||
if (sizeof(uint32_t) == sizeof(unsigned long))
|
|
||||||
return bb_strtoul(arg, endp, base);
|
|
||||||
return BUG_bb_strtou32_unimplemented();
|
|
||||||
}
|
|
||||||
static ALWAYS_INLINE
|
|
||||||
int32_t bb_strtoi32(const char *arg, char **endp, int base)
|
|
||||||
{
|
|
||||||
if (sizeof(int32_t) == sizeof(int))
|
|
||||||
return bb_strtoi(arg, endp, base);
|
|
||||||
if (sizeof(int32_t) == sizeof(long))
|
|
||||||
return bb_strtol(arg, endp, base);
|
|
||||||
return BUG_bb_strtou32_unimplemented();
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Floating point */
|
/* Floating point */
|
||||||
|
|
||||||
double bb_strtod(const char *arg, char **endp) FAST_FUNC;
|
double bb_strtod(const char *arg, char **endp) FAST_FUNC;
|
||||||
|
|
|
@ -63,13 +63,16 @@ char* FAST_FUNC strncpy_IFNAMSIZ(char *dst, const char *src)
|
||||||
* A truncated result contains the first few digits of the result ala strncpy.
|
* A truncated result contains the first few digits of the result ala strncpy.
|
||||||
* Returns a pointer past last generated digit, does _not_ store NUL.
|
* Returns a pointer past last generated digit, does _not_ store NUL.
|
||||||
*/
|
*/
|
||||||
void BUG_sizeof(void);
|
|
||||||
char* FAST_FUNC utoa_to_buf(unsigned n, char *buf, unsigned buflen)
|
char* FAST_FUNC utoa_to_buf(unsigned n, char *buf, unsigned buflen)
|
||||||
{
|
{
|
||||||
unsigned i, out, res;
|
unsigned i, out, res;
|
||||||
|
|
||||||
if (buflen) {
|
if (buflen) {
|
||||||
out = 0;
|
out = 0;
|
||||||
|
if (sizeof(n) == 2)
|
||||||
|
// 2^16-1 = 65535
|
||||||
|
i = 10000;
|
||||||
|
else
|
||||||
if (sizeof(n) == 4)
|
if (sizeof(n) == 4)
|
||||||
// 2^32-1 = 4294967295
|
// 2^32-1 = 4294967295
|
||||||
i = 1000000000;
|
i = 1000000000;
|
||||||
|
@ -80,7 +83,7 @@ char* FAST_FUNC utoa_to_buf(unsigned n, char *buf, unsigned buflen)
|
||||||
i = 10 ** 19;
|
i = 10 ** 19;
|
||||||
#endif
|
#endif
|
||||||
else
|
else
|
||||||
BUG_sizeof();
|
bb_error_msg_and_die("Unsupported sizeof(unsigned)");
|
||||||
for (; i; i /= 10) {
|
for (; i; i /= 10) {
|
||||||
res = n / i;
|
res = n / i;
|
||||||
n = n % i;
|
n = n % i;
|
||||||
|
|
|
@ -385,13 +385,6 @@ void FAST_FUNC xchdir(const char *path)
|
||||||
bb_perror_msg_and_die("can't change directory to '%s'", path);
|
bb_perror_msg_and_die("can't change directory to '%s'", path);
|
||||||
}
|
}
|
||||||
|
|
||||||
void FAST_FUNC xchroot(const char *path)
|
|
||||||
{
|
|
||||||
if (chroot(path))
|
|
||||||
bb_perror_msg_and_die("can't change root directory to '%s'", path);
|
|
||||||
xchdir("/");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Print a warning message if opendir() fails, but don't die.
|
// Print a warning message if opendir() fails, but don't die.
|
||||||
DIR* FAST_FUNC warn_opendir(const char *path)
|
DIR* FAST_FUNC warn_opendir(const char *path)
|
||||||
{
|
{
|
||||||
|
|
|
@ -38,7 +38,11 @@ static int cmp_func(const void * a, const void * b)
|
||||||
return 1;
|
return 1;
|
||||||
if (s2 == NULL)
|
if (s2 == NULL)
|
||||||
return -1;
|
return -1;
|
||||||
|
#ifndef __GNO__
|
||||||
return strcoll(s1, s2);
|
return strcoll(s1, s2);
|
||||||
|
#else
|
||||||
|
return strcmp(s1, s2);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
22
shell/hush.c
22
shell/hush.c
|
@ -3121,7 +3121,7 @@ static const struct reserved_combo* match_reserved_word(o_string *word)
|
||||||
static int reserved_word(o_string *word, struct parse_context *ctx)
|
static int reserved_word(o_string *word, struct parse_context *ctx)
|
||||||
{
|
{
|
||||||
# if ENABLE_HUSH_CASE
|
# if ENABLE_HUSH_CASE
|
||||||
static const struct reserved_combo reserved_match = {
|
static /*const*/ struct reserved_combo reserved_match = {
|
||||||
"", RES_MATCH, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_ESAC
|
"", RES_MATCH, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_ESAC
|
||||||
};
|
};
|
||||||
# endif
|
# endif
|
||||||
|
@ -5870,11 +5870,13 @@ static FILE *generate_stream_from_string(const char *s, pid_t *pid_p)
|
||||||
|
|
||||||
xpipe(channel);
|
xpipe(channel);
|
||||||
|
|
||||||
pid = BB_MMU ? xfork() : xvfork_and_run(xforked_child, &args_struct);
|
|
||||||
#if BB_MMU
|
#if BB_MMU
|
||||||
|
pid = xfork();
|
||||||
if (pid == 0) { /* child */
|
if (pid == 0) { /* child */
|
||||||
xforked_child(&args_struct);
|
xforked_child(&args_struct);
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
|
pid = xvfork_and_run(xforked_child, &args_struct);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* parent */
|
/* parent */
|
||||||
|
@ -6099,7 +6101,11 @@ static void xvforked_child(void *grandchild_args) {
|
||||||
/* child */
|
/* child */
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
disable_restore_tty_pgrp_on_exit();
|
disable_restore_tty_pgrp_on_exit();
|
||||||
pid = BB_MMU ? xfork() : xvfork_and_run(xforked_grandchild, grandchild_args);
|
#if BB_MMU
|
||||||
|
pid = xfork();
|
||||||
|
#else
|
||||||
|
pid = xvfork_and_run(xforked_grandchild, grandchild_args);
|
||||||
|
#endif
|
||||||
if (pid != 0)
|
if (pid != 0)
|
||||||
_exit(0);
|
_exit(0);
|
||||||
xforked_grandchild(grandchild_args); // Only get here in BB_MMU case
|
xforked_grandchild(grandchild_args); // Only get here in BB_MMU case
|
||||||
|
@ -7212,7 +7218,9 @@ static NOINLINE int run_pipe(struct pipe *pi)
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
#if ENABLE_FEATURE_SH_NOFORK
|
||||||
clean_up_and_ret:
|
clean_up_and_ret:
|
||||||
|
#endif
|
||||||
unset_vars(new_env);
|
unset_vars(new_env);
|
||||||
add_vars(old_vars);
|
add_vars(old_vars);
|
||||||
/* clean_up_and_ret0: */
|
/* clean_up_and_ret0: */
|
||||||
|
@ -7225,7 +7233,8 @@ static NOINLINE int run_pipe(struct pipe *pi)
|
||||||
return rcode;
|
return rcode;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ENABLE_FEATURE_SH_NOFORK) {
|
#if ENABLE_FEATURE_SH_NOFORK
|
||||||
|
{
|
||||||
int n = find_applet_by_name(argv_expanded[0]);
|
int n = find_applet_by_name(argv_expanded[0]);
|
||||||
if (n >= 0 && APPLET_IS_NOFORK(n)) {
|
if (n >= 0 && APPLET_IS_NOFORK(n)) {
|
||||||
rcode = redirect_and_varexp_helper(&new_env, &old_vars, command, squirrel, argv_expanded);
|
rcode = redirect_and_varexp_helper(&new_env, &old_vars, command, squirrel, argv_expanded);
|
||||||
|
@ -7237,6 +7246,7 @@ static NOINLINE int run_pipe(struct pipe *pi)
|
||||||
goto clean_up_and_ret;
|
goto clean_up_and_ret;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
/* It is neither builtin nor applet. We must fork. */
|
/* It is neither builtin nor applet. We must fork. */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7285,11 +7295,13 @@ static NOINLINE int run_pipe(struct pipe *pi)
|
||||||
if (cmd_no < pi->num_cmds)
|
if (cmd_no < pi->num_cmds)
|
||||||
xpiped_pair(pipefds);
|
xpiped_pair(pipefds);
|
||||||
|
|
||||||
command->pid = BB_MMU ? fork() : vfork_and_run(forked_child, &args_struct);
|
|
||||||
#if BB_MMU
|
#if BB_MMU
|
||||||
|
command->pid = fork();
|
||||||
if (!command->pid) { /* child */
|
if (!command->pid) { /* child */
|
||||||
forked_child(&args_struct);
|
forked_child(&args_struct);
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
|
command->pid = vfork_and_run(forked_child, &args_struct);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* parent or error */
|
/* parent or error */
|
||||||
|
|
28
shell/math.c
28
shell/math.c
|
@ -505,11 +505,20 @@ evaluate_string(arith_state_t *math_state, const char *expr)
|
||||||
/* The proof that there can be no more than strlen(startbuf)/2+1
|
/* The proof that there can be no more than strlen(startbuf)/2+1
|
||||||
* integers in any given correct or incorrect expression
|
* integers in any given correct or incorrect expression
|
||||||
* is left as an exercise to the reader. */
|
* is left as an exercise to the reader. */
|
||||||
var_or_num_t *const numstack = alloca((expr_len / 2) * sizeof(numstack[0]));
|
var_or_num_t *const numstack = malloc((expr_len / 2) * sizeof(numstack[0]));
|
||||||
var_or_num_t *numstackptr = numstack;
|
var_or_num_t *numstackptr = numstack;
|
||||||
/* Stack of operator tokens */
|
/* Stack of operator tokens */
|
||||||
operator *const stack = alloca(expr_len * sizeof(stack[0]));
|
operator *const stack = malloc(expr_len * sizeof(stack[0]));
|
||||||
operator *stackptr = stack;
|
operator *stackptr = stack;
|
||||||
|
/* Buffer to hold names of variables encountered in the expression. */
|
||||||
|
char *const namebuf = malloc(expr_len);
|
||||||
|
char *namebufptr = namebuf;
|
||||||
|
arith_t retval = 0;
|
||||||
|
|
||||||
|
if (numstack == NULL || stack == NULL || namebuf == NULL) {
|
||||||
|
errmsg = "memory allocation failure in arithmetic evaluation";
|
||||||
|
goto ret_with_custom_retval;
|
||||||
|
}
|
||||||
|
|
||||||
/* Start with a left paren */
|
/* Start with a left paren */
|
||||||
*stackptr++ = lasttok = TOK_LPAREN;
|
*stackptr++ = lasttok = TOK_LPAREN;
|
||||||
|
@ -558,7 +567,8 @@ evaluate_string(arith_state_t *math_state, const char *expr)
|
||||||
if (p != expr) {
|
if (p != expr) {
|
||||||
/* Name */
|
/* Name */
|
||||||
size_t var_name_size = (p-expr) + 1; /* +1 for NUL */
|
size_t var_name_size = (p-expr) + 1; /* +1 for NUL */
|
||||||
numstackptr->var = alloca(var_name_size);
|
numstackptr->var = namebufptr;
|
||||||
|
namebufptr += var_name_size;
|
||||||
safe_strncpy(numstackptr->var, expr, var_name_size);
|
safe_strncpy(numstackptr->var, expr, var_name_size);
|
||||||
expr = p;
|
expr = p;
|
||||||
num:
|
num:
|
||||||
|
@ -693,8 +703,18 @@ evaluate_string(arith_state_t *math_state, const char *expr)
|
||||||
err_with_custom_msg:
|
err_with_custom_msg:
|
||||||
numstack->val = -1;
|
numstack->val = -1;
|
||||||
ret:
|
ret:
|
||||||
|
retval = numstack->val;
|
||||||
|
ret_with_custom_retval:
|
||||||
math_state->errmsg = errmsg;
|
math_state->errmsg = errmsg;
|
||||||
return numstack->val;
|
|
||||||
|
if (numstack)
|
||||||
|
free(numstack);
|
||||||
|
if (stack)
|
||||||
|
free(stack);
|
||||||
|
if (namebuf)
|
||||||
|
free(namebuf);
|
||||||
|
|
||||||
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
arith_t FAST_FUNC
|
arith_t FAST_FUNC
|
||||||
|
|
Loading…
Reference in New Issue
Block a user