[pacman-dev] Add GOTO_ERR() macro to set error and then goto a label

Message ID 20200306200015.166240-1-anatol.pomozov@gmail.com
State Accepted, archived
Headers show
Series
  • [pacman-dev] Add GOTO_ERR() macro to set error and then goto a label
Related show

Commit Message

Anatol Pomozov March 6, 2020, 8 p.m. UTC
This is a macro similar to RET_ERR but useful in the case when we need
to record an error and then jump to some cleanup section.

Signed-off-by: Anatol Pomozov <anatol.pomozov@gmail.com>
---
 lib/libalpm/util.h | 5 +++++
 1 file changed, 5 insertions(+)

Comments

Allan McRae March 8, 2020, 4:37 a.m. UTC | #1
On 7/3/20 6:00 am, Anatol Pomozov wrote:
> This is a macro similar to RET_ERR but useful in the case when we need
> to record an error and then jump to some cleanup section.
> 
> Signed-off-by: Anatol Pomozov <anatol.pomozov@gmail.com>
> ---
>  lib/libalpm/util.h | 5 +++++
>  1 file changed, 5 insertions(+)
> 
> diff --git a/lib/libalpm/util.h b/lib/libalpm/util.h
> index 9827b2c1..ce80b32b 100644
> --- a/lib/libalpm/util.h
> +++ b/lib/libalpm/util.h
> @@ -71,6 +71,11 @@ void _alpm_alloc_fail(size_t size);
>  	(handle)->pm_errno = (err); \
>  	return (ret); } while(0)
>  
> +#define GOTO_ERR(handle, err, label) do { \
> +	_alpm_log(handle, ALPM_LOG_DEBUG, "got error %d at %s:%d : %s\n", err, __func__, __LINE__, alpm_strerror(err)); \
> +	(handle)->pm_errno = (err); \
> +	goto label; } while(0)
> +

This is OK.  And there are dozens of places it could be used.

Do we need __LINE__?  I note we don't use it in RET_ERR, and I am not a
fan of it...  Function and error should be enough, or we need more
specific error codes.

A
Anatol Pomozov March 8, 2020, 8:06 p.m. UTC | #2
Hi

On Sat, Mar 7, 2020 at 9:55 PM Allan McRae <allan@archlinux.org> wrote:
>
> On 7/3/20 6:00 am, Anatol Pomozov wrote:
> > This is a macro similar to RET_ERR but useful in the case when we need
> > to record an error and then jump to some cleanup section.
> >
> > Signed-off-by: Anatol Pomozov <anatol.pomozov@gmail.com>
> > ---
> >  lib/libalpm/util.h | 5 +++++
> >  1 file changed, 5 insertions(+)
> >
> > diff --git a/lib/libalpm/util.h b/lib/libalpm/util.h
> > index 9827b2c1..ce80b32b 100644
> > --- a/lib/libalpm/util.h
> > +++ b/lib/libalpm/util.h
> > @@ -71,6 +71,11 @@ void _alpm_alloc_fail(size_t size);
> >       (handle)->pm_errno = (err); \
> >       return (ret); } while(0)
> >
> > +#define GOTO_ERR(handle, err, label) do { \
> > +     _alpm_log(handle, ALPM_LOG_DEBUG, "got error %d at %s:%d : %s\n", err, __func__, __LINE__, alpm_strerror(err)); \
> > +     (handle)->pm_errno = (err); \
> > +     goto label; } while(0)
> > +
>
> This is OK.  And there are dozens of places it could be used.
>
> Do we need __LINE__?  I note we don't use it in RET_ERR, and I am not a
> fan of it...  Function and error should be enough, or we need more
> specific error codes.

There could be multiple RET_ERR/GOTO_ERR places in one function. How
to distinguish between those different error points?
Allan McRae March 9, 2020, 2:43 a.m. UTC | #3
On 9/3/20 6:06 am, Anatol Pomozov wrote:
> Hi
> 
> On Sat, Mar 7, 2020 at 9:55 PM Allan McRae <allan@archlinux.org> wrote:
>>
>> On 7/3/20 6:00 am, Anatol Pomozov wrote:
>>> This is a macro similar to RET_ERR but useful in the case when we need
>>> to record an error and then jump to some cleanup section.
>>>
>>> Signed-off-by: Anatol Pomozov <anatol.pomozov@gmail.com>
>>> ---
>>>  lib/libalpm/util.h | 5 +++++
>>>  1 file changed, 5 insertions(+)
>>>
>>> diff --git a/lib/libalpm/util.h b/lib/libalpm/util.h
>>> index 9827b2c1..ce80b32b 100644
>>> --- a/lib/libalpm/util.h
>>> +++ b/lib/libalpm/util.h
>>> @@ -71,6 +71,11 @@ void _alpm_alloc_fail(size_t size);
>>>       (handle)->pm_errno = (err); \
>>>       return (ret); } while(0)
>>>
>>> +#define GOTO_ERR(handle, err, label) do { \
>>> +     _alpm_log(handle, ALPM_LOG_DEBUG, "got error %d at %s:%d : %s\n", err, __func__, __LINE__, alpm_strerror(err)); \
>>> +     (handle)->pm_errno = (err); \
>>> +     goto label; } while(0)
>>> +
>>
>> This is OK.  And there are dozens of places it could be used.
>>
>> Do we need __LINE__?  I note we don't use it in RET_ERR, and I am not a
>> fan of it...  Function and error should be enough, or we need more
>> specific error codes.
> 
> There could be multiple RET_ERR/GOTO_ERR places in one function. How
> to distinguish between those different error points?

I was thinking we need more specific error codes if it is not clear, but
I guess we can see the same error in multiple places.  I'm OK keeping
__LINE__ if there are not objections.

A

Patch

diff --git a/lib/libalpm/util.h b/lib/libalpm/util.h
index 9827b2c1..ce80b32b 100644
--- a/lib/libalpm/util.h
+++ b/lib/libalpm/util.h
@@ -71,6 +71,11 @@  void _alpm_alloc_fail(size_t size);
 	(handle)->pm_errno = (err); \
 	return (ret); } while(0)
 
+#define GOTO_ERR(handle, err, label) do { \
+	_alpm_log(handle, ALPM_LOG_DEBUG, "got error %d at %s:%d : %s\n", err, __func__, __LINE__, alpm_strerror(err)); \
+	(handle)->pm_errno = (err); \
+	goto label; } while(0)
+
 #define RET_ERR_ASYNC_SAFE(handle, err, ret) do { \
 	(handle)->pm_errno = (err); \
 	return (ret); } while(0)