public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Avoiding stack buffer clear being optimised out
@ 2022-11-30 16:26 Jonny Grant
  2022-11-30 17:40 ` Jonathan Wakely
  2022-11-30 18:47 ` David Brown
  0 siblings, 2 replies; 15+ messages in thread
From: Jonny Grant @ 2022-11-30 16:26 UTC (permalink / raw)
  To: gcc-help

[-- Attachment #1: Type: text/plain, Size: 1356 bytes --]

Hello

Does GCC have a clear way to avoid memset being compiled out by optimiser?

This article came up, so I combined the broken.c with GCC
gcc -Wall -O2 -o broken broken.c

Note, I've been using gcc for many years, I'm not looking for just tips how to compile code. I only want to discuss this optimiser issue :-)

https://blog.cloudflare.com/the-linux-kernel-key-retention-service-and-why-you-should-use-it-in-your-next-application/

If I modify to clear the buffer, it gets removed by the compiler

The only way I could get it to not remove the memset is by adding another printf, (propagating a return code after checking memset wasn't enough)

    // gcc -Wall -O2 -o broken broken.c

#include <stdio.h>
#include <stdint.h>
#include <string.h>

static int encrypt(void)
{
    uint8_t key[] = "hunter2";
    printf("encrypting with super secret key: %s\n", key);
    void * addr = memset(key, 0, 8);
    //printf("encrypting with super secret key: %s\n", key);

    if(addr) return 1;
    else return 0;
}

static void log_completion(void)
{
    /* oh no, we forgot to init the msg */
    char msg[8];

    printf("not important, just fyi: %s\n", msg);
}

int main(void)
{
    int ret = encrypt();
    printf("ret:%d\n", ret);
    /* notify that we're done */
    log_completion();
    return ret;
}

Jonny

^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2023-01-24 14:26 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-30 16:26 Avoiding stack buffer clear being optimised out Jonny Grant
2022-11-30 17:40 ` Jonathan Wakely
2022-11-30 17:41   ` Jonathan Wakely
2022-12-01 10:44     ` Jonny Grant
2022-12-01 11:31       ` Jonathan Wakely
2022-12-01 11:34         ` Jonathan Wakely
2022-12-01 11:55         ` Jonny Grant
2022-12-13 20:12   ` Jonny Grant
2022-12-13 20:31     ` Jonathan Wakely
2022-12-13 22:07       ` Jonny Grant
2022-12-13 23:13         ` Jonathan Wakely
2022-12-13 23:16           ` Jonny Grant
2023-01-24 13:52     ` Jonny Grant
2023-01-24 14:26       ` Jonathan Wakely
2022-11-30 18:47 ` David Brown

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).