All of lore.kernel.org
 help / color / mirror / Atom feed
From: Markus Armbruster <armbru@redhat.com>
To: Claudio Fontana <cfontana@suse.de>
Cc: "Daniel P. Berrangé" <berrange@redhat.com>,
	"Philippe Mathieu-Daudé" <f4bug@amsat.org>,
	"Paolo Bonzini" <pbonzini@redhat.com>,
	"Richard Henderson" <richard.henderson@linaro.org>,
	"Kevin Wolf" <kwolf@redhat.com>,
	qemu-devel@nongnu.org, dinechin@redhat.com,
	"Gerd Hoffmann" <kraxel@redhat.com>,
	"Marc-André Lureau" <marcandre.lureau@redhat.com>
Subject: Re: [PATCH v4 2/3] module: add Error arguments to module_load_one and module_load_qom_one
Date: Fri, 23 Sep 2022 07:31:50 +0200	[thread overview]
Message-ID: <87edw2hd15.fsf@pond.sub.org> (raw)
In-Reply-To: <a30470e4-a861-c88e-e1ef-dfeae3352ad6@suse.de> (Claudio Fontana's message of "Thu, 22 Sep 2022 17:22:11 +0200")

Claudio Fontana <cfontana@suse.de> writes:

> On 9/22/22 16:36, Markus Armbruster wrote:
>> Claudio Fontana <cfontana@suse.de> writes:
>> 
>>> On 9/22/22 15:20, Markus Armbruster wrote:
>>>> Claudio Fontana <cfontana@suse.de> writes:
>>>>
>>>> [...]
>>>>
>>>>> I think it would be better to completely make the return value separate from the Error,
>>>>> and really treat Error as an exception and not mix it up with the regular execution,
>>>>>
>>>>> but if it is the general consensus that I am the only one seeing this conflation problem we can model it this way too.
>>>>
>>>> It's a matter of language pragmatics.  In Java, you throw an exception
>>>> on error.  In C, you return an error value.
>>>>
>>>> Trying to emulate exceptions in C might be even more unadvisable than
>>>> trying to avoid them in Java.  Best to work with the language, not
>>>> against it.
>>>>
>>>> Trouble is the error values we can conveniently return in C can't convey
>>>> enough information.  So we use Error for that.  Just like GLib uses
>>>
>>> Right, we use Error for that and that's fine, but we should use it _only Error_ for that.
>>>
>>> Ie map the Exceptions directly to Error.
>>>
>>> So:
>>>
>>> try {
>>>
>>>   rv = function_call(...);
>>>
>>>   use_return_value(rv);
>>>
>>> } catch (Exception e) {
>>>
>>>   /* handle exceptional case */
>>>
>>> }
>>>
>>> becomes
>>>
>>> rv = function_call(..., Error **errp);
>>>
>>> if (errp) {
>>>   /* handle exceptional case */
>>> }
>>>
>>> use_return_value(rv);
>> 
>> This is simply not the intended use of Error.
>> 
>> Also, "trying to emulate exceptions in C might be even more unadvisable
>> than trying to avoid them in Java."
>> 
>>> Instead we mix up the Exception code path and the regular code path, by having rv carry a mix of the Exception and regular return value,
>>> and this creates problems and confusion.
>> 
>> "In C, you return an error value."
>> 
>>> If we put a hard line between the two, I think more clarity emerges.
>> 
>> Maybe, but consistency matters.  Clarity doesn't emerge in isolation.  
>> Deviating from prevailing usage tends to confuse.
>> 
>>>> GError.
>>>>
>>>> More modern languages do "return error value" much better than C can.  C
>>>> is what it is.
>>>>
>>>> We could certainly argue how to do better than we do now in QEMU's C
>>>> code.  However, the Error API is used all over the place, which makes
>>>> changing it expensive.  "Rethinking the whole Error API" (your words)
>>>> would have to generate benefits worth this expense.  Which seems
>>>> unlikely.
>>>>
>>>> [...]
>>>>
>>>
>>> This is all fine, the problem is how we remodel this in C.
>>>
>>> This is how I see the next steps to clarify my position:
>>>
>>> short term:
>>>
>>> - keep the existing API with the existing assumptions, use a separate argument to carry the pointer to the actual return value (where the function return value as provided by the language is used to return if an exception was generated or not, as we assume today).
>> 
>> We don't actually need separate values for "actual return value" and
>> "success vs. failure" here.  We can easily encode them in a single
>
> Yes, we do, it would avoid the confusion that we see as soon as people look at the module_load_one code the first time.
>
>> return value.  This is *common* in C, for better or worse.
>
> We make our own life difficult, by wasting the very precious space of the return value that should be used to provide the meaning of the function,
>
> and using it to provide a completely useless and redundant bool return value, that by your own definition,
> is just "errp != NULL".

*errp != NULL, except that doesn't work when the caller NULL to errp.

> The very precious and scarce return value of the C function is completely wasted.

I think you're tilting at windmills :)

error.h again:

 * - Whenever practical, also return a value that indicates success /
 *   failure.  This can make the error checking more concise, and can
 *   avoid useless error object creation and destruction.  Note that
 *   we still have many functions returning void.  We recommend
 *   • bool-valued functions return true on success / false on failure,
 *   • pointer-valued functions return non-null / null pointer, and
 *   • integer-valued functions return non-negative / negative.

This does *not* ask you to waste the return value on a bool indicating
success.  It asks you to use error values whenever practical, and
recommends common ones, namely:

     • When a function returns a non-negative integer on success, use
       negative integers to signify failure.

     • When a function returns a non-null pointer on success, use a null
       pointer to signify failure.

     • When a function has nothing to return, make it return true on
       success, and false on failure.

Such use of return values is idiomatic C.

When a function can return any value of its return type on success,
there are no error values left.  Unless we can tweak the return type to
accomodate error values, say widen it from unsigned char to int, we're
outside "when practical" territory.

[...]



  reply	other threads:[~2022-09-23  5:34 UTC|newest]

Thread overview: 50+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-08 18:30 [PATCH v4 0/3] improve error handling for module load Claudio Fontana
2022-09-08 18:30 ` [PATCH v4 1/3] module: removed unused function argument "mayfail" Claudio Fontana
2022-09-08 18:30 ` [PATCH v4 2/3] module: add Error arguments to module_load_one and module_load_qom_one Claudio Fontana
2022-09-15  8:43   ` Claudio Fontana
2022-09-16  8:13   ` Richard Henderson
2022-09-16  8:16     ` Claudio Fontana
2022-09-16  9:27   ` Markus Armbruster
2022-09-16 10:48     ` Claudio Fontana
2022-09-16 14:26       ` Markus Armbruster
2022-09-16 15:06         ` Claudio Fontana
2022-09-19  8:17           ` Markus Armbruster
2022-09-19  8:45             ` Claudio Fontana
2022-09-21 12:47               ` Markus Armbruster
2022-09-19 10:18     ` Philippe Mathieu-Daudé via
2022-09-21 12:16       ` Markus Armbruster
2022-09-21 16:03         ` Claudio Fontana
2022-09-22  6:07           ` Markus Armbruster
2022-09-22  8:28             ` Daniel P. Berrangé
2022-09-22  9:20               ` Claudio Fontana
2022-09-22  9:21                 ` Claudio Fontana
2022-09-22  9:27                   ` Claudio Fontana
2022-09-22  9:31                 ` Daniel P. Berrangé
2022-09-22  9:34                   ` Claudio Fontana
2022-09-22 10:37                     ` Daniel P. Berrangé
2022-09-22 12:30                       ` Claudio Fontana
2022-09-22 12:33                         ` Daniel P. Berrangé
2022-09-22 12:35                           ` Claudio Fontana
2022-09-22  9:38               ` Markus Armbruster
2022-09-22  9:43                 ` Claudio Fontana
2022-09-22 12:42                   ` Markus Armbruster
2022-09-22 12:45                     ` Claudio Fontana
2022-09-22 13:20                       ` Markus Armbruster
2022-09-22 13:33                         ` Claudio Fontana
2022-09-22 14:36                           ` Markus Armbruster
2022-09-22 15:22                             ` Claudio Fontana
2022-09-23  5:31                               ` Markus Armbruster [this message]
2022-09-23  9:40                                 ` Claudio Fontana
2022-09-22 13:34                         ` Philippe Mathieu-Daudé via
2022-09-22 13:42                           ` Claudio Fontana
2022-09-22 13:44                           ` Daniel P. Berrangé
2022-09-22 14:01                             ` Claudio Fontana
2022-09-22 14:54                     ` Kevin Wolf
2022-09-22 15:08                       ` Claudio Fontana
2022-09-22 15:27                       ` Markus Armbruster
2022-09-22 15:51                         ` Claudio Fontana
2022-09-22 17:05                         ` Kevin Wolf
2022-09-23  9:42                           ` Claudio Fontana
2022-09-23  9:44                           ` Claudio Fontana
2022-09-25 10:35           ` Richard Henderson
2022-09-08 18:30 ` [PATCH v4 3/3] accel: abort if we fail to load the accelerator plugin Claudio Fontana

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=87edw2hd15.fsf@pond.sub.org \
    --to=armbru@redhat.com \
    --cc=berrange@redhat.com \
    --cc=cfontana@suse.de \
    --cc=dinechin@redhat.com \
    --cc=f4bug@amsat.org \
    --cc=kraxel@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=marcandre.lureau@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=richard.henderson@linaro.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.