Skip to contentSkip to conversation

checkpatch: Disallow glib asserts in main code

PATCH Applied Dr. David Alan Gilbert (git) 6 participants 9 messages Apr 27, 2017 – Apr 28, 2017 Checkpatch

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 checkpatch: Disallow glib asserts in main code

From: "Dr. David Alan Gilbert" <[email protected]>

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
#77: FILE: vl.c:4725:
+ g_assert_cmpstr("Chocolate", >, "Cheese");

---
 scripts/checkpatch.pl | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
scripts/checkpatch.pl +21 -0
index f084542934..73cee81b79 100755
@@ -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
Signature · 1 line
2.12.2
#1 lore raw

----- Original Message -----

Quoted 63 lines

From: "Dr. David Alan Gilbert (git)" <[email protected]>
To: [email protected], [email protected], [email protected]
Sent: Thursday, April 27, 2017 6:55:26 PM
Subject: [Qemu-devel] [PATCH] checkpatch: Disallow glib asserts in main code

From: "Dr. David Alan Gilbert" <[email protected]>

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
#77: FILE: vl.c:4725:
+ g_assert_cmpstr("Chocolate", >, "Cheese");

---
 scripts/checkpatch.pl | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
scripts/checkpatch.pl +21 -1
index f084542934..73cee81b79 100755
@@ -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
--
Quoted 4 lines

2.12.2

Queued, thanks.

Paolo

#2 lore raw
In reply to Dr. David Alan Gilbert (git)
Quoted 47 lines · Dr. David Alan Gilbert (git)

From: "Dr. David Alan Gilbert" <[email protected]>

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
#77: FILE: vl.c:4725:
+ g_assert_cmpstr("Chocolate", >, "Cheese");

---
 scripts/checkpatch.pl | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
scripts/checkpatch.pl +14 -0
index f084542934..73cee81b79 100755
@@ -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|

