Re: AW: Example code at the manpage of strtok produces segmentation fault.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 3/11/23 02:47, Leon Breidt wrote:
> Hello Alex,
> 

Hello Leon,

> 
> unfortunately it seems I did not receive the Makefile you wanted me to use, trying to assemble it from the console output you provided me failed. Probably my lack of experience with make doesn't benefit me either...

Sorry, I didn't specify.  I meant the Makefile in the man-pages'
git repository.

> 
> I have cloned the man-pages repository from git and looked in the Makefile there, but there is no build-src target specified there as well.

That Makefile is rather short, and as you say has no 'build-src'
target.  However, you might have noticed it has many 'include's
at the end of the file, which provide the actual functionality
(but you don't need to care about them; just run `make build-src`
from the root of the repo).

The main Makefile only contains the 'all' and 'help' targets.
BTW, if you run `make help`, you'll see a full list of targets
you can run.

> Could you send me the text content of the makefile you want me to use, it seems attached files don't make it all the way?
> 
> 
> To address your other question, I have tried a simple copy-paste with ctrl-c, ctrl-v from the manual page in my terminal, as well as from the website at https://man7.org/linux/man-pages/man3/strtok_r.3.html
> 
> 
> I do indeed see compiler warnings, you will find them below:
> 
> 
> breidt@STUDENT-III:~/tmp$ gcc main.c

You should _always_ compile with -Wall and -Wextra:

   $ gcc -Wall -Wextra main.c

