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:
		
							
								
								
									
										28
									
								
								funcs.c
									
									
									
									
									
								
							
							
						
						
									
										28
									
								
								funcs.c
									
									
									
									
									
								
							| @@ -5,7 +5,7 @@ | |||||||
|  |  | ||||||
| #include "sh.h" | #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 | int | ||||||
| c_cd(const char **wp) | c_cd(const char **wp) | ||||||
| @@ -2517,8 +2517,8 @@ int | |||||||
| test_eval(Test_env *te, Test_op op, const char *opnd1, const char *opnd2, | test_eval(Test_env *te, Test_op op, const char *opnd1, const char *opnd2, | ||||||
|     int do_eval) |     int do_eval) | ||||||
| { | { | ||||||
| 	int res; | 	int i; | ||||||
| 	int not; | 	size_t k; | ||||||
| 	struct stat b1, b2; | 	struct stat b1, b2; | ||||||
|  |  | ||||||
| 	if (!do_eval) | 	if (!do_eval) | ||||||
| @@ -2533,16 +2533,16 @@ test_eval(Test_env *te, Test_op op, const char *opnd1, const char *opnd2, | |||||||
| 	case TO_STZER: /* -z */ | 	case TO_STZER: /* -z */ | ||||||
| 		return *opnd1 == '\0'; | 		return *opnd1 == '\0'; | ||||||
| 	case TO_OPTION: /* -o */ | 	case TO_OPTION: /* -o */ | ||||||
| 		if ((not = *opnd1 == '!')) | 		if ((i = *opnd1 == '!')) | ||||||
| 			opnd1++; | 			opnd1++; | ||||||
| 		if ((res = option(opnd1)) < 0) | 		if ((k = option(opnd1)) == (size_t)-1) | ||||||
| 			res = 0; | 			k = 0; | ||||||
| 		else { | 		else { | ||||||
| 			res = Flag(res); | 			k = Flag(k); | ||||||
| 			if (not) | 			if (i) | ||||||
| 				res = !res; | 				k = !k; | ||||||
| 		} | 		} | ||||||
| 		return res; | 		return k; | ||||||
| 	case TO_FILRD: /* -r */ | 	case TO_FILRD: /* -r */ | ||||||
| 		return test_eaccess(opnd1, R_OK) == 0; | 		return test_eaccess(opnd1, R_OK) == 0; | ||||||
| 	case TO_FILWR: /* -w */ | 	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 */ | 	case TO_FILGZ: /* -s */ | ||||||
| 		return stat(opnd1, &b1) == 0 && b1.st_size > 0L; | 		return stat(opnd1, &b1) == 0 && b1.st_size > 0L; | ||||||
| 	case TO_FILTT: /* -t */ | 	case TO_FILTT: /* -t */ | ||||||
| 		if (opnd1 && !bi_getn(opnd1, &res)) { | 		if (opnd1 && !bi_getn(opnd1, &i)) { | ||||||
| 			te->flags |= TEF_ERROR; | 			te->flags |= TEF_ERROR; | ||||||
| 			res = 0; | 			i = 0; | ||||||
| 		} else | 		} else | ||||||
| 			res = isatty(opnd1 ? res : 0); | 			i = isatty(opnd1 ? i : 0); | ||||||
| 		return res; | 		return (i); | ||||||
| 	case TO_FILUID: /* -O */ | 	case TO_FILUID: /* -O */ | ||||||
| 		return stat(opnd1, &b1) == 0 && b1.st_uid == ksheuid; | 		return stat(opnd1, &b1) == 0 && b1.st_uid == ksheuid; | ||||||
| 	case TO_FILGID: /* -G */ | 	case TO_FILGID: /* -G */ | ||||||
|   | |||||||
							
								
								
									
										18
									
								
								misc.c
									
									
									
									
									
								
							
							
						
						
									
										18
									
								
								misc.c
									
									
									
									
									
								
							| @@ -6,7 +6,7 @@ | |||||||
| #include <grp.h> | #include <grp.h> | ||||||
| #endif | #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); | 	MKSH_SH_H_ID); | ||||||
|  |  | ||||||
| #undef USE_CHVT | #undef USE_CHVT | ||||||
| @@ -145,16 +145,16 @@ const struct option options[] = { | |||||||
| /* | /* | ||||||
|  * translate -o option into F* constant (also used for test -o option) |  * translate -o option into F* constant (also used for test -o option) | ||||||
|  */ |  */ | ||||||
| int | size_t | ||||||
| option(const char *n) | option(const char *n) | ||||||
| { | { | ||||||
| 	unsigned i; | 	size_t i; | ||||||
|  |  | ||||||
| 	for (i = 0; i < NELEM(options); i++) | 	for (i = 0; i < NELEM(options); i++) | ||||||
| 		if (options[i].name && strcmp(options[i].name, n) == 0) | 		if (options[i].name && strcmp(options[i].name, n) == 0) | ||||||
| 			return i; | 			return (i); | ||||||
|  |  | ||||||
| 	return -1; | 	return ((size_t)-1); | ||||||
| } | } | ||||||
|  |  | ||||||
| struct options_info { | struct options_info { | ||||||
| @@ -277,8 +277,8 @@ parse_args(const char **argv, | |||||||
| 	char *opts; | 	char *opts; | ||||||
| 	const char *array = NULL; | 	const char *array = NULL; | ||||||
| 	Getopt go; | 	Getopt go; | ||||||
|  | 	size_t i; | ||||||
| 	int optc, set, sortargs = 0, arrayset = 0; | 	int optc, set, sortargs = 0, arrayset = 0; | ||||||
| 	unsigned i; |  | ||||||
|  |  | ||||||
| 	/* First call?  Build option strings... */ | 	/* First call?  Build option strings... */ | ||||||
| 	if (cmd_opts[0] == '\0') { | 	if (cmd_opts[0] == '\0') { | ||||||
| @@ -350,18 +350,18 @@ parse_args(const char **argv, | |||||||
| 				break; | 				break; | ||||||
| 			} | 			} | ||||||
| 			i = option(go.optarg); | 			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 | 				/* Don't check the context if the flag | ||||||
| 				 * isn't changing - makes "set -o interactive" | 				 * isn't changing - makes "set -o interactive" | ||||||
| 				 * work if you're already interactive.  Needed | 				 * work if you're already interactive.  Needed | ||||||
| 				 * if the output of "set +o" is to be used. | 				 * if the output of "set +o" is to be used. | ||||||
| 				 */ | 				 */ | ||||||
| 				; | 				; | ||||||
| 			else if (i != (unsigned)-1 && (options[i].flags & what)) | 			else if ((i != (size_t)-1) && (options[i].flags & what)) | ||||||
| 				change_flag((enum sh_flag)i, what, set); | 				change_flag((enum sh_flag)i, what, set); | ||||||
| 			else { | 			else { | ||||||
| 				bi_errorf("%s: bad option", go.optarg); | 				bi_errorf("%s: bad option", go.optarg); | ||||||
| 				return -1; | 				return (-1); | ||||||
| 			} | 			} | ||||||
| 			break; | 			break; | ||||||
|  |  | ||||||
|   | |||||||
							
								
								
									
										4
									
								
								sh.h
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								sh.h
									
									
									
									
									
								
							| @@ -8,7 +8,7 @@ | |||||||
| /*	$OpenBSD: c_test.h,v 1.4 2004/12/20 11:34:26 otto Exp $	*/ | /*	$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 $	*/ | /*	$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" | #define MKSH_VERSION "R29 2007/05/10" | ||||||
|  |  | ||||||
| #if HAVE_SYS_PARAM_H | #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))) | #define str_save(s,ap) (str_nsave((s), (s) ? strlen(s) : 0, (ap))) | ||||||
| #endif | #endif | ||||||
| char *str_nsave(const char *, int, Area *); | char *str_nsave(const char *, int, Area *); | ||||||
| int option(const char *); | size_t option(const char *); | ||||||
| char *getoptions(void); | char *getoptions(void); | ||||||
| void change_flag(enum sh_flag, int, int); | void change_flag(enum sh_flag, int, int); | ||||||
| int parse_args(const char **, int, int *); | int parse_args(const char **, int, int *); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user