scripts/checkpatch.pl sub process { · line 2597

Quoted 10 lines
scripts/checkpatch.pl +7 -0
+						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

If these are screwy enough to warrant rejecting them in new code, they're probably screwy enough to purge them from existing code:

    $ git-grep -E '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' | grep -v ^tests/
    hw/ide/ahci.c:        g_assert_cmpint(size, >, 1);
    hw/ppc/spapr_ovec.c:    g_assert_cmpint(bitnr, <, OV_MAXBITS);
    hw/ppc/spapr_ovec.c:    g_assert_cmpint(bitnr, <, OV_MAXBITS);
    hw/ppc/spapr_ovec.c:    g_assert_cmpint(bitnr, <, OV_MAXBITS);
    hw/ppc/spapr_ovec.c:    g_assert_cmpint(vector, >=, 1); /* vector numbering starts at 1 */
    hw/ppc/spapr_ovec.c:    g_assert_cmpint(vector_len, <=, OV_MAXBYTES);
    hw/ppc/spapr_ovec.c:    g_assert_cmpint(vec_len, <=, OV_MAXBYTES);
    include/glib-compat.h:#ifndef g_assert_true
    include/glib-compat.h:#define g_assert_true(expr)                                                    \
    include/glib-compat.h:#ifndef g_assert_false
    include/glib-compat.h:#define g_assert_false(expr)                                                   \
    include/glib-compat.h:#ifndef g_assert_null
    include/glib-compat.h:#define g_assert_null(expr)                                                    \
    include/glib-compat.h:#ifndef g_assert_nonnull
    include/glib-compat.h:#define g_assert_nonnull(expr)                                                 \
    qom/object.c:        g_assert_cmpint(parent->class_size, <=, ti->class_size);
    qom/object.c:    g_assert_cmpint(type->instance_size, >=, sizeof(Object));
    qom/object.c:    g_assert_cmpint(size, >=, type->instance_size);
    qom/object.c:    g_assert_cmpint(obj->ref, ==, 0);
    qom/object.c:    g_assert_cmpint(obj->ref, >, 0);
    scripts/cocci-macro-file.h:#define g_assert_cmpint(a, op, b)   g_assert(a op b)
    scripts/cocci-macro-file.h:#define g_assert_cmpuint(a, op, b)   g_assert(a op b)
    scripts/cocci-macro-file.h:#define g_assert_cmphex(a, op, b)   g_assert(a op b)
    scripts/cocci-macro-file.h:#define g_assert_cmpstr(a, op, b)   g_assert(strcmp(a, b) op 0)
    util/qht.c:    g_assert_cmpuint(new->n_buckets, !=, old->n_buckets);

Volunteers?

#5 lore raw
In reply to Dr. David Alan Gilbert (git)
Quoted 11 lines · Dr. David Alan Gilbert (git)

From: "Dr. David Alan Gilbert" <[email protected]>

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.

Quoted 8 lines

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
#77: FILE: vl.c:4725:
+ g_assert_cmpstr("Chocolate", >, "Cheese");

Or could we perhaps instead undo the damage via a hack like

#define g_assert_cmpint g_assert_cmpint_orig

 #define g_assert_cmpint(x, y, z) \
     g_assert_cmpint_orig(x, y,x); \
     abort()

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#6 lore raw
Quoted 21 lines · * Daniel P. Berrange ([email protected]) wrote:
Quoted 11 lines · Dr. David Alan Gilbert (git)

From: "Dr. David Alan Gilbert" <[email protected]>

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.

Quoted 8 lines

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
#77: FILE: vl.c:4725:
+ g_assert_cmpstr("Chocolate", >, "Cheese");

Quoted 6 lines

Or could we perhaps instead undo the damage via a hack like

#define g_assert_cmpint g_assert_cmpint_orig

 #define g_assert_cmpint(x, y, z) \
     g_assert_cmpint_orig(x, y,x); \
     abort()

I'd be kind of OK adding a q_assert_cmpint if you wanted, but I think we shouldn't change the semantics of a public name.

Dave

Quoted 7 lines

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#3 lore raw
In reply to Markus Armbruster
Quoted 6 lines · Markus Armbruster

If these are screwy enough to warrant rejecting them in new code, they're probably screwy enough to purge them from existing code:

include/glib-compat.h:#ifndef g_assert_true include/glib-compat.h:#define g_assert_true(expr)

These ones prompted me to wonder if we could/should use #pragma GCC poison g_assert_true

rather than just leaving it up to checkpatch.

thanks
-- PMM

#4 lore raw
In reply to Markus Armbruster
Quoted 3 lines · Markus Armbruster
Quoted 3 lines

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

You'll also want to exclude scripts/ and possible include/glib-compat.h...

Quoted 11 lines

If these are screwy enough to warrant rejecting them in new code, they're probably screwy enough to purge them from existing code:

    $ git-grep -E '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' | grep -v ^tests/
    hw/ide/ahci.c:        g_assert_cmpint(size, >, 1);
    hw/ppc/spapr_ovec.c:    g_assert_cmpint(bitnr, <, OV_MAXBITS);
    hw/ppc/spapr_ovec.c:    g_assert_cmpint(bitnr, <, OV_MAXBITS);
    hw/ppc/spapr_ovec.c:    g_assert_cmpint(bitnr, <, OV_MAXBITS);
    hw/ppc/spapr_ovec.c:    g_assert_cmpint(vector, >=, 1); /* vector numbering starts at 1 */
    hw/ppc/spapr_ovec.c:    g_assert_cmpint(vector_len, <=, OV_MAXBYTES);
    hw/ppc/spapr_ovec.c:    g_assert_cmpint(vec_len, <=, OV_MAXBYTES);

Those should go.

Quoted 8 lines
    include/glib-compat.h:#ifndef g_assert_true
    include/glib-compat.h:#define g_assert_true(expr)                                                    \
    include/glib-compat.h:#ifndef g_assert_false
    include/glib-compat.h:#define g_assert_false(expr)                                                   \
    include/glib-compat.h:#ifndef g_assert_null
    include/glib-compat.h:#define g_assert_null(expr)                                                    \
    include/glib-compat.h:#ifndef g_assert_nonnull
    include/glib-compat.h:#define g_assert_nonnull(expr)                                                 \

We still need these until we can require glib 2.38 or even 2.40.

Quoted 5 lines
    qom/object.c:        g_assert_cmpint(parent->class_size, <=, ti->class_size);
    qom/object.c:    g_assert_cmpint(type->instance_size, >=, sizeof(Object));
    qom/object.c:    g_assert_cmpint(size, >=, type->instance_size);
    qom/object.c:    g_assert_cmpint(obj->ref, ==, 0);
    qom/object.c:    g_assert_cmpint(obj->ref, >, 0);

These should go.

Quoted 4 lines

scripts/cocci-macro-file.h:#define g_assert_cmpint(a, op, b) g_assert(a op b)
scripts/cocci-macro-file.h:#define g_assert_cmpuint(a, op, b) g_assert(a op b)
scripts/cocci-macro-file.h:#define g_assert_cmphex(a, op, b) g_assert(a op b)
scripts/cocci-macro-file.h:#define g_assert_cmpstr(a, op, b) g_assert(strcmp(a, b) op 0)

These must stay permanently.

Quoted 5 lines
    util/qht.c:    g_assert_cmpuint(new->n_buckets, !=, old->n_buckets);

Volunteers?

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#7 lore raw
In reply to Dr. David Alan Gilbert
Quoted 24 lines · Dr. David Alan Gilbert
Quoted 23 lines · * Daniel P. Berrange ([email protected]) wrote:
Quoted 11 lines · Dr. David Alan Gilbert (git)

From: "Dr. David Alan Gilbert" <[email protected]>

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.

Quoted 8 lines

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
#77: FILE: vl.c:4725:
+ g_assert_cmpstr("Chocolate", >, "Cheese");

Or could we perhaps instead undo the damage via a hack like

Quoted 8 lines
Quoted 4 lines

#define g_assert_cmpint g_assert_cmpint_orig

 #define g_assert_cmpint(x, y, z) \
     g_assert_cmpint_orig(x, y,x); \
     abort()

I'd be kind of OK adding a q_assert_cmpint if you wanted, but I think we shouldn't change the semantics of a public name.

Personally I think it would be worth having them - the whole point of these more specific g_assert_* macros is that they provide clearer error messages when they're triggered, so I prefer their use generally

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#8 lore raw
Quoted 6 lines · Daniel P. Berrange
Quoted 6 lines
Quoted 6 lines

Or could we perhaps instead undo the damage via a hack like

#define g_assert_cmpint g_assert_cmpint_orig

 #define g_assert_cmpint(x, y, z) \
     g_assert_cmpint_orig(x, y,x); \
     abort()

Not quite the right hack (we don't want to unconditionally abort, but only when the condition fails).

Quoted 4 lines
Quoted 4 lines

I'd be kind of OK adding a q_assert_cmpint if you wanted, but I think we shouldn't change the semantics of a public name.

I tend to agree there; having our own distinct name means that we can see at a glance that our version will quit, no matter what the glib version does.

Quoted 4 lines

Personally I think it would be worth having them - the whole point of these more specific g_assert_* macros is that they provide clearer error messages when they're triggered, so I prefer their use generally

I agree that the improved error messages part is worthwhile. So maybe we want:

#define q_assert_cmpint(x, y, z) \

  do { \
    g_assert_cmpint(x, y, z); \
    assert(x y z); \

} while (0)

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org