aboutsummaryrefslogtreecommitdiffstats
path: root/main/musl/0015-fix-getopt-handling-of-modifier-for-multibyte-option.patch
blob: dc41304726a70a5f99489827b451ff3bb1339932 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
From 014275b547e3059db5c45986408757c250e8198d Mon Sep 17 00:00:00 2001
From: Rich Felker <dalias@aerifal.cx>
Date: Thu, 4 Dec 2014 10:23:33 -0500
Subject: [PATCH] fix getopt handling of ':' modifier for multibyte option
 characters

the previous hard-coded offsets of +1 and +2 contained a hidden
assumption that the option character matched was single-byte, despite
this implementation of getopt attempting to support multibyte option
characters. this patch reworks the matching logic to leave the final
index pointing just past the matched character so that fixed offsets
can be used to check for ':'.
---
 src/misc/getopt.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/src/misc/getopt.c b/src/misc/getopt.c
index a698c8d..52aa7a3 100644
--- a/src/misc/getopt.c
+++ b/src/misc/getopt.c
@@ -58,7 +58,12 @@ int getopt(int argc, char * const argv[], const char *optstring)
 	if (optstring[0] == '-')
 		optstring++;
 
-	for (i=0; (l = mbtowc(&d, optstring+i, MB_LEN_MAX)) && d!=c; i+=l>0?l:1);
+	i = 0;
+	d = 0;
+	do {
+		l = mbtowc(&d, optstring+i, MB_LEN_MAX);
+		if (l>0) i+=l; else i++;
+	} while (l && d != c);
 
 	if (d != c) {
 		if (optstring[0] != ':' && opterr) {
@@ -69,8 +74,8 @@ int getopt(int argc, char * const argv[], const char *optstring)
 		}
 		return '?';
 	}
-	if (optstring[i+1] == ':') {
-		if (optstring[i+2] == ':') optarg = 0;
+	if (optstring[i] == ':') {
+		if (optstring[i+1] == ':') optarg = 0;
 		else if (optind >= argc) {
 			if (optstring[0] == ':') return ':';
 			if (opterr) {
@@ -81,7 +86,7 @@ int getopt(int argc, char * const argv[], const char *optstring)
 			}
 			return '?';
 		}
-		if (optstring[i+2] != ':' || optpos) {
+		if (optstring[i+1] != ':' || optpos) {
 			optarg = argv[optind++] + optpos;
 			optpos = 0;
 		}
-- 
2.2.0