Skip to content

Commit

Permalink
checkpatch: Disallow glib asserts in main code
Browse files Browse the repository at this point in the history
Glib commit a6a875068779 (from 2013) made many of the glib assert
macros non-fatal if a flag is set.
This causes two problems:
  a) Compilers moan that your code is unsafe even though you've
     put an assert in before the point of use.
  b) Someone evil could, in a library, call
     g_test_set_nonfatal_assertions() and cause our assertions in
     important places not to fail and potentially allow memory overruns.

Ban most of the glib assertion functions (basically everything except
g_assert and g_assert_not_reached) except in tests/

This makes checkpatch gives an error such as:

  ERROR: Use g_assert or g_assert_not_reached
  qemu#77: FILE: vl.c:4725:
  +    g_assert_cmpstr("Chocolate", >, "Cheese");

Signed-off-by: Dr. David Alan Gilbert <[email protected]>
Message-Id: <[email protected]>
Signed-off-by: Paolo Bonzini <[email protected]>
  • Loading branch information
dagrh authored and bonzini committed May 5, 2017
1 parent 31f5a72 commit 6e93895
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions scripts/checkpatch.pl
Original file line number Diff line number Diff line change
Expand Up @@ -2571,6 +2571,27 @@ sub process {
if ($line =~ /\bbzero\(/) {
ERROR("use memset() instead of bzero()\n" . $herecurr);
}
my $non_exit_glib_asserts = qr{g_assert_cmpstr|
g_assert_cmpint|
g_assert_cmpuint|
g_assert_cmphex|
g_assert_cmpfloat|
g_assert_true|
g_assert_false|
g_assert_nonnull|
g_assert_null|
g_assert_no_error|
g_assert_error|
g_test_assert_expected_messages|
g_test_trap_assert_passed|
g_test_trap_assert_stdout|
g_test_trap_assert_stdout_unmatched|
g_test_trap_assert_stderr|
g_test_trap_assert_stderr_unmatched}x;
if ($realfile !~ /^tests\// &&
$line =~ /\b(?:$non_exit_glib_asserts)\(/) {
ERROR("Use g_assert or g_assert_not_reached\n". $herecurr);
}
}

# If we have no input at all, then there is nothing to report on
Expand Down

0 comments on commit 6e93895

Please sign in to comment.