Thread (1 message) 1 message, 1 author, 2021-08-10

Re: Incorrect variable used in example of pthread_setname_np()

From: Michael Kerrisk (man-pages) <hidden>
Date: 2021-08-10 09:13:41

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.

Cheers,

Michael

-- 
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help