Possible fix for Coverity CID#7:

convert options() prototype to unsigned (size_t, in fact), and make an
explicitly casted (size_t)-1 the error return code, modelled after what
is often used in Unix libraries
This commit is contained in:
tg 2007-05-13 18:33:29 +00:00
parent 3daaf229ca
commit e392983af7
3 changed files with 27 additions and 27 deletions

30
funcs.c
View File

@ -5,7 +5,7 @@
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/funcs.c,v 1.51 2007/05/13 18:15:25 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/funcs.c,v 1.52 2007/05/13 18:33:28 tg Exp $");
int
c_cd(const char **wp)
@ -2517,14 +2517,14 @@ int
test_eval(Test_env *te, Test_op op, const char *opnd1, const char *opnd2,
int do_eval)
{
int res;
int not;
int i;
size_t k;
struct stat b1, b2;
if (!do_eval)
return 0;
switch ((int) op) {
switch ((int)op) {
/*
* Unary Operators
*/
@ -2533,16 +2533,16 @@ test_eval(Test_env *te, Test_op op, const char *opnd1, const char *opnd2,
case TO_STZER: /* -z */
return *opnd1 == '\0';
case TO_OPTION: /* -o */
if ((not = *opnd1 == '!'))
if ((i = *opnd1 == '!'))
opnd1++;
if ((res = option(opnd1)) < 0)
res = 0;
if ((k = option(opnd1)) == (size_t)-1)
k = 0;
else {
res = Flag(res);
if (not)
res = !res;
k = Flag(k);
if (i)
k = !k;
}
return res;
return k;
case TO_FILRD: /* -r */
return test_eaccess(opnd1, R_OK) == 0;
case TO_FILWR: /* -w */
@ -2584,12 +2584,12 @@ test_eval(Test_env *te, Test_op op, const char *opnd1, const char *opnd2,
case TO_FILGZ: /* -s */
return stat(opnd1, &b1) == 0 && b1.st_size > 0L;
case TO_FILTT: /* -t */
if (opnd1 && !bi_getn(opnd1, &res)) {
if (opnd1 && !bi_getn(opnd1, &i)) {
te->flags |= TEF_ERROR;
res = 0;
i = 0;
} else
res = isatty(opnd1 ? res : 0);
return res;
i = isatty(opnd1 ? i : 0);
return (i);
case TO_FILUID: /* -O */
return stat(opnd1, &b1) == 0 && b1.st_uid == ksheuid;
case TO_FILGID: /* -G */

20
misc.c
View File

@ -6,7 +6,7 @@
#include <grp.h>
#endif
__RCSID("$MirOS: src/bin/mksh/misc.c,v 1.53 2007/05/13 17:51:23 tg Exp $\t"
__RCSID("$MirOS: src/bin/mksh/misc.c,v 1.54 2007/05/13 18:33:29 tg Exp $\t"
MKSH_SH_H_ID);
#undef USE_CHVT
@ -145,16 +145,16 @@ const struct option options[] = {
/*
* translate -o option into F* constant (also used for test -o option)
*/
int
size_t
option(const char *n)
{
unsigned i;
size_t i;
for (i = 0; i < NELEM(options); i++)
if (options[i].name && strcmp(options[i].name, n) == 0)
return i;
return (i);
return -1;
return ((size_t)-1);
}
struct options_info {
@ -277,8 +277,8 @@ parse_args(const char **argv,
char *opts;
const char *array = NULL;
Getopt go;
size_t i;
int optc, set, sortargs = 0, arrayset = 0;
unsigned i;
/* First call? Build option strings... */
if (cmd_opts[0] == '\0') {
@ -350,18 +350,18 @@ parse_args(const char **argv,
break;
}
i = option(go.optarg);
if (i != (unsigned)-1 && set == Flag(i))
if ((i != (size_t)-1) && set == Flag(i))
/* Don't check the context if the flag
* isn't changing - makes "set -o interactive"
* work if you're already interactive. Needed
* if the output of "set +o" is to be used.
*/
;
else if (i != (unsigned)-1 && (options[i].flags & what))
change_flag((enum sh_flag) i, what, set);
else if ((i != (size_t)-1) && (options[i].flags & what))
change_flag((enum sh_flag)i, what, set);
else {
bi_errorf("%s: bad option", go.optarg);
return -1;
return (-1);
}
break;

4
sh.h
View File

@ -8,7 +8,7 @@
/* $OpenBSD: c_test.h,v 1.4 2004/12/20 11:34:26 otto Exp $ */
/* $OpenBSD: tty.h,v 1.5 2004/12/20 11:34:26 otto Exp $ */
#define MKSH_SH_H_ID "$MirOS: src/bin/mksh/sh.h,v 1.129 2007/05/13 17:51:23 tg Exp $"
#define MKSH_SH_H_ID "$MirOS: src/bin/mksh/sh.h,v 1.130 2007/05/13 18:33:29 tg Exp $"
#define MKSH_VERSION "R29 2007/05/10"
#if HAVE_SYS_PARAM_H
@ -1386,7 +1386,7 @@ char *str_save(const char *, Area *);
#define str_save(s,ap) (str_nsave((s), (s) ? strlen(s) : 0, (ap)))
#endif
char *str_nsave(const char *, int, Area *);
int option(const char *);
size_t option(const char *);
char *getoptions(void);
void change_flag(enum sh_flag, int, int);
int parse_args(const char **, int, int *);