Skip to content

Commit 8b17e09

Browse files
committed
check for valid integer command line parameters (#484)
1 parent adc4280 commit 8b17e09

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ project (libde265
55
VERSION 1.0.16
66
)
77

8-
set(CMAKE_CXX_STANDARD 11)
8+
set(CMAKE_CXX_STANDARD 17)
99
set(CMAKE_CXX_STANDARD_REQUIRED ON)
1010
set(CMAKE_CXX_EXTENSIONS OFF)
1111
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

dec265/dec265.cc

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
#define DO_MEMORY_LOGGING 0
2828

2929
#include "de265.h"
30+
#include <stdexcept>
31+
#include <iostream>
32+
#include <optional>
33+
3034
#ifdef HAVE_CONFIG_H
3135
#include "config.h"
3236
#endif
@@ -563,6 +567,40 @@ void (*volatile __malloc_initialize_hook)(void) = init_my_hooks;
563567
#endif
564568

565569

570+
int parse_param(const char* arg, std::optional<int> lower_bound, std::optional<int> upper_bound, const char* arg_name)
571+
{
572+
int value;
573+
574+
try {
575+
size_t len;
576+
value = std::stoi(optarg, &len);
577+
if (arg[len] != 0) {
578+
std::cerr << "invalid argument to " << arg_name << "\n";
579+
exit(5);
580+
}
581+
} catch (std::invalid_argument const& ex) {
582+
std::cerr << "invalid argument to " << arg_name << "\n";
583+
exit(5);
584+
}
585+
catch (std::out_of_range const& ex) {
586+
std::cerr << "argument to -T is out of range\n";
587+
exit(5);
588+
}
589+
590+
if (lower_bound && value < *lower_bound) {
591+
std::cerr << "argument to " << arg_name << " may not be smaller than " << *lower_bound << "\n";
592+
exit(5);
593+
}
594+
595+
if (upper_bound && value > *upper_bound) {
596+
std::cerr << "argument to " << arg_name << " may not be larger than " << *upper_bound << "\n";
597+
exit(5);
598+
}
599+
600+
return value;
601+
}
602+
603+
566604
int main(int argc, char** argv)
567605
{
568606
while (1) {
@@ -578,9 +616,9 @@ int main(int argc, char** argv)
578616

579617
switch (c) {
580618
case 'q': quiet++; break;
581-
case 't': nThreads=atoi(optarg); break;
619+
case 't': nThreads=parse_param(optarg, 0, std::nullopt, "-t"); break;
582620
case 'c': check_hash=true; break;
583-
case 'f': max_frames=atoi(optarg); break;
621+
case 'f': max_frames=parse_param(optarg, 1, std::nullopt, "-f"); break;
584622
case 'o': write_yuv=true; output_filename=optarg; break;
585623
case 'h': show_help=true; break;
586624
case 'd': dump_headers=true; break;
@@ -592,7 +630,7 @@ int main(int argc, char** argv)
592630
case 'm': measure_quality=true; reference_filename=optarg; break;
593631
case 's': show_ssim_map=true; break;
594632
case 'e': show_psnr_map=true; break;
595-
case 'T': highestTID=atoi(optarg); break;
633+
case 'T': highestTID = parse_param(optarg, 0, std::nullopt, "-T"); break;
596634
case 'v': verbosity++; break;
597635
}
598636
}

0 commit comments

Comments
 (0)