Skip to content

Commit 095bad1

Browse files
committed
main.cpp: improved handling of empty options
1 parent ad24c6e commit 095bad1

File tree

1 file changed

+38
-10
lines changed

1 file changed

+38
-10
lines changed

main.cpp

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <fstream>
1111
#include <iostream>
1212
#include <string>
13+
#include <utility>
1314
#include <vector>
1415

1516
int main(int argc, char **argv)
@@ -30,49 +31,76 @@ int main(int argc, char **argv)
3031
const char c = arg[1];
3132
switch (c) {
3233
case 'D': { // define symbol
34+
found = true;
3335
const char * const value = arg[2] ? (argv[i] + 2) : argv[++i];
36+
if (!value) {
37+
std::cout << "error: option -D with no value." << std::endl;
38+
error = true;
39+
break;
40+
}
3441
dui.defines.push_back(value);
35-
found = true;
3642
break;
3743
}
3844
case 'U': { // undefine symbol
45+
found = true;
3946
const char * const value = arg[2] ? (argv[i] + 2) : argv[++i];
47+
if (!value) {
48+
std::cout << "error: option -U with no value." << std::endl;
49+
error = true;
50+
break;
51+
}
4052
dui.undefined.insert(value);
41-
found = true;
4253
break;
4354
}
4455
case 'I': { // include path
45-
const char * const value = arg[2] ? (argv[i] + 2) : argv[++i];
46-
dui.includePaths.push_back(value);
4756
found = true;
57+
const char * const value = arg[2] ? (arg + 2) : argv[++i];
58+
if (!value) {
59+
std::cout << "error: option -I with no value." << std::endl;
60+
error = true;
61+
break;
62+
}
63+
dui.includePaths.push_back(value);
4864
break;
4965
}
5066
case 'i':
5167
if (std::strncmp(arg, "-include=",9)==0) {
52-
dui.includes.push_back(arg+9);
5368
found = true;
69+
std::string value = arg + 9;
70+
if (value.empty()) {
71+
std::cout << "error: option -include with no value." << std::endl;
72+
error = true;
73+
break;
74+
}
75+
dui.includes.push_back(std::move(value));
5476
} else if (std::strncmp(arg, "-is",3)==0) {
55-
use_istream = true;
5677
found = true;
78+
use_istream = true;
5779
}
5880
break;
5981
case 's':
6082
if (std::strncmp(arg, "-std=",5)==0) {
61-
dui.std = arg + 5;
6283
found = true;
84+
std::string value = arg + 5;
85+
if (value.empty()) {
86+
std::cout << "error: option -std with no value." << std::endl;
87+
error = true;
88+
break;
89+
}
90+
dui.std = std::move(value);
6391
}
6492
break;
6593
case 'q':
66-
quiet = true;
6794
found = true;
95+
quiet = true;
6896
break;
6997
case 'e':
70-
error_only = true;
7198
found = true;
99+
error_only = true;
72100
break;
73101
case 'f':
74-
fail_on_error = true;
75102
found = true;
103+
fail_on_error = true;
76104
break;
77105
}
78106
if (!found) {

0 commit comments

Comments
 (0)