> main.c: In function ‘main’:
> main.c:17:28: warning: initialization of ‘int’ from ‘char *’ makes integer from pointer without a cast [-Wint-conversion]
>    17 |     for (int j = 1, str1 = argv[1]; ; j++, str1 = NULL) {
>       |                            ^~~~
> main.c:17:49: warning: assignment to ‘int’ from ‘void *’ makes integer from pointer without a cast [-Wint-conversion]
>    17 |     for (int j = 1, str1 = argv[1]; ; j++, str1 = NULL) {
>       |                                                 ^
> main.c:18:26: warning: passing argument 1 of ‘strtok_r’ makes pointer from integer without a cast [-Wint-conversion]
>    18 |         token = strtok_r(str1, argv[2], &saveptr1);
>       |                          ^~~~
>       |                          |
>       |                          int
> In file included from main.c:3:
> /usr/include/string.h:366:41: note: expected ‘char * restrict’ but argument is of type ‘int’
>   366 | extern char *strtok_r (char *__restrict __s, const char *__restrict __delim,
>       |                        ~~~~~~~~~~~~~~~~~^~~
> 
> 
> 
> I have also attached the exact c file I am compiling with "gcc main.c". Lets hope it gets through.

Now I see the problem.  It was a bug accidentally introduced in man-pages-5.09:


---
$ git log -1 88893a773cbd219f08cc3ae34fffe079d89a120d
commit 88893a773cbd219f08cc3ae34fffe079d89a120d
Author: Michael Kerrisk <mtk.manpages@xxxxxxxxx>
Date:   Sat Sep 5 17:18:10 2020 +0200

    sprof.1, eventfd.2, execve.2, futex.2, getdents.2, mprotect.2, open_by_handle_at.2, recvmmsg.2, sched_setaffinity.2, CPU_SET.3, backtrace.3, bsearch.3, dl_iterate_phdr.3, dlinfo.3, duplocale.3, encrypt.3, envz_add.3, fopencookie.3, getaddrinfo.3, getaddrinfo_a.3, getdate.3, getgrent_r.3, getgrouplist.3, getifaddrs.3, getprotoent_r.3, getservent_r.3, hsearch.3, mallinfo.3, malloc_info.3, mbstowcs.3, mtrace.3, pthread_create.3, pthread_getcpuclockid.3, pthread_setaffinity_np.3, qsort.3, rand.3, strcat.3, strtok.3, tsearch.3, wordexp.3, core.5, aio.7, inotify.7, sock_diag.7, unix.7, user_namespaces.7: Use C99 style to declare loop counter variables
    
    Rather than:
    
        sometype x;
    
        for (x = ....; ...)
    
    use
    
        for (sometype x = ...; ...)
    
    This brings the declaration and use closer together (thus aiding
    readability) and also clearly indicates the scope of the loop
    counter variable.
    
    Signed-off-by: Michael Kerrisk <mtk.manpages@xxxxxxxxx>
$ git describe --contains 88893a773cbd219f08cc3ae34fffe079d89a120d
man-pages-5.09~442
$ git show man-pages-5.09
tag man-pages-5.09
Tagger: Michael Kerrisk <mtk.manpages@xxxxxxxxx>
Date:   Sun Nov 1 21:08:55 2020 +0100

This is man-pages-5.09

---



It was fixed in man-pages-6.00:


---
$ git show f091d3e26e4cdef9ecc632d6f4bd94dc16fce43e
commit f091d3e26e4cdef9ecc632d6f4bd94dc16fce43e
Author: Stephen Kitt <steve@xxxxxxx>
Date:   Sat Jan 8 16:43:04 2022 +0100

    strtok.3: Fix j/str1 declaration
    
        for (int j = 1, str1 = argv[1]; ...
    
    declares two variables of type int, j and str1; the pre-existing
    char * str1 isn't used. This causes compiler warnings. Declaring j
    outside the loop fixes everything.
    
    Signed-off-by: Stephen Kitt <steve@xxxxxxx>
    Signed-off-by: Alejandro Colomar <alx.manpages@xxxxxxxxx>

diff --git a/man3/strtok.3 b/man3/strtok.3
index aec914094..06e9688b6 100644
--- a/man3/strtok.3
+++ b/man3/strtok.3
@@ -255,6 +255,7 @@ .SS Program source
 {
     char *str1, *str2, *token, *subtoken;
     char *saveptr1, *saveptr2;
+    int j;
 
     if (argc != 4) {
         fprintf(stderr, "Usage: %s string delim subdelim\en",
@@ -262,7 +263,7 @@ .SS Program source
         exit(EXIT_FAILURE);
     }
 
-    for (int j = 1, str1 = argv[1]; ; j++, str1 = NULL) {
+    for (j = 1, str1 = argv[1]; ; j++, str1 = NULL) {
         token = strtok_r(str1, argv[2], &saveptr1);
         if (token == NULL)
             break;
$ git describe --contains f091d3e26e4cdef9ecc632d6f4bd94dc16fce43e
man-pages-6.00~1122
$ git show man-pages-6.00
tag man-pages-6.00
Tagger: Alejandro Colomar <alx@xxxxxxxxxx>
Date:   Sun Oct 9 18:48:44 2022 +0200

man-pages-6.00 - manual pages for GNU/Linux

I've released man-pages-6.00.  The release tarball will soon be
available on <kernel.org>.

This release resulted from patches, bug reports, reviews, and comments
from around 145 contributors.  The release includes around 1245
commits, and changed all of the pages.

The most notable of the changes in man-pages-6.00 are the following:

- A new set of man dirs: man2type/, man3const/, man3head/, and man3type.
  These hold new pages and pages splitted from system_data_types(7),
  which had become too big in the recent releases.

- An improved build system, which allows running linter programs that
  check the correctness of both the man(7) source and the C programs in
  EXAMPLES.

- A new LIBRARY section (mostly in sections 2 and 3).  There have also
  been other important changes to the title and other sections, such as
  the removal of the COLOPHON.

- We have added several new pages documenting new kernel features, such
  as landlock(7) and memfd_secret(2).

Especial mention to наб, with 58 commits to this release.

Thank you all for contributing.  Especially to those in the groff@
mailing list who helped me a lot in this release, and to Michael (mtk).
-----BEGIN PGP SIGNATURE-----

iQIzBAABCgAdFiEE6jqH8KTroDDkXfJAnowa+77/2zIFAmNC+6wACgkQnowa+77/
2zJEnQ/9GErYhSvR/SG1NUencHSg5pb2q0O/rVnavPpnK1CTBGWlDlF6a0d5+7RZ
zlP1BM289WN6xIIb4PyeobpKfMO0WUgRliKJPKCBz8lsiCIB0ZxrcD6o+rDeKvXK
g+eP6ofYYEZ9sTYWZR1Id2LYbwir4JO4Pp3O2xZA1W0XJJh8zN/tMMQwTxQfpBCF
I2K6vGKipT2/ueTu0qZ7A6lswdPSTwbeuFJjGV6VneI4yy/ID3KHgrU++3l/wq1h
sseEPNT5E+y+bhwA7958v/yjl9Sbn6ebvVFoAJakylv95UWPWprqFBVks2az4Xz+
oXAmcRdKQ1aWQwD+6MrwLqjKsO3qvlgxJzREDFr8ySUmiPnyp7PJ160XtvqP2zu2
elyvPPsu4YvGIO9lojXxAT66kY4XXg6tKgdaJac2QXt/dl/C9Q+Ni9MDuyLbnbGb
RLlQLjCpjsrpIaa+xniJzrZqh9VjbxDvG2YliyhvLjdGLoV3YQjOgIoYWJctaIw3
hFD6XLIGWrKXQ0uw26zGhYbXYd8z7WY5r36h5izdF9SeXP0Bgo1hw9NR6UNFbqND
nFXlIjyYBLdLYF2JWrqMdU0/qTJwRzSQ9feqd3KXTQIG21legeLUrG885408pUyu
wsCCIXqKuIpysAjHyjwWVCFvGDr4CQ5qtE1fYTrFFmHUFruj89Y=
=uOhR
-----END PGP SIGNATURE-----

---


The bad news is that the <man7.org> online pages have not been updated
since man-pages-5.13 (Aug 2021).  You're probably using an old distro
that still has some old pages too.


> Kind regards,
> 
> Leon
> 

Kind regards,

Alex

-- 
<http://www.alejandro-colomar.es/>
GPG key fingerprint: A9348594CE31283A826FBDD8D57633D441E25BB5

Attachment: OpenPGP_signature
Description: OpenPGP digital signature


[Index of Archives]     [Kernel Documentation]     [Netdev]     [Linux Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux