Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail From: James Kuyper Newsgroups: comp.lang.c Subject: Re: Buffer contents well-defined after fgets() reaches EOF ? Date: Sun, 16 Feb 2025 01:04:11 -0500 Organization: A noiseless patient Spider Lines: 36 Message-ID: References: <20250210124911.00006b31@yahoo.com> <86ldu9zxkb.fsf@linuxsc.com> <20250214165108.00002984@yahoo.com> <20250214085627.815@kylheku.com> <20250215192911.0000793d@yahoo.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Injection-Date: Sun, 16 Feb 2025 07:04:30 +0100 (CET) Injection-Info: dont-email.me; posting-host="3aa6c45380cd33b15659a8290c61f2a4"; logging-data="481686"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX197IUa+w2lfQHv35sUvw757tOfw8kyqR4A=" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:hHBLhhEdEG21jABSaTyq81ehVLk= In-Reply-To: Content-Language: en-US Bytes: 2910 On 2/15/25 22:29, Janis Papanagnou wrote: > On 15.02.2025 18:29, Michael S wrote: >> On Fri, 14 Feb 2025 20:51:38 +0100 >> Janis Papanagnou wrote: >>> >>> Actually, in the same code, I'm also using the strtok() function >> >> strtok() is one of the relatively small set of more problemetic >> functions in C library that are not thread-safe. > > I know that it's not thread-safe. (You can't miss that information > if you look up the man page to inspect the function interface.) > >> If you only care about POSIX target, the I'd reccomend to avoid strtok >> and to use strtok_r(). If you cannot assume POSIX, but can assume C2011 or later, you might be able to use strtok_s() instead. You need to add #ifdef __STDC_LIB_EXT1__     #define __STDC_WANT_LIB_EXT1__ 1     // strtok_s() will be declared in #endif #include > But since I don't use threads - neither here nor did I ever needed > them generally in my "C" contexts - that's unnecessary. Isn't it? No. What makes strtok() problematic can come up without any use of threads. Consider for the moment a bug I had to investigate. A function that was looping through strtok() calls to parse a string called a utility function during each pass through the loop. The utility function also called strtok() in a loop to parse an entirely different string for a different purpose. Exercise for the student: figure out what the consequences were.