Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Kill Ugly Radio -- Frank Zappa


computers / comp.sys.raspberry-pi / Re: Weird code crash

SubjectAuthor
* Weird code crashThe Natural Philosopher
+* Re: Weird code crashAhem A Rivet's Shot
|`* Re: Weird code crashThe Natural Philosopher
| `* Re: Weird code crashAhem A Rivet's Shot
|  `- Re: Weird code crashThe Natural Philosopher
+* Re: Weird code crashTheo
|+* Re: Weird code crashRichard Kettlewell
||`* Re: Weird code crashThe Natural Philosopher
|| +* Re: Weird code crashTheo
|| |`- Re: Weird code crashThe Natural Philosopher
|| `* Re: Weird code crashRichard Kettlewell
||  `* Re: Weird code crashThe Natural Philosopher
||   +- Re: Weird code crashRichard Kettlewell
||   +* Re: Weird code crashTheo
||   |+* Re: Weird code crashRichard Kettlewell
||   ||`- Re: Weird code crashThe Natural Philosopher
||   |+- Re: Weird code crashPancho
||   |`- Re: Weird code crashThe Natural Philosopher
||   `* Re: Weird code crashPancho
||    `- Re: Weird code crashMartin Gregorie
|`- Re: Weird code crashcandycanearter07
+- Re: Weird code crashRichard Kettlewell
+- Re: Weird code crashTauno Voipio
+* Re: Weird code crashRalf Fassel
|+* Re: Weird code crashThe Natural Philosopher
||+* Re: Weird code crashDavid W. Hodgins
|||+* Re: Weird code crashThe Natural Philosopher
||||+* Re: Weird code crashDavid W. Hodgins
|||||`* Re: Weird code crashThe Natural Philosopher
||||| `* Re: Weird code crashDavid W. Hodgins
|||||  `- Re: Weird code crashThe Natural Philosopher
||||`* Re: Weird code crashcandycanearter07
|||| `* Re: Weird code crashThe Natural Philosopher
||||  `- Re: Weird code crashcandycanearter07
|||`- Re: Weird code crashRichard Kettlewell
||+* Re: Weird code crashRobert Riches
|||`- Re: Weird code crashThe Natural Philosopher
||`* Re: Weird code crashRalf Fassel
|| `* Re: Weird code crashThe Natural Philosopher
||  +* Re: Weird code crashRichard Kettlewell
||  |`- Re: Weird code crashThe Natural Philosopher
||  `* Re: Weird code crashRalf Fassel
||   `* Re: Weird code crashThe Natural Philosopher
||    +* Re: Weird code crashTheo
||    |+* Re: Weird code crashThe Natural Philosopher
||    ||`* Re: Weird code crashvallor
||    || `* Re: Weird code crashThe Natural Philosopher
||    ||  +* Re: Weird code crashRalf Fassel
||    ||  |`- Re: Weird code crashvallor
||    ||  `- Re: Weird code crashvallor
||    |+* Re: Weird code crashThe Natural Philosopher
||    ||`* Re: Weird code crashRich
||    || `- Re: Weird code crashCharlie Gibbs
||    |`* Re: Weird code crashcandycanearter07
||    | +- Re: Weird code crashRich
||    | +- Re: Weird code crashThe Natural Philosopher
||    | `* Re: Weird code crashRichard Kettlewell
||    |  `- Re: Weird code crashThe Natural Philosopher
||    +- Re: Weird code crashRalf Fassel
||    `* Re: Weird code crashRalf Fassel
||     `* Re: Weird code crashThe Natural Philosopher
||      +* Re: Weird code crashRich
||      |+* Re: Weird code crashThe Natural Philosopher
||      ||`- Re: Weird code crashRich
||      |`- Re: Weird code crashCharlie Gibbs
||      `- Re: Weird code crashRalf Fassel
|`- Re: Weird code crashcandycanearter07
`* Re: Weird code crashnev young
 `* Re: Weird code crashThe Natural Philosopher
  `* Re: Weird code crashTheo
   `- Re: Weird code crashThe Natural Philosopher

Pages:123
Re: Weird code crash

<yga4jjvik9o.fsf@akutech.de>

  copy mid

https://www.novabbs.com/computers/article-flat.php?id=7192&group=comp.sys.raspberry-pi#7192

  copy link   Newsgroups: comp.os.linux.misc comp.sys.raspberry-pi
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: ralf...@gmx.de (Ralf Fassel)
Newsgroups: comp.os.linux.misc,comp.sys.raspberry-pi
Subject: Re: Weird code crash
Date: Fri, 15 Sep 2023 16:27:47 +0200
Lines: 26
Message-ID: <yga4jjvik9o.fsf@akutech.de>
References: <udu5c4$2gutd$1@dont-email.me> <ygamsxoixhx.fsf@akutech.de>
<udv982$2lppb$4@dont-email.me> <ygail8biyxm.fsf@akutech.de>
<ue1b2v$36ji2$1@dont-email.me> <ygaedizitb9.fsf@akutech.de>
<ue1idg$37u15$2@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain
X-Trace: individual.net rSL3yO9r4w2qWU8hwlvLogX7DvFGgvxiDaYvbGVUUU09rLors=
Cancel-Lock: sha1:nnMyuHA1FQT6wH2ivPFHrNhjt58= sha1:gnZH3FDWt3sDC2BhKMk+AIDBgWw= sha256:OzJAdM1mSHw5xyrdZtSercPTtlJHuDtB8rBEKfvUy1g=
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
 by: Ralf Fassel - Fri, 15 Sep 2023 14:27 UTC

* The Natural Philosopher <tnp@invalid.invalid>
| > | thermometers[i].name=strdup(p); //
| > | make a copy of the name and attach it
| > | to our thermometer structure
| > Memory leak if thermometers[i].name already contains something.
| >
| further up the line...
>
| bzero(filbuf,sizeof(filbuf));
| /** first thing to do is clean any allocated memory used to
| store values. **/
| for(i=0;i<NUMBER_RELAYS;i++)
| free(thermometers[i].name);

Note that the assignment

thermometers[i].name=strdup(p);

is *inside* the while() loop without a free().

Probably you argue that there ever is only a single file to read in that
dir anyway... Personally, I've been bitten by such assumptions, so I'd
rather check once too often than hunting down "can't happen" bugs.

R'

Re: Weird code crash

<ue1pue$39jhn$1@dont-email.me>

  copy mid

https://www.novabbs.com/computers/article-flat.php?id=7193&group=comp.sys.raspberry-pi#7193

  copy link   Newsgroups: comp.os.linux.misc comp.sys.raspberry-pi
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: tnp...@invalid.invalid (The Natural Philosopher)
Newsgroups: comp.os.linux.misc,comp.sys.raspberry-pi
Subject: Re: Weird code crash
Date: Fri, 15 Sep 2023 15:32:45 +0100
Organization: A little, after lunch
Lines: 45
Message-ID: <ue1pue$39jhn$1@dont-email.me>
References: <udu5c4$2gutd$1@dont-email.me> <ygamsxoixhx.fsf@akutech.de>
<udv982$2lppb$4@dont-email.me> <ygail8biyxm.fsf@akutech.de>
<ue1b2v$36ji2$1@dont-email.me> <ygaedizitb9.fsf@akutech.de>
<ue1idg$37u15$2@dont-email.me> <TGs*5Arqz@news.chiark.greenend.org.uk>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 15 Sep 2023 14:32:46 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="45cc9fa6fbd082e7ff5e80bf84879cb5";
logging-data="3460663"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18BJSJUDlKodvcixYY14zA6n1Tti0rE9QI="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:SQXMY41SmGFe6jTrGaokJVQvHc4=
Content-Language: en-GB
In-Reply-To: <TGs*5Arqz@news.chiark.greenend.org.uk>
 by: The Natural Philosop - Fri, 15 Sep 2023 14:32 UTC

On 15/09/2023 14:23, Theo wrote:
> You could get a SIGABRT if you were trying to free something that was
> already freed. Are you sure those are interlocked such that for each i you
> call strdup() exactly once, and subsequently free() exactly once? If there
> was some code path that was breaking out of the loop or similar you might
> get such behaviour.

Well, I am not sure if that was it or not, but I deleted manually a
thermometer file and the thing crashed instantly. That is consistent
with the name having been set once, and then repeatedly free()ed. I then
installed the code with the free()ed pointers set to NULL, and it
*didn't* crash instantly.

I had assumed that freeing a pointer that already had been freed would
either result in a NO-OP because the pointer no longer existed in the
heap memory allocation tables, or it would instantly crash , but it
seems that the action is 'undefined'.

Not sure that's done the trick, because I don't quite see how a file
could ever cease to exist.

To not exist in the first place is one thing, but once written, nothing
should delete them.

Unless fopen("w") does that for a fraction of a microsecond

Or fopen("w") creates an *empty* file, in which case it is *just*
possible that an empty file is read, no strdup was done and the pointer
was double freed...next time around.

Academic now anyway. Pointers all set to null after freeing. Defined
behaviour. frees on NULL ignored.

I'll let it run and run and see.

