All of lore.kernel.org
 help / color / mirror / Atom feed
* [patch] getgrouplist.3: tfix in example
@ 2024-03-06 16:13 Fedor Lapshin
  2024-03-16  0:27 ` Alejandro Colomar
  0 siblings, 1 reply; 5+ messages in thread
From: Fedor Lapshin @ 2024-03-06 16:13 UTC (permalink / raw)
  To: alx; +Cc: linux-man

In example of getgrouplist, if getpwnam returns NULL, the program
exits. The exit code EXIT_SUCCESS looks like a mistake, since the
program also calls perror right before.

This patch changes the exit code to EXIT_FAILURE.

Best regards, Fedor.

---
 man3/getgrouplist.3 | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/man3/getgrouplist.3 b/man3/getgrouplist.3
index 41389b6c3..239913ce6 100644
--- a/man3/getgrouplist.3
+++ b/man3/getgrouplist.3
@@ -165,7 +165,7 @@ main(int argc, char *argv[])
     pw = getpwnam(argv[1]);
     if (pw == NULL) {
         perror("getpwnam");
-        exit(EXIT_SUCCESS);
+        exit(EXIT_FAILURE);
     }
 \&
     /* Retrieve group list. */
-- 
2.34.1

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [patch] getgrouplist.3: tfix in example
  2024-03-06 16:13 [patch] getgrouplist.3: tfix in example Fedor Lapshin
@ 2024-03-16  0:27 ` Alejandro Colomar
  2024-03-27 19:20   ` [PATCH] getgrouplist.3: EXAMPLES: fix error handling for getpwnam(3) Fedor Lapshin
  0 siblings, 1 reply; 5+ messages in thread
From: Alejandro Colomar @ 2024-03-16  0:27 UTC (permalink / raw)
  To: Fedor Lapshin; +Cc: linux-man

[-- Attachment #1: Type: text/plain, Size: 2357 bytes --]

Hi Fedor,

On Wed, Mar 06, 2024 at 07:13:31PM +0300, Fedor Lapshin wrote:
> In example of getgrouplist, if getpwnam returns NULL, the program
> exits. The exit code EXIT_SUCCESS looks like a mistake, since the
> program also calls perror right before.
> 
> This patch changes the exit code to EXIT_FAILURE.

While your patch seems correct, it's not the only problem I see with
this call to getpwnam(3): it should clear errno before the call, since
a return of NULL doesn't necessarily mean an error.  See getpwnam(3):

$ MANWIDTH=72 man getpwnam | sed -n '/^RETURN/,/^$/p'
RETURN VALUE
     The getpwnam() and getpwuid() functions return  a  pointer  to  a
     passwd  structure,  or NULL if the matching entry is not found or
     an error occurs.  If an error occurs, errno is  set  to  indicate
     the error.  If one wants to check errno after the call, it should
     be set to zero before the call.

So I would also add 'errno = 0;' before the call, and then name the
commit something like

	getgrouplist.3: EXAMPLES: Fix error handling after getpwnam(3)

> 
> Best regards, Fedor.

Would you mind signing the patch?  See <./CONTRIBUTING.d/patches>:

       -  Sign your patch with "Signed-off-by:".  Read about the
          "Developer's Certificate of Origin" at
          <https://www.kernel.org/doc/Documentation/process/submitting-patches.rst>.
          When appropriate, other tags documented in that file, such as
          "Reported-by:", "Reviewed-by:", "Acked-by:", and
          "Suggested-by:" can be added to the patch.  The man-pages
          project also uses a "Cowritten-by:" tag with the obvious
          meaning.  Example:

              Signed-off-by: Alejandro Colomar <alx@kernel.org>


Have a lovely night!
Alex

> ---
>  man3/getgrouplist.3 | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/man3/getgrouplist.3 b/man3/getgrouplist.3
> index 41389b6c3..239913ce6 100644
> --- a/man3/getgrouplist.3
> +++ b/man3/getgrouplist.3
> @@ -165,7 +165,7 @@ main(int argc, char *argv[])
>      pw = getpwnam(argv[1]);
>      if (pw == NULL) {
>          perror("getpwnam");
> -        exit(EXIT_SUCCESS);
> +        exit(EXIT_FAILURE);
>      }
>  \&
>      /* Retrieve group list. */
> -- 
> 2.34.1

-- 
<https://www.alejandro-colomar.es/>

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH] getgrouplist.3: EXAMPLES: fix error handling for getpwnam(3)
  2024-03-16  0:27 ` Alejandro Colomar
@ 2024-03-27 19:20   ` Fedor Lapshin
  2024-03-27 19:23     ` Fedor Lapshin
  0 siblings, 1 reply; 5+ messages in thread
From: Fedor Lapshin @ 2024-03-27 19:20 UTC (permalink / raw)
  To: Alejandro Colomar; +Cc: linux-man

Signed-off-by: Fedor Lapshin <fe.lap.prog@gmail.com>
---
 man3/getgrouplist.3 | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/man3/getgrouplist.3 b/man3/getgrouplist.3
