public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
To: libc-alpha@sourceware.org
Cc: Vitaly Buka <vitalybuka@google.com>,
	Adhemerval Zanella <adhemerval.zanella@linaro.org>
Subject: commit 16adc58e73f3 (stdlib: Fix data race in __run_exit_handlers [BZ #27749])
Date: Mon, 13 Feb 2023 00:43:57 +0100	[thread overview]
Message-ID: <0a4a8828-ed9a-66d4-f65c-f2f386768bdb@prevas.dk> (raw)

I think the commit in $subject is broken. I was browsing through the
atexit handling code and stumbled on something which is always a code
smell, namely the pattern

  if (whatever)
    continue;

at the end of a loop.

And indeed, since we no longer jump back to the outer loop and refetch
the list head, one can observe a change in behavior. This program:

#include <stdio.h>
#include <stdlib.h>

void h(void)
{
	printf("third: %s()\n", __func__);
}
void j(void)
{
	printf("second: %s()\n", __func__);
}

void g(void)
{
	printf("first: %s()\n", __func__);
	atexit(h);
	atexit(j);
}
void f(void) {
	static int c;
	printf("%s: %d\n", __func__, ++c);
}

int main(int argc, char *argv[])
{
	int i;

	/*
	 * Stuff the "struct exit_function_list" with dummy callbacks;
	 * 30 may need to be adjusted depending on how many atexit()
	 * registrations libc itself does.
	 */
	for (i = 0; i < 30; ++i)
		atexit(f);

	/* Register one more, to fill the last slot in struct
exit_function_list. */
	atexit(g);
	
	return 0;
}

used to print

first: g()
second: j()
third: h()
f: 1
...
f: 30

but now it instead prints

first: g()
third: h()
f: 1
...
f: 30
second: j()


Rasmus

             reply	other threads:[~2023-02-12 23:44 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-12 23:43 Rasmus Villemoes [this message]
2023-02-13  1:54 ` Vitaly Buka
2023-02-13  5:57   ` [PATCH] stdlib: Undo post review change to 16adc58e73f3 [BZ #27749] Vitaly Buka
2023-02-13  6:01 ` Vitaly Buka
2023-02-13 16:27   ` Adhemerval Zanella Netto
2023-02-18 20:53     ` Vitaly Buka
2023-02-20 12:20       ` Adhemerval Zanella Netto

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=0a4a8828-ed9a-66d4-f65c-f2f386768bdb@prevas.dk \
    --to=rasmus.villemoes@prevas.dk \
    --cc=adhemerval.zanella@linaro.org \
    --cc=libc-alpha@sourceware.org \
    --cc=vitalybuka@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).