[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [PATCH 28/52] migration/rdma: Check negative error values the same w
From: |
Markus Armbruster |
Subject: |
Re: [PATCH 28/52] migration/rdma: Check negative error values the same way everywhere |
Date: |
Mon, 25 Sep 2023 09:29:55 +0200 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux) |
"Zhijian Li (Fujitsu)" <lizhijian@fujitsu.com> writes:
> On 18/09/2023 22:41, Markus Armbruster wrote:
>> When a function returns 0 on success, negative value on error,
>> checking for non-zero suffices, but checking for negative is clearer.
>> So do that.
>>
>
> This patch is no my favor convention.
Certainly a matter of taste, which means maintainers get to decide, not
me.
Failure checks can be confusing in C. Is
if (foo(...))
checking for success or for failure? Impossible to tell. If foo()
returns a pointer, it almost certainly checks for success. If it
returns bool, likewise. But if it returns an integer, it probably
checks for failure.
Getting a condition backwards is a common coding mistake. Consider
patch review of
if (condition) {
obviously the error case
}
Patch review is more likely to catch a backward condition when the
condition's sense is locally obvious.
Conventions can help. Here's the one I like:
* Check for a function's failure the same way everywhere.
* When a function returns something "truthy" on success, and something
"falsy" on failure, use
if (!fun(...))
Special cases:
- bool true on success, false on failure
- non-null pointer on success, null pointer on failure
* When a function returns non-negative value on success, negative value
on failure, use
if (fun(...) < 0)
* Avoid non-negative integer error values.
* Avoid if (fun(...)), because it's locally ambiguous.
> @Peter, Juan
>
> I'd like to hear your opinions.
Yes, please.
- [PATCH 09/52] migration/rdma: Put @errp parameter last, (continued)
- [PATCH 09/52] migration/rdma: Put @errp parameter last, Markus Armbruster, 2023/09/18
- [PATCH 03/52] migration/rdma: Clean up rdma_delete_block()'s return type, Markus Armbruster, 2023/09/18
- [PATCH 40/52] migration/rdma: Convert qemu_rdma_write() to Error, Markus Armbruster, 2023/09/18
- [PATCH 28/52] migration/rdma: Check negative error values the same way everywhere, Markus Armbruster, 2023/09/18
- [PATCH 37/52] migration/rdma: Convert qemu_rdma_reg_whole_ram_blocks() to Error, Markus Armbruster, 2023/09/18
- [PATCH 07/52] migration/rdma: Give qio_channel_rdma_source_funcs internal linkage, Markus Armbruster, 2023/09/18
- [PATCH 47/52] migration/rdma: Don't report received completion events as error, Markus Armbruster, 2023/09/18
- [PATCH 19/52] migration/rdma: Fix qemu_get_cm_event_timeout() to always set error, Markus Armbruster, 2023/09/18