From: Dan Carpenter <dan.carpenter@linaro.org>
To: David Lechner <dlechner@baylibre.com>
Cc: Jonathan Cameron <jic23@jic23.retrosnub.co.uk>,
linux-iio@vger.kernel.org, Kees Cook <kees@kernel.org>
Subject: Re: [bug report] iio: pressure: bmp280: drop sensor_data array
Date: Fri, 9 May 2025 13:01:49 +0300 [thread overview]
Message-ID: <aB3SjWjBVzuhZDJa@stanley.mountain> (raw)
In-Reply-To: <aB2Xam2JQ_eU9Bat@stanley.mountain>
No, I looked at test code from Noah Pendleton it has a mistake. He's
testing assignments, not initialization. It's a different thing.
I also looked up the relevant portion from the C11 standard (6.7.9)
— if it is an aggregate, every member is initialized (recursively)
according to these rules, and any padding is initialized to zero
bits;
Which sounds like padding is always zeroed, but the context of this
quote is that we didn't fully set every struct member. We drop
support for compiler that are over 10 years old so C11 covers
everything we support.
Here is the test code that Noah was using.
https://github.com/memfault/interrupt/blob/master/example/c-struct-padding-initialization/example.c
struct foo foo;
// 3. use { 0 } zero-initializer
memset(&foo, 0xa5, sizeof(foo));
foo = (struct foo){0};
There isn't an initializer on the foo struct. Then he does a memset()
and assigns a struct to foo. Assigning one struct to the other is a
different section of the C standard.
I created my own test:
struct foo {
unsigned int i;
unsigned char b;
// 3 bytes of padding inserted here, UNLESS -fpack-struct is used!
};
static void print_struct(void *buffer, int size)
{
unsigned char *p = buffer;
int i;
for (i = 0; i < sizeof(struct foo); i++) {
printf("0x%x ", p[i]);
}
printf("\n");
}
int main(int argc, char *argv[])
{
struct foo one = { 0, 0 };
struct foo two = { 0 };
struct foo three = { };
print_struct(&one, sizeof(struct foo));
print_struct(&two, sizeof(struct foo));
print_struct(&three, sizeof(struct foo));
return 0;
}
GCC does not initialize "one" because it's fully defined. Clang does
initialize it (because Clang goes above and beyond for security). The
rest are zeroed as expected.
regards,
dan carpenter
next prev parent reply other threads:[~2025-05-09 10:01 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-06 12:32 [bug report] iio: pressure: bmp280: drop sensor_data array Dan Carpenter
2025-05-06 14:25 ` David Lechner
2025-05-06 18:35 ` Dan Carpenter
2025-05-07 6:35 ` Jonathan Cameron
2025-05-07 7:41 ` Dan Carpenter
2025-05-07 13:33 ` David Lechner
2025-05-09 5:49 ` Dan Carpenter
2025-05-09 10:01 ` Dan Carpenter [this message]
2025-05-09 16:58 ` Kees Cook
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=aB3SjWjBVzuhZDJa@stanley.mountain \
--to=dan.carpenter@linaro.org \
--cc=dlechner@baylibre.com \
--cc=jic23@jic23.retrosnub.co.uk \
--cc=kees@kernel.org \
--cc=linux-iio@vger.kernel.org \
/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