--
The biggest threat to humanity comes from socialism, which has utterly
diverted our attention away from what really matters to our existential
survival, to indulging in navel gazing and faux moral investigations
into what the world ought to be, whilst we fail utterly to deal with
what it actually is.

Re: Weird code crash

<ue1qc0$39hqa$3@dont-email.me>

  copy mid

https://www.novabbs.com/computers/article-flat.php?id=7194&group=comp.sys.raspberry-pi#7194

  copy link   Newsgroups: comp.os.linux.misc comp.sys.raspberry-pi
Path: i2pn2.org!i2pn.org!news.chmurka.net!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: no...@thanks.net (candycanearter07)
Newsgroups: comp.os.linux.misc,comp.sys.raspberry-pi
Subject: Re: Weird code crash
Date: Fri, 15 Sep 2023 09:40:00 -0500
Organization: A noiseless patient Spider
Lines: 32
Message-ID: <ue1qc0$39hqa$3@dont-email.me>
References: <udu5c4$2gutd$1@dont-email.me> <ygamsxoixhx.fsf@akutech.de>
<udv982$2lppb$4@dont-email.me> <ygail8biyxm.fsf@akutech.de>
<ue1b2v$36ji2$1@dont-email.me> <ygaedizitb9.fsf@akutech.de>
<ue1idg$37u15$2@dont-email.me> <TGs*5Arqz@news.chiark.greenend.org.uk>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 15 Sep 2023 14:40:01 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="d2ebc6e72b4b902c9641527ad1752126";
logging-data="3458890"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18S2Xr3jOGXBo9UnZ4Ftgz1D18Lh0Ol9XFligkDn2XoXQ=="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:Enm32ycztS/s0MXTSHt6vS15Jxo=
Content-Language: en-US
In-Reply-To: <TGs*5Arqz@news.chiark.greenend.org.uk>
 by: candycanearter07 - Fri, 15 Sep 2023 14:40 UTC

