From: Markus Armbruster <armbru@redhat.com>
To: "Dr. David Alan Gilbert (git)" <dgilbert@redhat.com>
Cc: qemu-devel@nongnu.org, pbonzini@redhat.com
Subject: Re: [Qemu-devel] [PATCH] checkpatch: Disallow glib asserts in main code
Date: Fri, 28 Apr 2017 15:34:33 +0200 [thread overview]
Message-ID: <87fugsfyfq.fsf@dusky.pond.sub.org> (raw)
In-Reply-To: <20170427165526.19836-1-dgilbert@redhat.com> (David Alan Gilbert's message of "Thu, 27 Apr 2017 17:55:26 +0100")
"Dr. David Alan Gilbert (git)" <dgilbert@redhat.com> writes:
> From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
>
> 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");
>
> Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
> ---
> scripts/checkpatch.pl | 21 +++++++++++++++++++++
> 1 file changed, 21 insertions(+)
>
> diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
> index f084542934..73cee81b79 100755
> --- a/scripts/checkpatch.pl
> +++ b/scripts/checkpatch.pl
> @@ -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
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?
next prev parent reply other threads:[~2017-04-28 13:34 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-04-27 16:55 [Qemu-devel] [PATCH] checkpatch: Disallow glib asserts in main code Dr. David Alan Gilbert (git)
2017-04-28 13:23 ` Paolo Bonzini
2017-04-28 13:34 ` Markus Armbruster [this message]
2017-04-28 13:46 ` Peter Maydell
2017-04-28 15:05 ` Eric Blake
2017-04-28 13:42 ` Daniel P. Berrange
2017-04-28 13:45 ` Dr. David Alan Gilbert
2017-04-28 15:10 ` Daniel P. Berrange
2017-04-28 15:27 ` Eric Blake
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87fugsfyfq.fsf@dusky.pond.sub.org \
--to=armbru@redhat.com \
--cc=dgilbert@redhat.com \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.