Skip to content
Snippets Groups Projects
Commit ffd5385d authored by Deucе's avatar Deucе :ok_hand_tone4:
Browse files

Use strlcpy() for X?SAFECOPY() macros.

Also, for DEBUG builds, add a static assertion that
sizeof(dst) != sizeof(void*) to catch the most common breakage.
parent c35dec2f
Branches
Tags
1 merge request!455Update branch with changes from master
Pipeline #6245 failed
......@@ -23,6 +23,9 @@
#define _GEN_DEFS_H
#include "cp437defs.h"
#ifdef _DEBUG
#include <assert.h>
#endif
#include <errno.h>
/* Resolve multi-named errno constants */
......@@ -427,9 +430,24 @@ typedef struct {
#define TERMINATE(str) str[sizeof(str)-1]=0
/* This is a bound-safe version of strcpy basically - only works with fixed-length arrays */
#ifdef _DEBUG
#define SAFECOPY(dst,src) do { \
_Static_assert(sizeof(dst) != sizeof(void*), "SAFECOPY() on pointer-sized dst, use strlcpy"); \
strlcpy(dst,src,sizeof(dst)); \
} while(0)
#else
#define SAFECOPY(dst,src) strlcpy(dst,src,sizeof(dst))
#endif
/* Extra-safe SAFECOPY doesn't pass NULL-pointer to strncpy */
#ifdef _DEBUG
#define XSAFECOPY(dst,src) do { \
_Static_assert(sizeof(dst) != sizeof(void*), "SAFECOPY() on pointer-sized dst, use strlcpy"); \
strlcpy(dst,(src)==NULL?"(null)":(src),sizeof(dst)); \
} while(0)
#else
#define XSAFECOPY(dst,src) strlcpy(dst,(src)==NULL?"(null)":(src),sizeof(dst))
#endif
#define SAFECAT(dst, src) do { \
if(strlen((char*)(dst)) + strlen((char*)(src)) < sizeof(dst)) { \
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment