Skip to content

Commit

Permalink
lib/string/strdup/: STRDUPA(): Add macro
Browse files Browse the repository at this point in the history
This is like strdupa(3), but we make sure that the argument is a string
literal, which makes sure that we won't have a stack overflow.

Signed-off-by: Alejandro Colomar <[email protected]>
  • Loading branch information
alejandro-colomar committed Jan 18, 2025
1 parent 3f34129 commit 6cc1677
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ libshadow_la_SOURCES = \
string/strcpy/strncpy.h \
string/strcpy/strtcpy.c \
string/strcpy/strtcpy.h \
string/strdup/strdupa.c \
string/strdup/strdupa.h \
string/strdup/strndupa.c \
string/strdup/strndupa.h \
string/strdup/xstrdup.c \
Expand Down
7 changes: 7 additions & 0 deletions lib/string/strdup/strdupa.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// SPDX-FileCopyrightText: 2025, Alejandro Colomar <[email protected]>
// SPDX-License-Identifier: BSD-3-Clause


#include <config.h>

#include "string/strdup/strdupa.h"
18 changes: 18 additions & 0 deletions lib/string/strdup/strdupa.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-FileCopyrightText: 2025, Alejandro Colomar <[email protected]>
// SPDX-License-Identifier: BSD-3-Clause


#ifndef SHADOW_INCLUDE_LIB_STRING_STRDUP_STRDUPA_H_
#define SHADOW_INCLUDE_LIB_STRING_STRDUP_STRDUPA_H_


#include <config.h>

#include <string.h>


// strdupa(3), but make sure that the argument is a string literal.
#define STRDUPA(s) strdupa("" s "")


#endif // include guard

0 comments on commit 6cc1677

Please sign in to comment.