Skip to content

Commit ea9d0e7

Browse files
committed
Check for invalid or unknown options passed via "--rc key=value"
User expected to have some effect - or would not have passed the option. Without this check, user may see some unexpected results that look a lot like tool bugs (...we ignored some option). Signed-off-by: Henry Cox <[email protected]>
1 parent abdd369 commit ea9d0e7

File tree

7 files changed

+247
-113
lines changed

7 files changed

+247
-113
lines changed

bin/geninfo

Lines changed: 51 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,10 @@ if ($^O eq "msys") {
101101
require File::Spec::Win32;
102102
}
103103

104-
# Constants
105104
our @gcov_tool;
106105

106+
# Constants
107+
107108
our $GCOV_VERSION_8_0_0 = 0x80000;
108109
our $GCOV_VERSION_4_7_0 = 0x40700;
109110

@@ -208,29 +209,16 @@ our $files_created = 0; # Number of output files created
208209
our $base_directory;
209210
our $version;
210211
our $follow;
211-
our $opt_compat_libtool;
212212
our $opt_no_compat_libtool;
213-
our $rc_adjust_src_path; # Regexp specifying parts to remove from source path
214-
our $adjust_testname;
215-
our @ignore_errors; # List of errors to ignore (parameter)
216-
our @opt_filter; # list of coverpoints to ignore (parameter)
217213
our $initial;
218-
our $captureAll; # look for both .gcda and lone .gcno files
219214
our $no_recursion = 0;
220215
our $maxdepth;
221216
our $no_markers = 0;
222217
our $opt_derive_func_data = 0;
223-
our $opt_external;
224218
our $gcov_caps;
225-
our @opt_config_file;
226-
our $opt_gcov_all_blocks = 1;
227-
our $opt_adjust_unexecuted_blocks = 0;
228-
our $opt_compat;
229219
our %compat_value;
230-
our $rc_auto_base = 1;
231-
our $rc_intermediate = "auto";
232220
our $intermediate;
233-
our $intervalMonitor; # class for progress reporting
221+
our $intervalMonitor; # class for progress reporting
234222
our $totalChildCpuTime = 0;
235223
our $intervalChildCpuTime = 0; # since last updata
236224

@@ -256,55 +244,36 @@ $SIG{__DIE__} = \&die_handler;
256244
$ENV{"LC_ALL"} = "C";
257245

258246
# retrieve settings from RC file - use these if not overridden on command line
259-
my @rc_build_dir;
260-
my %geninfo_rc_opts = (
261-
"geninfo_gcov_tool" => \@gcov_tool,
262-
"geninfo_adjust_testname" => \$adjust_testname,
263-
"geninfo_checksum" => \$lcovutil::verify_checksum,
264-
"geninfo_compat_libtool" => \$opt_compat_libtool,
265-
"geninfo_external" => \$opt_external,
266-
"geninfo_gcov_all_blocks" => \$opt_gcov_all_blocks,
267-
"geninfo_unexecuted_blocks" => \$opt_adjust_unexecuted_blocks,
268-
"geninfo_compat" => \$opt_compat,
269-
"geninfo_adjust_src_path" => \$rc_adjust_src_path,
270-
"geninfo_auto_base" => \$rc_auto_base,
271-
"geninfo_intermediate" => \$rc_intermediate,
272-
"geninfo_no_exception_branch" => \$lcovutil::exclude_exception_branch,
273-
'geninfo_chunk_size' => \$defaultChunkSize,
274-
'geninfo_interval_update' => \$defaultInterval,
275-
'geninfo_capture_all' => \$captureAll,
276-
'build_directory' => \@rc_build_dir);
277-
278247
my %geninfo_opts = ("test-name|t=s" => \$test_name,
279248
"output-filename|o=s" => \$output_filename,
280249
"base-directory|b=s" => \$base_directory,
281250
"build-directory=s" => \@build_directory,
282251
"follow|f" => \$follow,
283-
"compat-libtool" => \$opt_compat_libtool,
252+
"compat-libtool" => \$lcovutil::opt_compat_libtool,
284253
"no-compat-libtool" => \$opt_no_compat_libtool,
285254
"gcov-tool=s" => \@gcov_tool,
286255
"initial|i" => \$initial,
287-
"all" => \$captureAll,
256+
"all" => \$lcovutil::geninfo_captureAll,
288257
"no-recursion" => \$no_recursion,
289258
"no-markers" => \$no_markers,
290259
"derive-func-data" => \$opt_derive_func_data,
291-
"external|e" => \$opt_external,
260+
"external|e" => \$lcovutil::opt_external,
292261
"no-external" => \$lcovutil::opt_no_external,
293-
"compat=s" => \$opt_compat,);
262+
"compat=s" => \$lcovutil::geninfo_opt_compat,);
294263