index 41389b6c3..cf23dfa78 100644
--- a/man3/getgrouplist.3
+++ b/man3/getgrouplist.3
@@ -162,10 +162,11 @@ main(int argc, char *argv[])
 \&
     /* Fetch passwd structure (contains first group ID for user). */
 \&
+    errno = 0;
     pw = getpwnam(argv[1]);
     if (pw == NULL) {
         perror("getpwnam");
-        exit(EXIT_SUCCESS);
+        exit(EXIT_FAILURE);
     }
 \&
     /* Retrieve group list. */
--
2.34.1

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] getgrouplist.3: EXAMPLES: fix error handling for getpwnam(3)
  2024-03-27 19:20   ` [PATCH] getgrouplist.3: EXAMPLES: fix error handling for getpwnam(3) Fedor Lapshin
@ 2024-03-27 19:23     ` Fedor Lapshin
  2024-03-27 20:14       ` Alejandro Colomar
  0 siblings, 1 reply; 5+ messages in thread
From: Fedor Lapshin @ 2024-03-27 19:23 UTC (permalink / raw)
  To: Alejandro Colomar; +Cc: linux-man

So sorry for the email to your personal email without the cc!

ср, 27 мар. 2024 г. в 22:20, Fedor Lapshin <fe.lap.prog@gmail.com>:
>
> Signed-off-by: Fedor Lapshin <fe.lap.prog@gmail.com>
> ---
>  man3/getgrouplist.3 | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/man3/getgrouplist.3 b/man3/getgrouplist.3
> index 41389b6c3..cf23dfa78 100644
> --- a/man3/getgrouplist.3
> +++ b/man3/getgrouplist.3
> @@ -162,10 +162,11 @@ main(int argc, char *argv[])
>  \&
>      /* Fetch passwd structure (contains first group ID for user). */
>  \&
> +    errno = 0;
>      pw = getpwnam(argv[1]);
>      if (pw == NULL) {
>          perror("getpwnam");
> -        exit(EXIT_SUCCESS);
> +        exit(EXIT_FAILURE);
>      }
>  \&
>      /* Retrieve group list. */
> --
> 2.34.1

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] getgrouplist.3: EXAMPLES: fix error handling for getpwnam(3)
  2024-03-27 19:23     ` Fedor Lapshin
@ 2024-03-27 20:14       ` Alejandro Colomar
  0 siblings, 0 replies; 5+ messages in thread
From: Alejandro Colomar @ 2024-03-27 20:14 UTC (permalink / raw)
  To: Fedor Lapshin; +Cc: linux-man

[-- Attachment #1: Type: text/plain, Size: 1717 bytes --]

Hi Fedor,

On Wed, Mar 27, 2024 at 10:23:21PM +0300, Fedor Lapshin wrote:
> So sorry for the email to your personal email without the cc!

No problem!  We humans make mistakes some times (and AIs make them all
the time).  ;)

You can configure git to always send to me and the list for this
repository.  I have this in the man-pages <.git/config>, which might be
useful to you:

[sendemail]
	to = linux-man@vger.kernel.org

> ср, 27 мар. 2024 г. в 22:20, Fedor Lapshin <fe.lap.prog@gmail.com>:
> >
> > Signed-off-by: Fedor Lapshin <fe.lap.prog@gmail.com>
> > ---
> >  man3/getgrouplist.3 | 3 ++-
> >  1 file changed, 2 insertions(+), 1 deletion(-)
> >
> > diff --git a/man3/getgrouplist.3 b/man3/getgrouplist.3
> > index 41389b6c3..cf23dfa78 100644
> > --- a/man3/getgrouplist.3
> > +++ b/man3/getgrouplist.3
> > @@ -162,10 +162,11 @@ main(int argc, char *argv[])
> >  \&
> >      /* Fetch passwd structure (contains first group ID for user). */
> >  \&
> > +    errno = 0;
> >      pw = getpwnam(argv[1]);
> >      if (pw == NULL) {

> >          perror("getpwnam");
> > -        exit(EXIT_SUCCESS);
> > +        exit(EXIT_FAILURE);

In case errno == NULL, there's no such user, but the call succeeded.
Calling perror(3) in that case will be weird.  I suggest:

	errno = 0;
	pw = getpwnam(argv[1]);
	if (pw == NULL) {
		if (errno == 0)
			fprintf(stderr, "'%s': No such user", argv[1]);
		else
			perror("getpwnam");

		exit(EXIT_FAILURE);
	}

Have a lovely night!
Alex

> >      }
> >  \&
> >      /* Retrieve group list. */
> > --
> > 2.34.1
> 

-- 
<https://www.alejandro-colomar.es/>
Looking for a remote C programming job at the moment.

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2024-03-27 20:14 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-06 16:13 [patch] getgrouplist.3: tfix in example Fedor Lapshin
2024-03-16  0:27 ` Alejandro Colomar
2024-03-27 19:20   ` [PATCH] getgrouplist.3: EXAMPLES: fix error handling for getpwnam(3) Fedor Lapshin
2024-03-27 19:23     ` Fedor Lapshin
2024-03-27 20:14       ` Alejandro Colomar

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.