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

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

 



Hi Leon,

On 3/10/23 06:09, Leon Breidt wrote:
> Dear Mr. Kerrisk, dear Mr. Colomar,
> 
> 
> I am a student at the university of applied sciences of Kaiserslautern, Germany. I am currently working on some code for a class using c, in which I needed to split some char arrays.
> 
> 
> I tried to understand the functionality of strtok using the manual and found a code example which seemed helpful. However, simply copy-pasting it, compiling it and executing the example usage produces a segmentation fault. Particulary annoying is this as the code was used in answers on stackoverflow, and continues to spread its false message from there...
> 
> 
> Steps to reproduce:
> 
> - execute: man strtok
> 
> - copy the code from the section "EXAMPLES"
> 
> - paste it into a new file "main.c"
> 
> - execute "gcc main.c"
> 
> - execute the example usage given in the manual: ./a.out 'a/bbb///cc;xxx:yyy:' ':;' '/'
> 
> 
> => Segmentation fault instead of the output described in the man page.

I can't reproduce that[1].  Could you please confirm if you can build and run
the program as shown below using the build system (Makefile)?

> 
> 
> I would be more than happy to create a better usage example, but I am uncertain how to submit a patch via E-Mail. I've never done this before. Will it be sufficient to send you the code of a new, hopefully better/fixed version via E-Mail?

Sure, if you can't send a patch, something like that should be good.

Nevertheless, I encourage you to learn to send patches through email.
It's not so difficult.  I guess you know how to use git(1).  If you do,
the simplest thing you can do is to paste the output of `git diff` (or
a similar command) into an email.  Once you can do that, you can learn
git-format-patch(1) and git-send-email(1), which would allow me to
apply the patch directly from the email, without having to copy+paste,
but git-diff(1) should be enough for a start.

> 
> 
> Kind regards,
> 
> Leon Breidt

Kind regards,

Alex



[1]:

alx@debian:~/src/linux/man-pages/man-pages/main$ make build-src -ij >/dev/null 2>&1
alx@debian:~/src/linux/man-pages/man-pages/main$ make build-src -tj
alx@debian:~/src/linux/man-pages/man-pages/main$ touch man3/strtok.3
alx@debian:~/src/linux/man-pages/man-pages/main$ make build-src V=1
MKDIR	tmp/src/man3/strtok.3.d
mkdir -p tmp/src/man3/strtok.3.d
touch tmp/src/man3/strtok.3.d
SED	tmp/src/man3/strtok.3.d/strtok.c
<man3/strtok.3 \
sed -n \
	-e '/^\.TH/,/^\.SH/{/^\.SH/!p}' \
	-e '/^\.SH EXAMPLES/p' \
	-e "/^\... SRC BEGIN (strtok.c)$/,/^\... SRC END$/p" \
| man -P cat -l - \
| sed '/^[^ ]/d' \
| sed 's/^       //' \
>tmp/src/man3/strtok.3.d/strtok.c
CC	tmp/src/man3/strtok.3.d/strtok.o
cc -c -isystem /usr/include/bsd -DLIBBSD_OVERLAY   -std=gnu17 -Wall -Wextra -Wstrict-prototypes -Wdeclaration-after-statement -Werror -Wno-error=unused-parameter -Wno-error=sign-compare -Wno-error=format -Wno-error=uninitialized  -o tmp/src/man3/strtok.3.d/strtok.o tmp/src/man3/strtok.3.d/strtok.c
LD	tmp/src/man3/strtok.3.d/strtok
cc -std=gnu17 -Wall -Wextra -Wstrict-prototypes -Wdeclaration-after-statement -Werror -Wno-error=unused-parameter -Wno-error=sign-compare -Wno-error=format -Wno-error=uninitialized  -Wl,--as-needed -Wl,--no-allow-shlib-undefined -Wl,--no-copy-dt-needed-entries -Wl,--no-undefined  -o tmp/src/man3/strtok.3.d/strtok tmp/src/man3/strtok.3.d/strtok.o -lc -lbsd  
alx@debian:~/src/linux/man-pages/man-pages/main$ tmp/src/man3/strtok.3.d/strtok
Usage: tmp/src/man3/strtok.3.d/strtok string delim subdelim
alx@debian:~/src/linux/man-pages/man-pages/main$ tmp/src/man3/strtok.3.d/strtok a.s,q.w,z.x . ,
1: a
	 --> a
2: s,q
	 --> s
	 --> q
3: w,z
	 --> w
	 --> z
4: x
	 --> x
alx@debian:~/src/linux/man-pages/man-pages/main$ tmp/src/man3/strtok.3.d/strtok 'a/bbb///cc;xxx:yyy:' ':;' '/'
1: a/bbb///cc
	 --> a
	 --> bbb
	 --> cc
2: xxx
	 --> xxx
3: yyy
	 --> yyy


If it crashes for you running these commands, there might be something wrong
either in the page or in your system.

If it works, then the problem is probably in the way you copied it.  Do you
see any warnings from the compiler?


-- 
<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