Skip to content

Commit 0f33534

Browse files
committed
string: Add memory barrier to memset_s
The memset_s spec talks about how it's different from memset and has stronger guarantees. I can't see how that actually requires the writes if the memory is released afterwards (either through a call to free or a function return), but it seems like the spec might encourage people to think that it does. Adding a memory barrier ensures the compiler will not elide the actual memory operations even when built with LTO. Signed-off-by: Keith Packard <[email protected]>
1 parent fd4324c commit 0f33534

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

newlib/libc/string/memset_s.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,15 @@ memset_s(void *s, rsize_t smax, int c, rsize_t n)
6464

6565
// Normal return path
6666
(void)memset(s, c, n);
67+
/* Compiler barrier. */
68+
__asm__ __volatile__ ("" ::"r"(s): "memory");
6769
return 0;
6870

6971
handle_error:
7072
if (s != NULL) {
7173
(void)memset(s, c, smax);
74+
/* Compiler barrier. */
75+
__asm__ __volatile__ ("" ::"r"(s): "memory");
7276
}
7377

7478
if (__cur_handler != NULL) {

0 commit comments

Comments
 (0)