Path: ...!feeds.phibee-telecom.net!2.eu.feeder.erje.net!feeder.erje.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: avoiding strdup() Date: Mon, 11 Mar 2024 12:34:19 -0700 Organization: None to speak of Lines: 33 Message-ID: <875xxsa7gk.fsf@nosuchdomain.example.com> References: <87y1ayj6hs.fsf_-_@bsb.me.uk> <87r0gizzuo.fsf@nosuchdomain.example.com> <20240310101101.00001fd4@yahoo.com> <20240310100715.866@kylheku.com> <20240311104527.444@kylheku.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: dont-email.me; posting-host="803e1d91970c7135fb6b17cffb70db2d"; logging-data="3990758"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/gEWXayEIu94bX63j/vF99" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) Cancel-Lock: sha1:HcmiD9PG0aL9VV/fjiHWlFsmFyI= sha1:lq1Mxjc/OpjQychjc0kuA4/1Ugw= Bytes: 3054 Richard Kettlewell writes: > Kaz Kylheku <433-929-6894@kylheku.com> writes: >> Scott Lurndal wrote: >>> Malcolm McLean writes: >>>>On 10/03/2024 18:47, Scott Lurndal wrote: >>>>> Kaz Kylheku <433-929-6894@kylheku.com> writes: >>>>>> Not take up space in every application for a common library routine. >>>>> >>>>> It's a form of lazy programming. I've seen a lot of open source >>>>> code that uses strdup without checking for failure and frequently >>>>> "forgetting" to free the result. >>>> >>>>And it is probably more likely that machine with many gigabytes of RAM >>> >>> Actually, your assumptions that: >>> 1) strdup is the only allocation function used by an application >>> 2) all strings are "short" >> >> Even if a string is long enough to need its own mmap request, >> that will still return valid memory that later fails to commit. > > Since strdup necessarily writes to almost all the memory it allocates, > you’d expect the failure to be immediate. Yes, but that write happens *after* the malloc() call, so it won't necessarily be reflected in the value returned by strdup(). It could crash the current program, or in the worst case it could cause the OoM killer to kill some other process. -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com Working, but not speaking, for Medtronic void Void(void) { Void(); } /* The recursive call of the void */