Skip to content

Commit 83f104a

Browse files
committed
strtof, strtol
1 parent 7cefb74 commit 83f104a

File tree

1 file changed

+63
-25
lines changed

1 file changed

+63
-25
lines changed

misc.c++

+63-25
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,39 @@ extern int vfSoftClip;
7878
extern int vfLimitClip;
7979
extern void SetSoundIn(int fSoundIn);
8080

81+
// For parsing numbers from argv[] in ParseArgs(),
82+
// more robustly than atoi() and atof().
83+
// On error, simply exit(1).
84+
int parseInt(const char* s)
85+
{
86+
const auto saved = errno;
87+
errno = 0;
88+
char* tmp;
89+
const auto val = strtol(s, &tmp, 0);
90+
if (tmp == s || *tmp != '\0' || errno == ERANGE || errno == EINVAL) {
91+
std::cerr << "Failed to parse int from '" << s << "'.\n";
92+
exit(1);
93+
}
94+
if (errno == 0)
95+
errno = saved;
96+
return int(val);
97+
}
98+
99+
double parseFloat(const char* s)
100+
{
101+
const auto saved = errno;
102+
errno = 0;
103+
char* tmp;
104+
const auto val = strtod(s, &tmp);
105+
if (tmp == s || *tmp != '\0' || errno == ERANGE || errno == EINVAL) {
106+
std::cerr << "Failed to parse float from '" << s << "'.\n";
107+
exit(1);
108+
}
109+
if (errno == 0)
110+
errno = saved;
111+
return val;
112+
}
113+
81114
void ParseArgs(int argc,char *argv[],int * /*udp_port*/, int *liveaudio,
82115
float *sample_rate, int *nchansVSS, int *nchansIn, int *nchansOut, int *hog, int * /*lwm*/, int * /*hwm*/, char* ofile)
83116
{
@@ -92,9 +125,10 @@ void ParseArgs(int argc,char *argv[],int * /*udp_port*/, int *liveaudio,
92125
if(strcmp(*argv, "-srate")==0)
93126
{
94127
argc--; argv++;++nargs;
95-
if(argc>0)
96-
{
97-
*sample_rate = atof(*argv);
128+
if (argc <= 0)
129+
cerr << "vss: ignoring -srate with missing value.\n";
130+
else {
131+
*sample_rate = parseFloat(*argv);
98132
if (*sample_rate < 8000.)
99133
{
100134
cerr << "vss: boosting sampling rate to 8000 from " << *sample_rate << "\n";
@@ -111,9 +145,10 @@ void ParseArgs(int argc,char *argv[],int * /*udp_port*/, int *liveaudio,
111145
else if(strcmp(*argv, "-hog")==0)
112146
{
113147
argc--; argv++;++nargs;
114-
if(argc>0)
115-
{
116-
*hog = atoi(*argv);
148+
if (argc <= 0)
149+
cerr << "vss: ignoring -hog with missing value.\n";
150+
else {
151+
*hog = parseInt(*argv);
117152
argc--; argv++;++nargs;
118153
}
119154
}
@@ -144,18 +179,17 @@ void ParseArgs(int argc,char *argv[],int * /*udp_port*/, int *liveaudio,
144179
else if(strcmp(*argv, "-port")==0)
145180
{
146181
argc--; argv++;++nargs;
147-
if(argc>0)
148-
{
149-
globs.udp_port = atoi(*argv);
182+
if (argc <= 0)
183+
cerr << "vss: ignoring -port with missing value.\n";
184+
else {
185+
globs.udp_port = parseInt(*argv);
150186
if (globs.udp_port < 1000 || globs.udp_port > 65535)
151187
{
152188
cerr << "vss: port number " << globs.udp_port << " out of range [1000, 65535]. Using default 7999.\n";
153189
globs.udp_port = 7999;
154190
}
155191
argc--; argv++;++nargs;
156192
}
157-
else
158-
cerr << "vss: ignoring -port with missing number.\n";
159193
}
160194
else if(strcmp(*argv, "-soft")==0)
161195
{
@@ -173,9 +207,9 @@ void ParseArgs(int argc,char *argv[],int * /*udp_port*/, int *liveaudio,
173207
argc--; argv++;++nargs;
174208
if(argc>1)
175209
{
176-
int lwm = atoi(*argv);
210+
const auto lwm = parseInt(*argv);
177211
argc--; argv++;++nargs;
178-
int hwm = atoi(*argv);
212+
const auto hwm = parseInt(*argv);
179213
argc--; argv++;++nargs;
180214
if (0 < lwm && lwm < hwm)
181215
{
@@ -187,7 +221,7 @@ void ParseArgs(int argc,char *argv[],int * /*udp_port*/, int *liveaudio,
187221
}
188222
else
189223
{
190-
cerr << "vss: ignoring -latency with missing numbers.\n";
224+
cerr << "vss: ignoring -latency with missing values.\n";
191225
if (argc>0)
192226
{
193227
argc--; argv++;++nargs;
@@ -197,9 +231,10 @@ void ParseArgs(int argc,char *argv[],int * /*udp_port*/, int *liveaudio,
197231
else if(strcmp(*argv, "-antidropout")==0)
198232
{
199233
argc--; argv++;++nargs;
200-
if(argc>0)
201-
{
202-
float msec = atof(*argv);
234+
if (argc <= 0)
235+
cerr << "vss: ignoring -antidropout with missing value.\n";
236+
else {
237+
const auto msec = parseFloat(*argv);
203238
if (msec > 0.)
204239
globs.msecAntidropout = msec;
205240
else
@@ -211,9 +246,10 @@ void ParseArgs(int argc,char *argv[],int * /*udp_port*/, int *liveaudio,
211246
{
212247
argc--; argv++;++nargs;
213248
SetSoundIn(true);
214-
if (argc>0)
215-
{
216-
const auto w = atoi(*argv);
249+
if (argc <= 0)
250+
cerr << "vss: ignoring -input with missing value.\n";
251+
else {
252+
const auto w = parseInt(*argv);
217253
if (w > 0)
218254
{
219255
// it really was "-input 4", not "-input -graphoutput" (for example).
@@ -225,17 +261,19 @@ void ParseArgs(int argc,char *argv[],int * /*udp_port*/, int *liveaudio,
225261
else if(strcmp(*argv, "-ofile")==0)
226262
{
227263
argc--; argv++;++nargs;
228-
if(argc>0)
229-
{
264+
if (argc <= 0)
265+
cerr << "vss: ignoring -ofile with missing value.\n";
266+
else {
230267
strcpy(ofile, *argv);
231268
argc--; argv++;++nargs;
232269
}
233270
}
234271
else if(strcmp(*argv, "-chans")==0)
235272
{
236273
argc--; argv++;++nargs;
237-
if(argc>0)
238-
{
274+
if (argc <= 0)
275+
cerr << "vss: ignoring -chans with missing value.\n";
276+
else {
239277
if (strstr(*argv, "into"))
240278
{
241279
// Fancy "-chans 8into4:0,1,2,3" parsing.
@@ -275,7 +313,7 @@ LDoneChans:;
275313
else
276314
{
277315
// Simple case "-chans x".
278-
*nchansVSS = *nchansOut = atoi(*argv);
316+
*nchansVSS = *nchansOut = parseInt(*argv);
279317
globs.fRemappedOutput = 0;
280318
}
281319
argc--; argv++;++nargs;

0 commit comments

Comments
 (0)