Browse Source

More robust example of numeric argument handling. The old example

code would dereference NULL for mixed letter and number args.
OK deraadt@
OPENBSD_3_8
millert 20 years ago
parent
commit
6ec8c4af59
1 changed files with 23 additions and 11 deletions
  1. +23
    -11
      src/lib/libc/stdlib/getopt.3

+ 23
- 11
src/lib/libc/stdlib/getopt.3 View File

@ -25,7 +25,7 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE. .\" SUCH DAMAGE.
.\" .\"
.\" $OpenBSD: getopt.3,v 1.31 2005/03/26 22:02:15 millert Exp $ .\" $OpenBSD: getopt.3,v 1.32 2005/07/01 02:11:55 millert Exp $
.\" .\"
.Dd December 17, 2002 .Dd December 17, 2002
.Dt GETOPT 3 .Dt GETOPT 3
@ -351,22 +351,34 @@ as an option.
This practice is wrong, and should not be used in any current development. This practice is wrong, and should not be used in any current development.
It is provided for backward compatibility It is provided for backward compatibility
.Em only . .Em only .
The following code fragment works in most cases. The following code fragment works in most cases and can handle mixed
number and letter arguments.
.Bd -literal -offset indent .Bd -literal -offset indent
int ch; int aflag = 0, bflag = 0, ch, lastch = '\e0';
long length; int length = -1, newarg = 1, prevoptind = 1;
char *p;
while ((ch = getopt(argc, argv, "0123456789")) != -1) { while ((ch = getopt(argc, argv, "0123456789ab")) != -1) {
switch (ch) { switch (ch) {
case '0': case '1': case '2': case '3': case '4': case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9': case '5': case '6': case '7': case '8': case '9':
p = argv[optind - 1]; if (newarg || !isdigit(lastch))
if (p[0] == '-' && p[1] == ch && !p[2]) length = 0;
length = ch - '0'; else if (length > INT_MAX / 10)
else usage();
length = strtol(argv[optind] + 1, NULL, 10); length = (length * 10) + (ch - '0');
break;
case 'a':
aflag = 1;
break; break;
case 'b':
bflag = 1;
break;
default:
usage();
} }
lastch = ch;
newarg = optind != prevoptind;
prevoptind = optind;
} }
.Ed .Ed

|||||||
|||||||
xxxxxxxxxx
 
000:0
x
 
000:0
Loading…
Cancel
Save