From 44a16bcb618469edf50931b6306525b5bee95de9 Mon Sep 17 00:00:00 2001 From: Michael Kerrisk Date: Tue, 10 Aug 2021 11:11:40 +0200 Subject: [PATCH] pthread_setname_np.3: EXAMPLES: remove a bug by simplify the code From an email conversation with Alexis: Hello Alexis, On 8/6/21 7:06 PM, Alexis Wilke wrote: > Hi guys, > > The pthread_setname_np(3) manual page has an example where the second > argument is used to get a size of the thread name. > > https://man7.org/linux/man-pages/man3/pthread_setname_np.3.html#EXAMPLES > > The current code: > > rc = pthread_getname_np(thread, thread_name, > (argc > 2) ? atoi(argv[1]) : NAMELEN); > > The suggested code: > > rc = pthread_getname_np(thread, thread_name, > (argc > 2) ? atoi(argv[2]) : NAMELEN); I agree that there's a problem, but I think we could go even simpler: rc = pthread_getname_np(thread, thread_name, NAMELEN); > I'm thinking that maybe the author meant to compute the length like so: > > rc = pthread_getname_np(thread, thread_name, > (argc > 2) ? strlen(argv[1]) + 1 : > NAMELEN); > > But I think that the atoi() points to using argv[2] as a number > representing the length. > > (Of course, it should be tested against NAMELEN as a maximum, but I > understand that examples do not always show how to verify each possible > error). I imagine that the author's intention was to allow the user to do experiments where argv[2] specified a number less than NAMELEN, in order to see the resulting ERANGE error. But, that experiment is of limited value, and complicates the code unnecessarily, IMO, so that's why I made the change above. -- Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/ Reported-by: Alexis Wilke Signed-off-by: Michael Kerrisk --- man3/pthread_setname_np.3 | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/man3/pthread_setname_np.3 b/man3/pthread_setname_np.3 index 77c18a3cf..0dfe3df39 100644 --- a/man3/pthread_setname_np.3 +++ b/man3/pthread_setname_np.3 @@ -201,8 +201,7 @@ main(int argc, char *argv[]) sleep(2); - rc = pthread_getname_np(thread, thread_name, - (argc > 2) ? atoi(argv[1]) : NAMELEN); + rc = pthread_getname_np(thread, thread_name, NAMELEN); if (rc != 0) errExitEN(rc, "pthread_getname_np"); printf("The thread name after setting it is %s.\en", thread_name);