295264
# Parse command line options
296-
if (!lcovutil::parseOptions(\%geninfo_rc_opts, \%geninfo_opts)) {
265+
if (!lcovutil::parseOptions(\%lcovutil::geninfo_rc_opts, \%geninfo_opts)) {
297266
print(STDERR "Use $tool_name --help to get usage information\n");
298267
exit(1);
299268
}
300269

301270
$buildDirSearchPath =
302271
SearchPath->new('build directory',
303-
@build_directory ? @build_directory : @rc_build_dir);
272+
@build_directory ? @build_directory : @lcovutil::rc_build_dir);
304273

305274
# Check regexp
306-
if (defined($rc_adjust_src_path)) {
307-
my ($pattern, $replace) = split(/\s*=>\s*/, $rc_adjust_src_path);
275+
if (defined($lcovutil::rc_adjust_src_path)) {
276+
my ($pattern, $replace) = split(/\s*=>\s*/, $lcovutil::rc_adjust_src_path);
308277
# If no replacement is specified, simply remove pattern
309278
$replace = '' unless defined($replace);
310279
my $p = "s#$pattern#$replace#g";
@@ -313,17 +282,17 @@ if (defined($rc_adjust_src_path)) {
313282
my $str = eval { '$test =~ ' . $p . ';' };
314283
if ($@) {
315284
lcovutil::ignorable_error($lcovutil::ERROR_FORMAT,
316-
"Invalid 'geninfo_adjust_src_path=$rc_adjust_src_path' syntax: '$@'"
285+
"Invalid 'geninfo_adjust_src_path=$lcovutil::rc_adjust_src_path' syntax: '$@'"
317286
);
318287
} else {
319288
push(@lcovutil::file_subst_patterns, [$p, 0]);
320289
}
321290
}
322291

323-
if ($captureAll && $initial) {
292+
if ($lcovutil::geninfo_captureAll && $initial) {
324293
lcovutil::ignorable_error($lcovutil::ERROR_USAGE,
325294
"'--all' ignored when '--initial' is used");
326-
$captureAll = undef;
295+
$lcovutil::geninfo_captureAll = undef;
327296
}
328297

329298
if (defined($lcovutil::tempdirname)) {
@@ -335,13 +304,13 @@ if (defined($lcovutil::tempdirname)) {
335304

336305
# Merge options
337306
if (defined($opt_no_compat_libtool)) {
338-
$opt_compat_libtool = ($opt_no_compat_libtool ? 0 : 1);
339-
$opt_no_compat_libtool = undef;
307+
$lcovutil::opt_compat_libtool = ($opt_no_compat_libtool ? 0 : 1);
308+
$opt_no_compat_libtool = undef;
340309
}
341310

342-
if (defined($opt_external)) {
311+
if (defined($lcovutil::opt_external)) {
343312
$lcovutil::opt_no_external = 0;
344-
$opt_external = undef;
313+
$lcovutil::opt_external = undef;
345314
}
346315

347316
my $start = Time::HiRes::gettimeofday();
@@ -388,18 +357,19 @@ if (system_no_output(3, @gcov_tool, "--help") == -1) {
388357
$gcov_caps = get_gcov_capabilities();
389358

390359
# Determine intermediate mode
391-
if ($rc_intermediate eq "0") {
360+
if ($lcovutil::rc_intermediate eq "0") {
392361
$intermediate = 0;
393-
} elsif ($rc_intermediate eq "1") {
362+
} elsif ($lcovutil::rc_intermediate eq "1") {
394363
$intermediate = 1;
395-
} elsif (lc($rc_intermediate) eq "auto") {
364+
} elsif (lc($lcovutil::rc_intermediate) eq "auto") {
396365
# Use intermediate format if supported by gcov and not conflicting with
397366
# exception branch exclusion
398367
$intermediate = (($gcov_caps->{'intermediate-format'} &&
399368
!$lcovutil::exclude_exception_branch) ||
400369
$gcov_caps->{'json-format'}) ? 1 : 0;
401370
} else {
402-
die("invalid value for geninfo_intermediate: " . "'$rc_intermediate'\n");
371+
die("invalid value for geninfo_intermediate: " .
372+
"'$lcovutil::rc_intermediate'\n");
403373
}
404374

405375
if ($gcov_version >= (9 << 16) &&
@@ -428,14 +398,14 @@ push(@gcov_tool, "-b")
428398
if ($gcov_caps->{'branch-probabilities'} &&
429399
($lcovutil::br_coverage ||
430400
$lcovutil::func_coverage ||
431-
$opt_adjust_unexecuted_blocks));
401+
$lcovutil::opt_adjust_unexecuted_blocks));
432402
push(@gcov_tool, "-c")
433403
if ($gcov_caps->{'branch-counts'} &&
434404
$lcovutil::br_coverage);
435405
push(@gcov_tool, "-a")
436406
if ($gcov_caps->{'all-blocks'} &&
437-
$opt_gcov_all_blocks &&
438-
$lcovutil::br_coverage &&
407+
$lcovutil::opt_gcov_all_blocks &&
408+
$lcovutil::br_coverage &&
439409
!$intermediate);
440410
if ($gcov_caps->{'hash-filenames'}) {
441411
push(@gcov_tool, "-x");
@@ -445,10 +415,10 @@ if ($gcov_caps->{'hash-filenames'}) {
445415
push(@gcov_tool, '-i') if $intermediate;
446416

447417
# Determine compatibility modes
448-
parse_compat_modes($opt_compat);
418+
parse_compat_modes($lcovutil::geninfo_opt_compat);
449419

450-
push(@opt_filter, "region", "branch_region") unless $no_markers;
451-
parse_cov_filters(@opt_filter);
420+
push(@lcovutil::opt_filter, "region", "branch_region") unless $no_markers;
421+
parse_cov_filters(@lcovutil::opt_filter);
452422

453423
# Make sure test names only contain valid characters
454424
if ($test_name =~ s/\W/_/g) {
@@ -457,7 +427,7 @@ if ($test_name =~ s/\W/_/g) {
457427
}
458428

459429
# Adjust test name to include uname output if requested
460-
if ($adjust_testname) {
430+
if ($lcovutil::geninfo_adjust_testname) {
461431
$test_name .= "__" . `uname -a`;
462432
$test_name =~ s/\W/_/g;
463433
}
@@ -1008,7 +978,8 @@ sub find_files
1008978
my ($name, $d, $e) =
1009979
File::Basename::fileparse($directory, qr/\.[^.]*/);
1010980
if ($e ne $ext &&
1011-
(!$captureAll || $e ne $graph_file_extension)) {
981+
(!$lcovutil::geninfo_captureAll || $e ne $graph_file_extension)
982+
) {
1012983
$self->[2]->{$directory} = [
1013984
$lcovutil::ERROR_USAGE,
1014985
"$directory has unsupported extension: expected '$ext'" .
@@ -1249,8 +1220,8 @@ sub gen_info(@)
12491220

12501221
$builder->find_files($initial, @_);
12511222
if (!defined($initial) &&
1252-
defined($captureAll) &&
1253-
$captureAll) {
1223+
defined($lcovutil::geninfo_captureAll) &&
1224+
$lcovutil::geninfo_captureAll) {
12541225
$builder->find_files(2, @_);
12551226
}
12561227
my $filelist = $builder->worklist();
@@ -1260,8 +1231,8 @@ sub gen_info(@)
12601231
$lcovutil::maxParallelism ?
12611232
(int($total / $lcovutil::maxParallelism)) :
12621233
1;
1263-
if (defined($defaultChunkSize)) {
1264-
if ($defaultChunkSize =~ /^(\d+)\s*(%?)$/) {
1234+
if (defined($lcovutil::defaultChunkSize)) {
1235+
if ($lcovutil::defaultChunkSize =~ /^(\d+)\s*(%?)$/) {
12651236
if (defined($2) && $2) {
12661237
# a percentage
12671238
$chunkSize = int($total * $1 / 100);
@@ -1271,7 +1242,8 @@ sub gen_info(@)
12711242
}
12721243
} else {
12731244
lcovutil::ignorable_error($lcovutil::ERROR_FORMAT,
1274-
"geninfo_chunk_size '$defaultChunkSize' is not recognized");
1245+
"geninfo_chunk_size '$lcovutil::defaultChunkSize' is not recognized"
1246+
);
12751247
}
12761248
}
12771249
# Need to balance time in child vs. time to merge child data -
@@ -1307,9 +1279,9 @@ sub gen_info(@)
13071279
my $processedChunks = 0;
13081280

13091281
# process at least 5% of files before printing stats
1310-
$defaultInterval = 5 unless defined($defaultInterval);
1282+
$lcovutil::defaultInterval = 5 unless defined($lcovutil::defaultInterval);
13111283

1312-
my $intervalLength = int($total * $defaultInterval / 100);
1284+
my $intervalLength = int($total * $lcovutil::defaultInterval / 100);
13131285
my $start = Time::HiRes::gettimeofday();
13141286

13151287
$intervalMonitor =
@@ -1476,8 +1448,10 @@ sub gen_info(@)
14761448
--$currentParallel;
14771449
_merge_one_child($child, \%children, $tempFileExt, $childstatus);
14781450
}
1479-
info("Finished processing %d " .
1480-
($initial ? 'GCNO' : ($captureAll ? 'GCDA/GCNO' : 'GCDA')) .
1451+
info("Finished processing %d "
1452+
.
1453+
($initial ? 'GCNO' :
1454+
($lcovutil::geninfo_captureAll ? 'GCDA/GCNO' : 'GCDA')) .
14811455
" file%s\n",
14821456
$processedFiles,
14831457
1 == $processedFiles ? '' : 's');
@@ -1646,7 +1620,7 @@ sub process_dafile($$$$)
16461620
($instr, $graph) = read_gcno($bb_filename);
16471621

16481622
# Try to find base directory automatically if requested by user
1649-
if ($rc_auto_base) {
1623+
if ($lcovutil::rc_auto_base) {
16501624
$base_dir = find_base_from_source($base_dir,
16511625
[keys(%{$instr}), keys(%{$graph})]);
16521626
}
@@ -1894,7 +1868,7 @@ sub process_dafile($$$$)
18941868
lcovutil::debug(
18951869
"$source_filename:$line_number: unexecuted block on non-branch line with count=$hit\n"
18961870
);
1897-
if ($opt_adjust_unexecuted_blocks) {
1871+
if ($lcovutil::opt_adjust_unexecuted_blocks) {
18981872
$hit = 0;
18991873
} else {
19001874
lcovutil::ignorable_warning(
@@ -2597,7 +2571,7 @@ sub intermediate_json_to_info($)
25972571
lcovutil::debug(
25982572
"$filename:$line: unexecuted block on non-branch line with count=$count\n"
25992573
);
2600-
if ($opt_adjust_unexecuted_blocks) {
2574+
if ($lcovutil::opt_adjust_unexecuted_blocks) {
26012575
$count = 0;
26022576
} else {
26032577
lcovutil::ignorable_warning($ERROR_INCONSISTENT_DATA,
@@ -2875,7 +2849,7 @@ sub process_intermediate($$$$)
28752849
}
28762850

28772851
# Try to find base directory automatically if requested by user
2878-
if ($rc_auto_base) {
2852+
if ($lcovutil::rc_auto_base) {
28792853
$base = find_base_from_source($base, [keys(%data)]);
28802854
}
28812855
}
@@ -3061,7 +3035,7 @@ sub process_graphfile($$)
30613035
($instr, $graph) = read_gcno($graph_filename);
30623036

30633037
# Try to find base directory automatically if requested by user
3064-
if ($rc_auto_base) {
3038+
if ($lcovutil::rc_auto_base) {
30653039
$base_dir = find_base_from_source($base_dir,
30663040
[keys(%{$instr}), keys(%{$graph})]);
30673041
}
@@ -4010,9 +3984,9 @@ sub parse_compat_modes($)
40103984
%compat_value = %COMPAT_MODE_DEFAULTS;
40113985

40123986
# Add old style specifications
4013-
if (defined($opt_compat_libtool)) {
3987+
if (defined($lcovutil::opt_compat_libtool)) {
40143988
$compat_value{$COMPAT_MODE_LIBTOOL} =
4015-
$opt_compat_libtool ? $COMPAT_VALUE_ON :
3989+
$lcovutil::opt_compat_libtool ? $COMPAT_VALUE_ON :
40163990
$COMPAT_VALUE_OFF;
40173991
}
40183992

bin/lcov

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,11 @@ my %lcov_options = ("directory|d|di=s" => \@directory,
249249
'intersect=s' => \@intersect,
250250
'subtract=s' => \@difference,);
251251

252+
# geninfo args might get passed to lcov for --capture mode - so we need to not croak on them
253+
my %mergedRcOpts = (%lcov_rc_params, %lcovutil::geninfo_rc_opts);
254+
252255
# Parse command line options
253-
if (!lcovutil::parseOptions(\%lcov_rc_params, \%lcov_options)) {
256+
if (!lcovutil::parseOptions(\%mergedRcOpts, \%lcov_options)) {
254257
print(STDERR "Use $tool_name --help to get usage information\n");
255258
exit(1);
256259
}

0 commit comments

Comments
 (0)