On 9/15/23 08:23, Theo wrote:
> In comp.sys.raspberry-pi The Natural Philosopher <tnp@invalid.invalid> wrote:
>> On 15/09/2023 12:12, Ralf Fassel wrote:
>>> | {
>>> | *q++=0;
>>> | thermometers[i].name=strdup(p); //
>>> | make a copy of the name and attach it
>>> | to our thermometer structure
>>>
>>> Memory leak if thermometers[i].name already contains something.
>>>
>> further up the line...
>>
>> bzero(filbuf,sizeof(filbuf));
>> /** first thing to do is clean any allocated memory used to store
>> values. **/
>> for(i=0;i<NUMBER_RELAYS;i++)
>> free(thermometers[i].name);
>
> You could get a SIGABRT if you were trying to free something that was
> already freed. Are you sure those are interlocked such that for each i you
> call strdup() exactly once, and subsequently free() exactly once? If there
> was some code path that was breaking out of the loop or similar you might
> get such behaviour.
>
> Theo

I thought double free was a SIGSEGV?
--
--
user <candycane> is generated from /dev/urandom

Re: Weird code crash

<ue1r8m$39qpu$1@dont-email.me>

  copy mid

https://www.novabbs.com/computers/article-flat.php?id=7195&group=comp.sys.raspberry-pi#7195

  copy link   Newsgroups: comp.os.linux.misc comp.sys.raspberry-pi
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: tnp...@invalid.invalid (The Natural Philosopher)
Newsgroups: comp.os.linux.misc,comp.sys.raspberry-pi
Subject: Re: Weird code crash
Date: Fri, 15 Sep 2023 15:55:17 +0100
Organization: A little, after lunch
Lines: 68
Message-ID: <ue1r8m$39qpu$1@dont-email.me>
References: <udu5c4$2gutd$1@dont-email.me> <ygamsxoixhx.fsf@akutech.de>
<udv982$2lppb$4@dont-email.me> <ygail8biyxm.fsf@akutech.de>
<ue1b2v$36ji2$1@dont-email.me> <ygaedizitb9.fsf@akutech.de>
<ue1idg$37u15$2@dont-email.me> <yga4jjvik9o.fsf@akutech.de>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 15 Sep 2023 14:55:18 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="45cc9fa6fbd082e7ff5e80bf84879cb5";
logging-data="3468094"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+z7bBAcSKi2w8O4OP+3tzx/VISn/6wbcQ="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:2kEviVlSjVazgb+rfXPVskppC+c=
In-Reply-To: <yga4jjvik9o.fsf@akutech.de>
Content-Language: en-GB
 by: The Natural Philosop - Fri, 15 Sep 2023 14:55 UTC

On 15/09/2023 15:27, Ralf Fassel wrote:
> * The Natural Philosopher <tnp@invalid.invalid>
> | > | thermometers[i].name=strdup(p); //
> | > | make a copy of the name and attach it
> | > | to our thermometer structure
> | > Memory leak if thermometers[i].name already contains something.
> | >
> | further up the line...
>>
> | bzero(filbuf,sizeof(filbuf));
> | /** first thing to do is clean any allocated memory used to
> | store values. **/
> | for(i=0;i<NUMBER_RELAYS;i++)
> | free(thermometers[i].name);
>
> Note that the assignment
>
> thermometers[i].name=strdup(p);
>
> is *inside* the while() loop without a free().
>
> Probably you argue that there ever is only a single file to read in that
> dir anyway... Personally, I've been bitten by such assumptions, so I'd
> rather check once too often than hunting down "can't happen" bugs.
>
> R'
>
No. you have misunderstood how the code works.

There are up to 4 (NUMBER_RELAYS) thermometer files in that dir, and all
of them are read in the loop. What there shouldn't be is more than one
file with a ZONE number the same. So no pointer gets more than one STRDUP

If there were, it might be possible to strdup the same pointer twice.
And the daemon would get a memory leak and crash.

(It would be trivial to simply add a conditional that only strdups to a
pointer if it is NULL).

That is a possibility that could be caused by mis-configuration of the
thermometers themselves.

However they are not at this time misconfigured, so it shouldn't be the
crash problem, although it is an issue I will consider because fat
fingers *could* cause it.

I do think that what has happened is that a valid file name has been
found with empty data, or no file at all, and then no strdup is done -
but the free is, next time around.

That should never happen of course, as the fopen/fwrite sequence should
certainly not delete the filename, but it is entirely possible that a
the fopen *truncates* its data. At which point we cant strdup anything,
so the next free gets a woopsie

Setting the pointers to NULL after free() is nice defensive coding

As is allocating memory only if the pointers are null.

So both are in there now.

--
“Progress is precisely that which rules and regulations did not foresee,”

– Ludwig von Mises

Re: Weird code crash

<ue1ri8$39po5$1@dont-email.me>

  copy mid

https://www.novabbs.com/computers/article-flat.php?id=7196&group=comp.sys.raspberry-pi#7196

  copy link   Newsgroups: comp.os.linux.misc comp.sys.raspberry-pi
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ric...@example.invalid (Rich)
Newsgroups: comp.os.linux.misc,comp.sys.raspberry-pi
Subject: Re: Weird code crash
Date: Fri, 15 Sep 2023 15:00:24 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 35
Message-ID: <ue1ri8$39po5$1@dont-email.me>
References: <udu5c4$2gutd$1@dont-email.me> <ygamsxoixhx.fsf@akutech.de> <udv982$2lppb$4@dont-email.me> <ygail8biyxm.fsf@akutech.de> <ue1b2v$36ji2$1@dont-email.me> <ygaedizitb9.fsf@akutech.de> <ue1idg$37u15$2@dont-email.me> <TGs*5Arqz@news.chiark.greenend.org.uk> <ue1pue$39jhn$1@dont-email.me>
Injection-Date: Fri, 15 Sep 2023 15:00:24 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="9ed59c27ef1e98f694f4178ed42cabf8";
logging-data="3467013"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18QQSMtE6TzSy15hNBTDeOf"
User-Agent: tin/2.6.1-20211226 ("Convalmore") (Linux/5.15.117 (x86_64))
Cancel-Lock: sha1:UeFrkE+9evaRYK+rfmfgiToznmg=
 by: Rich - Fri, 15 Sep 2023 15:00 UTC

In comp.os.linux.misc The Natural Philosopher <tnp@invalid.invalid> wrote:
> I had assumed that freeing a pointer that already had been freed would
> either result in a NO-OP because the pointer no longer existed in the
> heap memory allocation tables, or it would instantly crash , but it
> seems that the action is 'undefined'.

Yes, C explicitly labels "double free" as "undefined":

<http://port70.net/~nsz/c/c99/n1256.html#J.2>

Look under J.2 Undefined behavior (easiest is to search for "free"):

J.2 Undefined behavior

1 The behavior is undefined in the following circumstances:

...

The pointer argument to the free or realloc function does not match
a pointer earlier returned by calloc, malloc, or realloc, or the
space has been deallocated by a call to free or realloc (7.20.3.2,
7.20.3.4).

And th 7.20.3.2 link in the page jumps to this:

The free function causes the space pointed to by ptr to be
deallocated, that is, made available for further allocation. If
ptr is a null pointer, no action occurs. Otherwise, if the
argument does not match a pointer earlier returned by the calloc,
malloc, or realloc function, or if the space has been deallocated
by a call to free or realloc, the behavior is undefined.

So if by chance you are double-freeing sometimes, then you are tickling
the undefined behaviour devil, and all bets are off as to what might
eventually occur.

Re: Weird code crash

<ue1rm2$39po5$2@dont-email.me>

  copy mid

https://www.novabbs.com/computers/article-flat.php?id=7197&group=comp.sys.raspberry-pi#7197

  copy link   Newsgroups: comp.os.linux.misc comp.sys.raspberry-pi
Path: i2pn2.org!i2pn.org!news.hispagatos.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ric...@example.invalid (Rich)
Newsgroups: comp.os.linux.misc,comp.sys.raspberry-pi
Subject: Re: Weird code crash
Date: Fri, 15 Sep 2023 15:02:26 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 6
Message-ID: <ue1rm2$39po5$2@dont-email.me>
References: <udu5c4$2gutd$1@dont-email.me> <ygamsxoixhx.fsf@akutech.de> <udv982$2lppb$4@dont-email.me> <ygail8biyxm.fsf@akutech.de> <ue1b2v$36ji2$1@dont-email.me> <ygaedizitb9.fsf@akutech.de> <ue1idg$37u15$2@dont-email.me> <TGs*5Arqz@news.chiark.greenend.org.uk> <ue1qc0$39hqa$3@dont-email.me>
Injection-Date: Fri, 15 Sep 2023 15:02:26 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="9ed59c27ef1e98f694f4178ed42cabf8";
logging-data="3467013"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/8irED1i49hYnahjLlSseD"
User-Agent: tin/2.6.1-20211226 ("Convalmore") (Linux/5.15.117 (x86_64))
Cancel-Lock: sha1:396TlBH7Q8MAVHwMoC5sMhMYa9s=
 by: Rich - Fri, 15 Sep 2023 15:02 UTC

In comp.os.linux.misc candycanearter07 <no@thanks.net> wrote:
>
> I thought double free was a SIGSEGV?

Check my other reply to TNP for the details, but it is "undefined" in
C.

Re: Weird code crash

<ue1rt8$39qpu$2@dont-email.me>

  copy mid

https://www.novabbs.com/computers/article-flat.php?id=7198&group=comp.sys.raspberry-pi#7198

  copy link   Newsgroups: comp.os.linux.misc comp.sys.raspberry-pi
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: tnp...@invalid.invalid (The Natural Philosopher)
Newsgroups: comp.os.linux.misc,comp.sys.raspberry-pi
Subject: Re: Weird code crash
Date: Fri, 15 Sep 2023 16:06:16 +0100
Organization: A little, after lunch
Lines: 52
Message-ID: <ue1rt8$39qpu$2@dont-email.me>
References: <udu5c4$2gutd$1@dont-email.me> <ygamsxoixhx.fsf@akutech.de>
<udv982$2lppb$4@dont-email.me> <ygail8biyxm.fsf@akutech.de>
<ue1b2v$36ji2$1@dont-email.me> <ygaedizitb9.fsf@akutech.de>
<ue1idg$37u15$2@dont-email.me> <TGs*5Arqz@news.chiark.greenend.org.uk>
<ue1qc0$39hqa$3@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 15 Sep 2023 15:06:16 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="45cc9fa6fbd082e7ff5e80bf84879cb5";
logging-data="3468094"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/Zk+CStFJohhiWGIbKN1+RP1MoWzKMQXo="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:w5ACnlrH7PehtLYmmShlCd4Fi2k=
Content-Language: en-GB
In-Reply-To: <ue1qc0$39hqa$3@dont-email.me>
 by: The Natural Philosop - Fri, 15 Sep 2023 15:06 UTC

On 15/09/2023 15:40, candycanearter07 wrote:
> On 9/15/23 08:23, Theo wrote:
>> In comp.sys.raspberry-pi The Natural Philosopher <tnp@invalid.invalid>
>> wrote:
>>> On 15/09/2023 12:12, Ralf Fassel wrote:
>>>> |                             {
>>>> |                             *q++=0;
>>>> |                             thermometers[i].name=strdup(p); //
>>>> |                             make a copy of the name and attach it
>>>> |                             to our thermometer structure
>>>>
>>>> Memory leak if thermometers[i].name already contains something.
>>>>
>>> further up the line...
>>>
>>>          bzero(filbuf,sizeof(filbuf));
>>>          /** first thing to do is clean any allocated memory used to
>>> store
>>> values. **/
>>>          for(i=0;i<NUMBER_RELAYS;i++)
>>>                  free(thermometers[i].name);
>>
>> You could get a SIGABRT if you were trying to free something that was
>> already freed.  Are you sure those are interlocked such that for each
>> i you
>> call strdup() exactly once, and subsequently free() exactly once?  If
>> there
>> was some code path that was breaking out of the loop or similar you might
>> get such behaviour.
>>
>> Theo
>
> I thought double free was a SIGSEGV?

In fact it seems fairly undefined

It looks like it is somewhat implementation dependent. SIGSEGV means you
accessed unallocated memory, but that is not the same as freeing
allocated memory, twice.

There seem to be instances of it reported. Google is a friend here.

I *suspect* that if that is the problem, its a signal from deep within libc.
Whereas SIGSEGV probably emanates from a memory management unit somewhere

--
"Strange as it seems, no amount of learning can cure stupidity, and
higher education positively fortifies it."

- Stephen Vizinczey

Re: Weird code crash

<wwvmsxncw2o.fsf@LkoBDZeT.terraraq.uk>

  copy mid

https://www.novabbs.com/computers/article-flat.php?id=7199&group=comp.sys.raspberry-pi#7199

  copy link   Newsgroups: comp.os.linux.misc comp.sys.raspberry-pi
Path: i2pn2.org!i2pn.org!news.chmurka.net!nntp.terraraq.uk!.POSTED.tunnel.sfere.anjou.terraraq.org.uk!not-for-mail
From: inva...@invalid.invalid (Richard Kettlewell)
Newsgroups: comp.os.linux.misc,comp.sys.raspberry-pi
Subject: Re: Weird code crash
Date: Fri, 15 Sep 2023 16:09:19 +0100
Organization: terraraq NNTP server
Message-ID: <wwvmsxncw2o.fsf@LkoBDZeT.terraraq.uk>
References: <udu5c4$2gutd$1@dont-email.me> <ygamsxoixhx.fsf@akutech.de>
<udv982$2lppb$4@dont-email.me> <ygail8biyxm.fsf@akutech.de>
<ue1b2v$36ji2$1@dont-email.me> <ygaedizitb9.fsf@akutech.de>
<ue1idg$37u15$2@dont-email.me> <TGs*5Arqz@news.chiark.greenend.org.uk>
<ue1qc0$39hqa$3@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: innmantic.terraraq.uk; posting-host="tunnel.sfere.anjou.terraraq.org.uk:172.17.207.6";
logging-data="99456"; mail-complaints-to="usenet@innmantic.terraraq.uk"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:Ph7JBGaAej2BNTGXMggCt0VCgzU=
X-Face: h[Hh-7npe<<b4/eW[]sat,I3O`t8A`(ej.H!F4\8|;ih)`7{@:A~/j1}gTt4e7-n*F?.Rl^
F<\{jehn7.KrO{!7=:(@J~]<.[{>v9!1<qZY,{EJxg6?Er4Y7Ng2\Ft>Z&W?r\c.!4DXH5PWpga"ha
+r0NzP?vnz:e/knOY)PI-
X-Boydie: NO
 by: Richard Kettlewell - Fri, 15 Sep 2023 15:09 UTC

candycanearter07 <no@thanks.net> writes:
> On 9/15/23 08:23, Theo wrote:
>> You could get a SIGABRT if you were trying to free something that was
>> already freed. Are you sure those are interlocked such that for each
>> i you call strdup() exactly once, and subsequently free() exactly
>> once? If there was some code path that was breaking out of the loop
>> or similar you might get such behaviour.
>
> I thought double free was a SIGSEGV?

If Glibc detects it you’ll get a diagnostic and SIGABRT.

If it doesn’t detect it then anything could happen - SIGSEGV is just one
possibility.

--
https://www.greenend.org.uk/rjk/

Re: Weird code crash

<ue1s8o$39es4$1@dont-email.me>

  copy mid

https://www.novabbs.com/computers/article-flat.php?id=7200&group=comp.sys.raspberry-pi#7200

  copy link   Newsgroups: comp.os.linux.misc comp.sys.raspberry-pi
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: val...@cultnix.org (vallor)
Newsgroups: comp.os.linux.misc,comp.sys.raspberry-pi
Subject: Re: Weird code crash
Date: Fri, 15 Sep 2023 15:12:24 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 62
Message-ID: <ue1s8o$39es4$1@dont-email.me>
References: <udu5c4$2gutd$1@dont-email.me> <ygamsxoixhx.fsf@akutech.de>
<udv982$2lppb$4@dont-email.me> <ygail8biyxm.fsf@akutech.de>
<ue1b2v$36ji2$1@dont-email.me> <ygaedizitb9.fsf@akutech.de>
<ue1idg$37u15$2@dont-email.me> <TGs*5Arqz@news.chiark.greenend.org.uk>
<ue1nq7$39033$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 15 Sep 2023 15:12:24 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="7adb3027fbbf5f0e63a7aa98b6f8b4f4";
logging-data="3455876"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/wX2Mczh9Dsi9LhI3wRZ7x"
Cancel-Lock: sha1:tIopR1VaozlJY4PFkM5xiwEmC8I=
X-Face: \}2`P"_@pS86<'EM:'b.Ml}8IuMK"pV"?FReF$'c.S%u9<Q#U*4QO)$l81M`{Q/n
XL'`91kd%N::LG:=*\35JS0prp\VJN^<s"b#bff@fA7]5lJA.jn,x_d%Md$,{.EZ
 by: vallor - Fri, 15 Sep 2023 15:12 UTC

On Fri, 15 Sep 2023 14:56:23 +0100, The Natural Philosopher
<tnp@invalid.invalid> wrote in <ue1nq7$39033$1@dont-email.me>:

> On 15/09/2023 14:23, Theo wrote:
>> In comp.sys.raspberry-pi The Natural Philosopher <tnp@invalid.invalid>
>> wrote:
>>> On 15/09/2023 12:12, Ralf Fassel wrote:
>>>> | {
>>>> | *q++=0;
>>>> | thermometers[i].name=strdup(p); //
>>>> | make a copy of the name and attach it |
>>>> to our thermometer structure
>>>>
>>>> Memory leak if thermometers[i].name already contains something.
>>>>
>>> further up the line...
>>>
>>> bzero(filbuf,sizeof(filbuf));
>>> /** first thing to do is clean any allocated memory used to
>>> store
>>> values. **/
>>> for(i=0;i<NUMBER_RELAYS;i++)
>>> free(thermometers[i].name);
>>
>> You could get a SIGABRT if you were trying to free something that was
>> already freed. Are you sure those are interlocked such that for each i
>> you call strdup() exactly once, and subsequently free() exactly once?
>> If there was some code path that was breaking out of the loop or
>> similar you might get such behaviour.
>>
> Hmm. I free the pointers even for relay zones that don't have
> thermometers, whose pointers are 0. That isn't an issue.
>
> But that might be a remotely possible issue. I dont zero the pointers
> after freeing them as far as I can tell. The silly thing is that this
> program doesn't use the name anyway.
>
> Its used elsewhere Well I don't think its an issue, but I can zero the
> pointers anyway after free()ing
>
>> Theo

Hi, read the thread with interest.

If you're getting SIGABRT, that's almost always the software
calling abort(3). If you aren't, maybe there's a library calling it?

$ man 7 signal
[...]
Signal Standard Action Comment
SIGABRT P1990 Core Abort signal from abort(3)
[but it also lists]
SIGIOT - Core IOT trap. A synonym for SIGABRT
_ _ _ _ _ _ _

Meanwhile, if you want to avoid locking your file, you might want to write
a fresh file with a unique name, then rename() it,
which -- please correct me if I'm wrong -- should replace
the desired file atomically.

--
-v

Re: Weird code crash

<ue1shq$34d9o$1@dont-email.me>

  copy mid

https://www.novabbs.com/computers/article-flat.php?id=7201&group=comp.sys.raspberry-pi#7201

  copy link   Newsgroups: comp.os.linux.misc comp.sys.raspberry-pi
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: mar...@mydomain.invalid (Martin Gregorie)
Newsgroups: comp.os.linux.misc,comp.sys.raspberry-pi
Subject: Re: Weird code crash
Date: Fri, 15 Sep 2023 15:17:14 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 28
Message-ID: <ue1shq$34d9o$1@dont-email.me>
References: <udu5c4$2gutd$1@dont-email.me>
<VGs*53kqz@news.chiark.greenend.org.uk>
<wwv8r99b1ui.fsf@LkoBDZeT.terraraq.uk> <udusa0$2k1q0$1@dont-email.me>
<wwvpm2jgagf.fsf@LkoBDZeT.terraraq.uk> <ue1966$36a0j$1@dont-email.me>
<ue1eki$36hof$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 15 Sep 2023 15:17:14 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="93bcf7a089b38464364fcd026599721d";
logging-data="3290424"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18B1LY+MlD6PwVZiSHE0g84bDb8BGJ/HoI="
User-Agent: Pan/0.149 (Bellevue; 4c157ba git@gitlab.gnome.org:GNOME/pan.git)
Cancel-Lock: sha1:u2QlseTBv54cMYeQ0bwPTc4toAM=
 by: Martin Gregorie - Fri, 15 Sep 2023 15:17 UTC

On Fri, 15 Sep 2023 12:19:45 +0100, Pancho wrote:

> Personally, I want to run with full debug, stack trace, logging,
> exception handling, and bounds checking turned on all the time, even in
> production. Which is why I generally use a modern language like C# or
> Java.
>
Same here. Many years back I wrote the type of debugging and programming
support library I personally find most useful: it can report the content
of all common variable types as well as dumping byte arrays as both hex
and ASCII as well as parsing the command line and allow the amount of
debug info the be controlled by a command line argument.

They are structured as small libraries that designed to be lightweight
enough to be left in a program when its in general use.

The library was originally written in C, but I soon wrote a Java version
as well, though this hasn't been separately published yet.

If this sounds useful, both versions can be found on www.libelle-
systems.com in the "Free Stuff" section.

--

Martin | martin at
Gregorie | gregorie dot org

Re: Weird code crash

<ue1t2j$3a5fs$1@dont-email.me>

  copy mid

https://www.novabbs.com/computers/article-flat.php?id=7202&group=comp.sys.raspberry-pi#7202

  copy link   Newsgroups: comp.os.linux.misc comp.sys.raspberry-pi
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ric...@example.invalid (Rich)
Newsgroups: comp.os.linux.misc,comp.sys.raspberry-pi
Subject: Re: Weird code crash
Date: Fri, 15 Sep 2023 15:26:11 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 40
Message-ID: <ue1t2j$3a5fs$1@dont-email.me>
References: <udu5c4$2gutd$1@dont-email.me> <ygamsxoixhx.fsf@akutech.de> <udv982$2lppb$4@dont-email.me> <ygail8biyxm.fsf@akutech.de> <ue1b2v$36ji2$1@dont-email.me> <ygaedizitb9.fsf@akutech.de> <ue1idg$37u15$2@dont-email.me> <yga4jjvik9o.fsf@akutech.de> <ue1r8m$39qpu$1@dont-email.me>
Injection-Date: Fri, 15 Sep 2023 15:26:11 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="9ed59c27ef1e98f694f4178ed42cabf8";
logging-data="3479036"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19ZAwiO6HV6sENoVnh1uU1F"
User-Agent: tin/2.6.1-20211226 ("Convalmore") (Linux/5.15.117 (x86_64))
Cancel-Lock: sha1:jlwsCDIkeqKPNwU9alxTHQPzExA=
 by: Rich - Fri, 15 Sep 2023 15:26 UTC

In comp.os.linux.misc The Natural Philosopher <tnp@invalid.invalid> wrote:
> On 15/09/2023 15:27, Ralf Fassel wrote:
>> Note that the assignment
>>
>> thermometers[i].name=strdup(p);
>>
>> is *inside* the while() loop without a free().
>>
>> Probably you argue that there ever is only a single file to read in
>> that dir anyway... Personally, I've been bitten by such
>> assumptions, so I'd rather check once too often than hunting down
>> "can't happen" bugs.
>>
> I do think that what has happened is that a valid file name has been
> found with empty data, or no file at all, and then no strdup is done
> - but the free is, next time around.
>
> That should never happen of course, as the fopen/fwrite sequence
> should certainly not delete the filename, but it is entirely possible
> that a the fopen *truncates* its data. At which point we cant strdup
> anything, so the next free gets a woopsie

Are the "files" being written to by an independent process separate
from this reading process?

If yes, are you doing any form of locking/synchronization to prevent
the reading process from trying to read from a file that a writing
process has open/truncated, but not yet written any data into?

If no, then you may be also hitting a race condition where the stars
align just right, the writer has just performed its fopen/truncate
(leaving the file empty) and the kernel decides to context switch away
to the reader at that point, before the writer can write and close the
file. The reader will then see an empty file.

The classic "lock free" solution to this one is for the writer to
create and write to a temporary file, and after closing the temp file
to rename() it to the name of the real file. Rename is documented to
be atomic, so the reader would never see a half open, or partially
complete, file in this case.

Re: Weird code crash

<ue1tol$3a7pg$1@dont-email.me>

  copy mid

https://www.novabbs.com/computers/article-flat.php?id=7203&group=comp.sys.raspberry-pi#7203

  copy link   Newsgroups: comp.os.linux.misc comp.sys.raspberry-pi
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: tnp...@invalid.invalid (The Natural Philosopher)
Newsgroups: comp.os.linux.misc,comp.sys.raspberry-pi
Subject: Re: Weird code crash
Date: Fri, 15 Sep 2023 16:37:57 +0100
Organization: A little, after lunch
Lines: 34
Message-ID: <ue1tol$3a7pg$1@dont-email.me>
References: <udu5c4$2gutd$1@dont-email.me> <ygamsxoixhx.fsf@akutech.de>
<udv982$2lppb$4@dont-email.me> <ygail8biyxm.fsf@akutech.de>
<ue1b2v$36ji2$1@dont-email.me> <ygaedizitb9.fsf@akutech.de>
<ue1idg$37u15$2@dont-email.me> <TGs*5Arqz@news.chiark.greenend.org.uk>
<ue1qc0$39hqa$3@dont-email.me> <wwvmsxncw2o.fsf@LkoBDZeT.terraraq.uk>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 15 Sep 2023 15:37:57 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="45cc9fa6fbd082e7ff5e80bf84879cb5";
logging-data="3481392"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/OCQ0HNWZCaNR2XvwcZf1XUE8xJgXT8Vo="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:CUihHByJR7SteSzfauN0mzwyrY8=
Content-Language: en-GB
In-Reply-To: <wwvmsxncw2o.fsf@LkoBDZeT.terraraq.uk>
 by: The Natural Philosop - Fri, 15 Sep 2023 15:37 UTC

On 15/09/2023 16:09, Richard Kettlewell wrote:
> candycanearter07 <no@thanks.net> writes:
>> On 9/15/23 08:23, Theo wrote:
>>> You could get a SIGABRT if you were trying to free something that was
>>> already freed. Are you sure those are interlocked such that for each
>>> i you call strdup() exactly once, and subsequently free() exactly
>>> once? If there was some code path that was breaking out of the loop
>>> or similar you might get such behaviour.
>>
>> I thought double free was a SIGSEGV?
>
> If Glibc detects it you’ll get a diagnostic and SIGABRT.

I think that is conclusive.

It seems to have been a double free caused by lack of defensive coding
plus an asynch timed file write function causing the temporary creation
of an empty file, or perhaps no file at all.

>
> If it doesn’t detect it then anything could happen - SIGSEGV is just one
> possibility.
>

--
I would rather have questions that cannot be answered...
....than to have answers that cannot be questioned

Richard Feynman

Re: Weird code crash

<ue1u5m$3a7pg$2@dont-email.me>

  copy mid

https://www.novabbs.com/computers/article-flat.php?id=7204&group=comp.sys.raspberry-pi#7204

  copy link   Newsgroups: comp.os.linux.misc comp.sys.raspberry-pi
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: tnp...@invalid.invalid (The Natural Philosopher)
Newsgroups: comp.os.linux.misc,comp.sys.raspberry-pi
Subject: Re: Weird code crash
Date: Fri, 15 Sep 2023 16:44:54 +0100
Organization: A little, after lunch
Lines: 65
Message-ID: <ue1u5m$3a7pg$2@dont-email.me>
References: <udu5c4$2gutd$1@dont-email.me> <ygamsxoixhx.fsf@akutech.de>
<udv982$2lppb$4@dont-email.me> <ygail8biyxm.fsf@akutech.de>
<ue1b2v$36ji2$1@dont-email.me> <ygaedizitb9.fsf@akutech.de>
<ue1idg$37u15$2@dont-email.me> <yga4jjvik9o.fsf@akutech.de>
<ue1r8m$39qpu$1@dont-email.me> <ue1t2j$3a5fs$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 15 Sep 2023 15:44:54 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="45cc9fa6fbd082e7ff5e80bf84879cb5";
logging-data="3481392"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19+Kj9D4HhCQVlt1/MyhS1EPTz4PjUZwps="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:pPsQb9FlqhIOIuB82X7iZTgzh3s=
Content-Language: en-GB
In-Reply-To: <ue1t2j$3a5fs$1@dont-email.me>
 by: The Natural Philosop - Fri, 15 Sep 2023 15:44 UTC

On 15/09/2023 16:26, Rich wrote:
> In comp.os.linux.misc The Natural Philosopher <tnp@invalid.invalid> wrote:
>> On 15/09/2023 15:27, Ralf Fassel wrote:
>>> Note that the assignment
>>>
>>> thermometers[i].name=strdup(p);
>>>
>>> is *inside* the while() loop without a free().
>>>
>>> Probably you argue that there ever is only a single file to read in
>>> that dir anyway... Personally, I've been bitten by such
>>> assumptions, so I'd rather check once too often than hunting down
>>> "can't happen" bugs.
>>>
>> I do think that what has happened is that a valid file name has been
>> found with empty data, or no file at all, and then no strdup is done
>> - but the free is, next time around.
>>
>> That should never happen of course, as the fopen/fwrite sequence
>> should certainly not delete the filename, but it is entirely possible
>> that a the fopen *truncates* its data. At which point we cant strdup
>> anything, so the next free gets a woopsie
>
> Are the "files" being written to by an independent process separate
> from this reading process?
>
Yes

> If yes, are you doing any form of locking/synchronization to prevent
> the reading process from trying to read from a file that a writing
> process has open/truncated, but not yet written any data into?
>
No.

> If no, then you may be also hitting a race condition where the stars
> align just right, the writer has just performed its fopen/truncate
> (leaving the file empty) and the kernel decides to context switch away
> to the reader at that point, before the writer can write and close the
> file. The reader will then see an empty file.
>
I think that is exactly the case. I didnt think that was in fact possible

> The classic "lock free" solution to this one is for the writer to
> create and write to a temporary file, and after closing the temp file
> to rename() it to the name of the real file. Rename is documented to
> be atomic, so the reader would never see a half open, or partially
> complete, file in this case.

Yes, I was just wondering that before I read this post. Rename unlinks
the old file does it?

I might implement that, as well. It doesn't really matter however, as
in practice the structures than contain thermometer data don't get
altered if no valid data is found, so the lack of a proper file, ex of
causing a crash, now simply means the (unused in this program) name data
gets erased. For a few seconds. It simply misses a reading and uses last
times data for everything else. Mostly the temperature.

--
Truth welcomes investigation because truth knows investigation will lead
to converts. It is deception that uses all the other techniques.

Re: Weird code crash

<ue1u93$3a7pg$3@dont-email.me>

  copy mid

https://www.novabbs.com/computers/article-flat.php?id=7205&group=comp.sys.raspberry-pi#7205

  copy link   Newsgroups: comp.os.linux.misc comp.sys.raspberry-pi
Path: i2pn2.org!i2pn.org!news.hispagatos.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: tnp...@invalid.invalid (The Natural Philosopher)
Newsgroups: comp.os.linux.misc,comp.sys.raspberry-pi
Subject: Re: Weird code crash
Date: Fri, 15 Sep 2023 16:46:43 +0100
Organization: A little, after lunch
Lines: 73
Message-ID: <ue1u93$3a7pg$3@dont-email.me>
References: <udu5c4$2gutd$1@dont-email.me> <ygamsxoixhx.fsf@akutech.de>
<udv982$2lppb$4@dont-email.me> <ygail8biyxm.fsf@akutech.de>
<ue1b2v$36ji2$1@dont-email.me> <ygaedizitb9.fsf@akutech.de>
<ue1idg$37u15$2@dont-email.me> <TGs*5Arqz@news.chiark.greenend.org.uk>
<ue1nq7$39033$1@dont-email.me> <ue1s8o$39es4$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 15 Sep 2023 15:46:43 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="45cc9fa6fbd082e7ff5e80bf84879cb5";
logging-data="3481392"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+R3HVnLpFjbVFYXBNHcnriPxqsuYwUINc="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:Wz2/8NllxAZeLmNsX4sBgU7wsOw=
Content-Language: en-GB
In-Reply-To: <ue1s8o$39es4$1@dont-email.me>
 by: The Natural Philosop - Fri, 15 Sep 2023 15:46 UTC

On 15/09/2023 16:12, vallor wrote:
> On Fri, 15 Sep 2023 14:56:23 +0100, The Natural Philosopher
> <tnp@invalid.invalid> wrote in <ue1nq7$39033$1@dont-email.me>:
>
>> On 15/09/2023 14:23, Theo wrote:
>>> In comp.sys.raspberry-pi The Natural Philosopher <tnp@invalid.invalid>
>>> wrote:
>>>> On 15/09/2023 12:12, Ralf Fassel wrote:
>>>>> | {
>>>>> | *q++=0;
>>>>> | thermometers[i].name=strdup(p); //
>>>>> | make a copy of the name and attach it |
>>>>> to our thermometer structure
>>>>>
>>>>> Memory leak if thermometers[i].name already contains something.
>>>>>
>>>> further up the line...
>>>>
>>>> bzero(filbuf,sizeof(filbuf));
>>>> /** first thing to do is clean any allocated memory used to
>>>> store
>>>> values. **/
>>>> for(i=0;i<NUMBER_RELAYS;i++)
>>>> free(thermometers[i].name);
>>>
>>> You could get a SIGABRT if you were trying to free something that was
>>> already freed. Are you sure those are interlocked such that for each i
>>> you call strdup() exactly once, and subsequently free() exactly once?
>>> If there was some code path that was breaking out of the loop or
>>> similar you might get such behaviour.
>>>
>> Hmm. I free the pointers even for relay zones that don't have
>> thermometers, whose pointers are 0. That isn't an issue.
>>
>> But that might be a remotely possible issue. I dont zero the pointers
>> after freeing them as far as I can tell. The silly thing is that this
>> program doesn't use the name anyway.
>>
>> Its used elsewhere Well I don't think its an issue, but I can zero the
>> pointers anyway after free()ing
>>
>>> Theo
>
> Hi, read the thread with interest.
>
> If you're getting SIGABRT, that's almost always the software
> calling abort(3). If you aren't, maybe there's a library calling it?
>
> $ man 7 signal
> [...]
> Signal Standard Action Comment
> SIGABRT P1990 Core Abort signal from abort(3)
> [but it also lists]
> SIGIOT - Core IOT trap. A synonym for SIGABRT
> _ _ _ _ _ _ _
>
> Meanwhile, if you want to avoid locking your file, you might want to write
> a fresh file with a unique name, then rename() it,
> which -- please correct me if I'm wrong -- should replace
> the desired file atomically.
>

I think the consensus is that it does.

Presumably if the read process has the old file open, that will be valid
until it closes it?

--
"I guess a rattlesnake ain't risponsible fer bein' a rattlesnake, but ah
puts mah heel on um jess the same if'n I catches him around mah chillun".

Re: Weird code crash

<ygazg1nh0sm.fsf@akutech.de>

  copy mid

https://www.novabbs.com/computers/article-flat.php?id=7206&group=comp.sys.raspberry-pi#7206

  copy link   Newsgroups: comp.os.linux.misc comp.sys.raspberry-pi
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: ralf...@gmx.de (Ralf Fassel)
Newsgroups: comp.os.linux.misc,comp.sys.raspberry-pi
Subject: Re: Weird code crash
Date: Fri, 15 Sep 2023 18:13:45 +0200
Lines: 65
Message-ID: <ygazg1nh0sm.fsf@akutech.de>
References: <udu5c4$2gutd$1@dont-email.me> <ygamsxoixhx.fsf@akutech.de>
<udv982$2lppb$4@dont-email.me> <ygail8biyxm.fsf@akutech.de>
<ue1b2v$36ji2$1@dont-email.me> <ygaedizitb9.fsf@akutech.de>
<ue1idg$37u15$2@dont-email.me> <yga4jjvik9o.fsf@akutech.de>
<ue1r8m$39qpu$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain
X-Trace: individual.net oK7haGuPzYGnIfDYOTl45wteCAOa2u7zWokRhkzfFKC7SbASQ=
Cancel-Lock: sha1:VDPTyGli3cMkZkTi1Qp2CCn1g1k= sha1:SfAQZwgn6jnxP+SnfCvYyvIwL3w= sha256:MrLiqeXkxlbNpCoc5c/6s4/mauEx88+yb663z6VFbMo=
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
 by: Ralf Fassel - Fri, 15 Sep 2023 16:13 UTC

* The Natural Philosopher <tnp@invalid.invalid>
| On 15/09/2023 15:27, Ralf Fassel wrote:
| > * The Natural Philosopher <tnp@invalid.invalid>
| > | > | thermometers[i].name=strdup(p); //
| > | > | make a copy of the name and attach it
| > | > | to our thermometer structure
| > | > Memory leak if thermometers[i].name already contains something.
| > | >
| > | further up the line...
| >>
| > | bzero(filbuf,sizeof(filbuf));
| > | /** first thing to do is clean any allocated memory used to
| > | store values. **/
| > | for(i=0;i<NUMBER_RELAYS;i++)
| > | free(thermometers[i].name);
| > Note that the assignment
| > thermometers[i].name=strdup(p);
| > is *inside* the while() loop without a free().
| > Probably you argue that there ever is only a single file to read in
| > that dir anyway... Personally, I've been bitten by such assumptions, so I'd
| > rather check once too often than hunting down "can't happen" bugs.
| > R'
| >
| No. you have misunderstood how the code works.

Sorry, but I have to give that compliment back. You describe how the
code is _intended_ to work. I described how the code _actually_ works.

It all depends on what files with which content are there in that
directory, so if there ever is only one file per ZONE, all is peachy.
If not, all bets are off.

Not 100% seriously, may I refer you to
https://core.tcl-lang.org/tips/doc/trunk/tip/131.md
;-)

| (It would be trivial to simply add a conditional that only strdups to
| a pointer if it is NULL).

With char* malloc'd pointers, I find it much easier to simply stick to
the pattern:
- initialize to 0
- free before reassignment
- assign to 0 after free when not directly reassigning
instead of arguing at each place why not sticking to the pattern is not
a problem.

| However they are not at this time misconfigured, so it shouldn't be
| the crash problem, [...]

Agreed.

| I do think that what has happened is that a valid file name has been
| found with empty data, or no file at all, and then no strdup is done -
| but the free is, next time around.

Easy to verify via diagnostics, just add a stderr-output for every
unexpected situation (such as the same index seen twice etc).

| As is allocating memory only if the pointers are null.

Why not simply free()/strdup()? If you assign to 0 only, you may get
old contents for the new file inside the loop (can't happen, I know :-)!

R'

Re: Weird code crash

<ygav8cbh0ji.fsf@akutech.de>

  copy mid

https://www.novabbs.com/computers/article-flat.php?id=7207&group=comp.sys.raspberry-pi#7207

  copy link   Newsgroups: comp.os.linux.misc comp.sys.raspberry-pi
Path: i2pn2.org!rocksolid2!i2pn.org!paganini.bofh.team!2.eu.feeder.erje.net!feeder.erje.net!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: ralf...@gmx.de (Ralf Fassel)
Newsgroups: comp.os.linux.misc,comp.sys.raspberry-pi
Subject: Re: Weird code crash
Date: Fri, 15 Sep 2023 18:19:13 +0200
Lines: 19
Message-ID: <ygav8cbh0ji.fsf@akutech.de>
References: <udu5c4$2gutd$1@dont-email.me> <ygamsxoixhx.fsf@akutech.de>
<udv982$2lppb$4@dont-email.me> <ygail8biyxm.fsf@akutech.de>
<ue1b2v$36ji2$1@dont-email.me> <ygaedizitb9.fsf@akutech.de>
<ue1idg$37u15$2@dont-email.me> <TGs*5Arqz@news.chiark.greenend.org.uk>
<ue1nq7$39033$1@dont-email.me> <ue1s8o$39es4$1@dont-email.me>
<ue1u93$3a7pg$3@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain
X-Trace: individual.net CwPI71TDUwj96db1uwqukgao3KMErSPo6gfzfbOihbijvOVdo=
Cancel-Lock: sha1:7KZ6refXPS0GDNZY79JUUB/tvAQ= sha1:lUu6hr7IsjhfBzcxwizsey3vYGc= sha256:UWFDXCjfIXzvMTP5B+HTv3kHHcBgzaN+HkFF+Rt39yQ=
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
 by: Ralf Fassel - Fri, 15 Sep 2023 16:19 UTC

* The Natural Philosopher <tnp@invalid.invalid>
| On 15/09/2023 16:12, vallor wrote:
| > Meanwhile, if you want to avoid locking your file, you might want to
| > write
| > a fresh file with a unique name, then rename() it,
| > which -- please correct me if I'm wrong -- should replace
| > the desired file atomically.
>
| I think the consensus is that it does.
>
| Presumably if the read process has the old file open, that will be
| valid until it closes it?

On Linux: yes. Once a process has a file open, it sees the 'old'
contents if the file is removed from disk.

https://stackoverflow.com/questions/2028874/what-happens-to-an-open-file-handle-on-linux-if-the-pointed-file-gets-moved-or-d

R'

Re: Weird code crash

<ue20b6$39es4$2@dont-email.me>

  copy mid

https://www.novabbs.com/computers/article-flat.php?id=7208&group=comp.sys.raspberry-pi#7208

  copy link   Newsgroups: comp.os.linux.misc comp.sys.raspberry-pi
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: val...@cultnix.org (vallor)
Newsgroups: comp.os.linux.misc,comp.sys.raspberry-pi
Subject: Re: Weird code crash
Date: Fri, 15 Sep 2023 16:21:58 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 75
Message-ID: <ue20b6$39es4$2@dont-email.me>
References: <udu5c4$2gutd$1@dont-email.me> <ygamsxoixhx.fsf@akutech.de>
<udv982$2lppb$4@dont-email.me> <ygail8biyxm.fsf@akutech.de>
<ue1b2v$36ji2$1@dont-email.me> <ygaedizitb9.fsf@akutech.de>
<ue1idg$37u15$2@dont-email.me> <TGs*5Arqz@news.chiark.greenend.org.uk>
<ue1nq7$39033$1@dont-email.me> <ue1s8o$39es4$1@dont-email.me>
<ue1u93$3a7pg$3@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 15 Sep 2023 16:21:58 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="7adb3027fbbf5f0e63a7aa98b6f8b4f4";
logging-data="3455876"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19c1ZIqzZOqNKH9jU9Hyr4U"
Cancel-Lock: sha1:acKGUS6jRSWJZc9M1Znd5gZ427A=
X-Face: \}2`P"_@pS86<'EM:'b.Ml}8IuMK"pV"?FReF$'c.S%u9<Q#U*4QO)$l81M`{Q/n
XL'`91kd%N::LG:=*\35JS0prp\VJN^<s"b#bff@fA7]5lJA.jn,x_d%Md$,{.EZ
 by: vallor - Fri, 15 Sep 2023 16:21 UTC

On Fri, 15 Sep 2023 16:46:43 +0100, The Natural Philosopher
<tnp@invalid.invalid> wrote in <ue1u93$3a7pg$3@dont-email.me>:

> On 15/09/2023 16:12, vallor wrote:
>> On Fri, 15 Sep 2023 14:56:23 +0100, The Natural Philosopher
>> <tnp@invalid.invalid> wrote in <ue1nq7$39033$1@dont-email.me>:
>>
>>> On 15/09/2023 14:23, Theo wrote:
>>>> In comp.sys.raspberry-pi The Natural Philosopher
>>>> <tnp@invalid.invalid> wrote:
>>>>> On 15/09/2023 12:12, Ralf Fassel wrote:
>>>>>> | {
>>>>>> | *q++=0;
>>>>>> | thermometers[i].name=strdup(p); //
>>>>>> | make a copy of the name and attach it
>>>>>> |
>>>>>> to our thermometer structure
>>>>>>
>>>>>> Memory leak if thermometers[i].name already contains something.
>>>>>>
>>>>> further up the line...
>>>>>
>>>>> bzero(filbuf,sizeof(filbuf));
>>>>> /** first thing to do is clean any allocated memory used
>>>>> to store
>>>>> values. **/
>>>>> for(i=0;i<NUMBER_RELAYS;i++)
>>>>> free(thermometers[i].name);
>>>>
>>>> You could get a SIGABRT if you were trying to free something that was
>>>> already freed. Are you sure those are interlocked such that for each
>>>> i you call strdup() exactly once, and subsequently free() exactly
>>>> once? If there was some code path that was breaking out of the loop
>>>> or similar you might get such behaviour.
>>>>
>>> Hmm. I free the pointers even for relay zones that don't have
>>> thermometers, whose pointers are 0. That isn't an issue.
>>>
>>> But that might be a remotely possible issue. I dont zero the pointers
>>> after freeing them as far as I can tell. The silly thing is that this
>>> program doesn't use the name anyway.
>>>
>>> Its used elsewhere Well I don't think its an issue, but I can zero the
>>> pointers anyway after free()ing
>>>
>>>> Theo
>>
>> Hi, read the thread with interest.
>>
>> If you're getting SIGABRT, that's almost always the software calling
>> abort(3). If you aren't, maybe there's a library calling it?
>>
>> $ man 7 signal [...]
>> Signal Standard Action Comment SIGABRT P1990
>> Core Abort signal from abort(3)
>> [but it also lists]
>> SIGIOT - Core IOT trap. A synonym for SIGABRT
>> _ _ _ _ _ _ _
>>
>> Meanwhile, if you want to avoid locking your file, you might want to
>> write a fresh file with a unique name, then rename() it,
>> which -- please correct me if I'm wrong -- should replace the desired
>> file atomically.
>>
>>
> I think the consensus is that it does.
>
> Presumably if the read process has the old file open, that will be valid
> until it closes it?

Yes -- and the old file remains allocated on disk until
its file descriptor is closed.

--
-v

Re: Weird code crash

<ue20mi$39es4$3@dont-email.me>

  copy mid

https://www.novabbs.com/computers/article-flat.php?id=7209&group=comp.sys.raspberry-pi#7209

  copy link   Newsgroups: comp.os.linux.misc comp.sys.raspberry-pi
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: val...@cultnix.org (vallor)
Newsgroups: comp.os.linux.misc,comp.sys.raspberry-pi
Subject: Re: Weird code crash
Date: Fri, 15 Sep 2023 16:28:02 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 31
Message-ID: <ue20mi$39es4$3@dont-email.me>
References: <udu5c4$2gutd$1@dont-email.me> <ygamsxoixhx.fsf@akutech.de>
<udv982$2lppb$4@dont-email.me> <ygail8biyxm.fsf@akutech.de>
<ue1b2v$36ji2$1@dont-email.me> <ygaedizitb9.fsf@akutech.de>
<ue1idg$37u15$2@dont-email.me> <TGs*5Arqz@news.chiark.greenend.org.uk>
<ue1nq7$39033$1@dont-email.me> <ue1s8o$39es4$1@dont-email.me>
<ue1u93$3a7pg$3@dont-email.me> <ygav8cbh0ji.fsf@akutech.de>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 15 Sep 2023 16:28:02 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="7adb3027fbbf5f0e63a7aa98b6f8b4f4";
logging-data="3455876"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/NB7X6yJMTrbLuzR1G5KyS"
Cancel-Lock: sha1:5hFqXkaiTsoJ50fuVtCg2jZquCA=
X-Face: \}2`P"_@pS86<'EM:'b.Ml}8IuMK"pV"?FReF$'c.S%u9<Q#U*4QO)$l81M`{Q/n
XL'`91kd%N::LG:=*\35JS0prp\VJN^<s"b#bff@fA7]5lJA.jn,x_d%Md$,{.EZ
 by: vallor - Fri, 15 Sep 2023 16:28 UTC

On Fri, 15 Sep 2023 18:19:13 +0200, Ralf Fassel <ralfixx@gmx.de> wrote in
<ygav8cbh0ji.fsf@akutech.de>:

> * The Natural Philosopher <tnp@invalid.invalid>
> | On 15/09/2023 16:12, vallor wrote:
> | > Meanwhile, if you want to avoid locking your file, you might want to
> | > write | > a fresh file with a unique name, then rename() it,
> | > which -- please correct me if I'm wrong -- should replace | > the
> desired file atomically.
>>
> | I think the consensus is that it does.
>>
> | Presumably if the read process has the old file open, that will be |
> valid until it closes it?
>
> On Linux: yes. Once a process has a file open, it sees the 'old'
> contents if the file is removed from disk.
>
> https://stackoverflow.com/questions/2028874/what-happens-to-an-open-
file-handle-on-linux-if-the-pointed-file-gets-moved-or-d
>
> R'

Speaking of which: back in the days of Linux yore, you
could retrieve the contents of a delete file if a
process still had it open through: /proc/##/fd/*.

(Nowadays, those are symlinks.)

--
-v

Re: Weird code crash

<wa1NM.13947$Lpcc.11499@fx42.iad>

  copy mid

https://www.novabbs.com/computers/article-flat.php?id=7210&group=comp.sys.raspberry-pi#7210

  copy link   Newsgroups: comp.os.linux.misc comp.sys.raspberry-pi
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx42.iad.POSTED!not-for-mail
Newsgroups: comp.os.linux.misc,comp.sys.raspberry-pi
From: cgi...@kltpzyxm.invalid (Charlie Gibbs)
Subject: Re: Weird code crash
References: <udu5c4$2gutd$1@dont-email.me> <ygamsxoixhx.fsf@akutech.de>
<udv982$2lppb$4@dont-email.me> <ygail8biyxm.fsf@akutech.de>
<ue1b2v$36ji2$1@dont-email.me> <ygaedizitb9.fsf@akutech.de>
<ue1idg$37u15$2@dont-email.me> <TGs*5Arqz@news.chiark.greenend.org.uk>
<ue1pue$39jhn$1@dont-email.me> <ue1ri8$39po5$1@dont-email.me>
User-Agent: slrn/1.0.3 (Linux)
Lines: 61
Message-ID: <wa1NM.13947$Lpcc.11499@fx42.iad>
X-Complaints-To: https://www.astraweb.com/aup
NNTP-Posting-Date: Fri, 15 Sep 2023 18:14:52 UTC
Date: Fri, 15 Sep 2023 18:14:52 GMT
X-Received-Bytes: 3615
 by: Charlie Gibbs - Fri, 15 Sep 2023 18:14 UTC

On 2023-09-15, Rich <rich@example.invalid> wrote:

> In comp.os.linux.misc The Natural Philosopher <tnp@invalid.invalid> wrote:
>
>> I had assumed that freeing a pointer that already had been freed would
>> either result in a NO-OP because the pointer no longer existed in the
>> heap memory allocation tables, or it would instantly crash , but it
>> seems that the action is 'undefined'.
>
> Yes, C explicitly labels "double free" as "undefined":
>
> <http://port70.net/~nsz/c/c99/n1256.html#J.2>
>
> Look under J.2 Undefined behavior (easiest is to search for "free"):
>
> J.2 Undefined behavior
>
> 1 The behavior is undefined in the following circumstances:
>
> ...
>
> The pointer argument to the free or realloc function does not match
> a pointer earlier returned by calloc, malloc, or realloc, or the
> space has been deallocated by a call to free or realloc (7.20.3.2,
> 7.20.3.4).
>
> And th 7.20.3.2 link in the page jumps to this:
>
> The free function causes the space pointed to by ptr to be
> deallocated, that is, made available for further allocation. If
> ptr is a null pointer, no action occurs. Otherwise, if the
> argument does not match a pointer earlier returned by the calloc,
> malloc, or realloc function, or if the space has been deallocated
> by a call to free or realloc, the behavior is undefined.
>
> So if by chance you are double-freeing sometimes, then you are tickling
> the undefined behaviour devil, and all bets are off as to what might
> eventually occur.

That's why I take steps to avoid it. Since files and dynamically-
allocated memory are global resources, I declare file and memory
pointers as global variables. I initialize them to NULL as an
indication that the file is not open or memory is not allocated.
If fopen() or malloc() fails, the pointer remains NULL and serves
as an indication of failure. When I close a file or free a block
of memory, I immediately set the corresponding pointer to NULL.
All my programs exit through a routine I call quit_cleanup(),
which closes files or frees memory for any non-NULL pointer
before calling exit(). I can call this function at any time
(e.g. to force abnormal termination) and everything will be
appropriately freed. Similarly, I can check a pointer for
non-NULL before opening a file or allocating memory (although
realloc() takes care of that automatically). It's quite
effective in avoiding resource leaks as well as the undefined
behaviour mentioned above.

--
/~\ Charlie Gibbs | They offer a huge range of
\ / <cgibbs@kltpzyxm.invalid> | world-class vulnerabilities
X I'm really at ac.dekanfrus | that only Microsoft can provide.
/ \ if you read it the right way. | -- druck <news@druck.org.uk>

Re: Weird code crash

<xa1NM.13948$Lpcc.12241@fx42.iad>

  copy mid

https://www.novabbs.com/computers/article-flat.php?id=7211&group=comp.sys.raspberry-pi#7211

  copy link   Newsgroups: comp.os.linux.misc comp.sys.raspberry-pi
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx42.iad.POSTED!not-for-mail
Newsgroups: comp.os.linux.misc,comp.sys.raspberry-pi
From: cgi...@kltpzyxm.invalid (Charlie Gibbs)
Subject: Re: Weird code crash
References: <udu5c4$2gutd$1@dont-email.me> <ygamsxoixhx.fsf@akutech.de>
<udv982$2lppb$4@dont-email.me> <ygail8biyxm.fsf@akutech.de>
<ue1b2v$36ji2$1@dont-email.me> <ygaedizitb9.fsf@akutech.de>
<ue1idg$37u15$2@dont-email.me> <yga4jjvik9o.fsf@akutech.de>
<ue1r8m$39qpu$1@dont-email.me> <ue1t2j$3a5fs$1@dont-email.me>
User-Agent: slrn/1.0.3 (Linux)
Lines: 38
Message-ID: <xa1NM.13948$Lpcc.12241@fx42.iad>
X-Complaints-To: https://www.astraweb.com/aup
NNTP-Posting-Date: Fri, 15 Sep 2023 18:14:53 UTC
Date: Fri, 15 Sep 2023 18:14:53 GMT
X-Received-Bytes: 2670
 by: Charlie Gibbs - Fri, 15 Sep 2023 18:14 UTC

On 2023-09-15, Rich <rich@example.invalid> wrote:

> The classic "lock free" solution to this one is for the writer to
> create and write to a temporary file, and after closing the temp file
> to rename() it to the name of the real file. Rename is documented to
> be atomic, so the reader would never see a half open, or partially
> complete, file in this case.

I've always deleted the original file before doing the rename,
probably because under MS-DOS or Windows the rename will fail
if the original file already exists.

As a side note, don't use this technique if you're porting your
code to Windows, where the delete/rename paradigm is permanently
and incurably unreliable. When you ask Windows to delete a file,
it queues the request and doesn't actually delete the file until
it feels like it. Sometimes this doesn't happen until after
you've attempted the rename, which then fails because the original
file is still there. Some time afterwards, Windows gets around
to doing the delete, and once your cleanup routine gets rid of
the work file, your data is gone for good.

This doesn't happen very often, but even a 0.01% failure rate
can result in a trouble call every week or two if your program
is running daily at 1000 customer sites.

I discovered this little "feature" back in the Windows 95 days,
and as far as I know it's still there. The only safe workaround
is to copy the contents of the work file back to the original
file. For small files the time difference is neglegible, and
for large files... well, too bad, so sad. Suck it up and wait.
Or convince the customer to switch to Linux.

--
/~\ Charlie Gibbs | They offer a huge range of
\ / <cgibbs@kltpzyxm.invalid> | world-class vulnerabilities
X I'm really at ac.dekanfrus | that only Microsoft can provide.
/ \ if you read it the right way. | -- druck <news@druck.org.uk>

Re: Weird code crash

<ue27m8$3c45e$1@dont-email.me>

  copy mid

https://www.novabbs.com/computers/article-flat.php?id=7212&group=comp.sys.raspberry-pi#7212

  copy link   Newsgroups: comp.os.linux.misc comp.sys.raspberry-pi
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ric...@example.invalid (Rich)
Newsgroups: comp.os.linux.misc,comp.sys.raspberry-pi
Subject: Re: Weird code crash
Date: Fri, 15 Sep 2023 18:27:20 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 74
Message-ID: <ue27m8$3c45e$1@dont-email.me>
References: <udu5c4$2gutd$1@dont-email.me> <ygamsxoixhx.fsf@akutech.de> <udv982$2lppb$4@dont-email.me> <ygail8biyxm.fsf@akutech.de> <ue1b2v$36ji2$1@dont-email.me> <ygaedizitb9.fsf@akutech.de> <ue1idg$37u15$2@dont-email.me> <yga4jjvik9o.fsf@akutech.de> <ue1r8m$39qpu$1@dont-email.me> <ue1t2j$3a5fs$1@dont-email.me> <ue1u5m$3a7pg$2@dont-email.me>
Injection-Date: Fri, 15 Sep 2023 18:27:20 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="9ed59c27ef1e98f694f4178ed42cabf8";
logging-data="3543214"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19W1xQ56/kMAC1MC1XQY4IY"
User-Agent: tin/2.6.1-20211226 ("Convalmore") (Linux/5.15.117 (x86_64))
Cancel-Lock: sha1:gZBidhDM834FVIf9hrd2LEi8YJc=
 by: Rich - Fri, 15 Sep 2023 18:27 UTC

In comp.os.linux.misc The Natural Philosopher <tnp@invalid.invalid> wrote:
> On 15/09/2023 16:26, Rich wrote:
>> Are the "files" being written to by an independent process separate
>> from this reading process?
>>
> Yes
>
>> If yes, are you doing any form of locking/synchronization to prevent
>> the reading process from trying to read from a file that a writing
>> process has open/truncated, but not yet written any data into?
>>
> No.
>
>> If no, then you may be also hitting a race condition where the stars
>> align just right, the writer has just performed its fopen/truncate
>> (leaving the file empty) and the kernel decides to context switch
>> away to the reader at that point, before the writer can write and
>> close the file. The reader will then see an empty file.
>>
> I think that is exactly the case. I didnt think that was in fact
> possible

It is. One of the points where Linux evaluates to determe if it should
task switch is upon exit from a syscall. If your writer process runs
out its timeslice during the in-kernel portion of the work for an
fopen, then the kernel will suspend it and schedule another process to
run. You now have an empty, unwritten file on disk which will not be
written to until the writer is next scheduled by the kernel. If the
next process scheduled is the reader, and it was last suspended just
before it did an fopen() on this same file, it will now fopen() an
empty file.

>> The classic "lock free" solution to this one is for the writer to
>> create and write to a temporary file, and after closing the temp file
>> to rename() it to the name of the real file. Rename is documented to
>> be atomic, so the reader would never see a half open, or partially
>> complete, file in this case.
>
> Yes, I was just wondering that before I read this post. Rename unlinks
> the old file does it?

Yes: (man 2 rename):

If newpath already exists, it will be atomically replaced, so that
there is no point at which another process attempting to access
newpath will find it missing. However, there will probably be a
window in which both oldpath and newpath refer to the file being
renamed.

> I might implement that, as well. It doesn't really matter however,
> as in practice the structures than contain thermometer data don't get
> altered if no valid data is found, so the lack of a proper file, ex
> of causing a crash, now simply means the (unused in this program)
> name data gets erased. For a few seconds. It simply misses a
> reading and uses last times data for everything else. Mostly the
> temperature.

Yes, your temperature monitoring was unaffected. But if the race was
sometimes triggering the pointer double-free that your loop previously
had, then the lack of atomicity was at least one trigger for the
intermittent crash.

So seems like two routes to fix:

1) remove the conditions that can cause a double-free to occur in the
code (seems like you've already done this from other posts)

2) use rename() to move newly written files into place for the reader,
so the reader never opens an empty file (exclusive of the writer
crashing before it wrote anything to the file).

For something that you'll potentially want to 'just run' for
months/years on end without daily care and feeding, doing both is the
better defense.

Pages:123
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor