attempt to deal with the portability fiasco of strnvis(3)

This commit is contained in:
Omar Polo 2024-06-06 13:43:12 +00:00
parent 1bc73c3bcb
commit 848189f10a
6 changed files with 44 additions and 2 deletions

9
configure vendored
View File

@ -275,6 +275,7 @@ fi
HAVE_ENDIAN_H=0 HAVE_ENDIAN_H=0
HAVE_SYS_ENDIAN_H=0 HAVE_SYS_ENDIAN_H=0
HAVE_MACHINE_ENDIAN=0 HAVE_MACHINE_ENDIAN=0
HAVE_BROKEN_STRNVIS=0
runtest endian_h ENDIAN_H || \ runtest endian_h ENDIAN_H || \
runtest sys_endian_h SYS_ENDIAN_H || \ runtest sys_endian_h SYS_ENDIAN_H || \
@ -317,6 +318,12 @@ runtest tree_h TREE_H || true
runtest vasprintf VASPRINTF -D_GNU_SOURCE || true runtest vasprintf VASPRINTF -D_GNU_SOURCE || true
runtest vis VIS -DLIBBSD_OPENBSD_VIS || true runtest vis VIS -DLIBBSD_OPENBSD_VIS || true
if [ ${HAVE_VIS} -eq 1 ]; then
if singletest broken-strnvis BROKEN_STRNVIS "-Wall -Werror"; then
HAVE_BROKEN_STRNVIS=1
fi
fi
if [ ${HAVE_ARC4RANDOM} -eq 1 -a ${HAVE_ARC4RANDOM_BUF} -eq 0 ]; then if [ ${HAVE_ARC4RANDOM} -eq 1 -a ${HAVE_ARC4RANDOM_BUF} -eq 0 ]; then
COMPATS="compat/arc4random.c ${COMPATS}" COMPATS="compat/arc4random.c ${COMPATS}"
fi fi
@ -648,6 +655,8 @@ cat <<__HEREDOC__
# define HOST_NAME_MAX 255 # define HOST_NAME_MAX 255
# endif # endif
#endif #endif
#define HAVE_BROKEN_STRNVIS ${HAVE_BROKEN_STRNVIS}
__HEREDOC__ __HEREDOC__
echo "file config.h: written" 1>&2 echo "file config.h: written" 1>&2

2
ge.c
View File

@ -83,7 +83,7 @@ log_request(struct client *c, int code, const char *meta)
strlcpy(cntmp, subj + 4, sizeof(cntmp)); strlcpy(cntmp, subj + 4, sizeof(cntmp));
if ((n = strchr(cntmp, '/')) != NULL) if ((n = strchr(cntmp, '/')) != NULL)
*n = '\0'; *n = '\0';
strnvis(cn, cntmp, sizeof(cn), VIS_WHITE|VIS_DQ); gmid_strnvis(cn, cntmp, sizeof(cn), VIS_WHITE|VIS_DQ);
} }
} }

2
gmid.c
View File

@ -140,7 +140,7 @@ log_request(struct client *c, int code, const char *meta)
strlcpy(cntmp, subj + 4, sizeof(cntmp)); strlcpy(cntmp, subj + 4, sizeof(cntmp));
if ((n = strchr(cntmp, '/')) != NULL) if ((n = strchr(cntmp, '/')) != NULL)
*n = '\0'; *n = '\0';
strnvis(cn, cntmp, sizeof(cn), VIS_WHITE|VIS_DQ); gmid_strnvis(cn, cntmp, sizeof(cn), VIS_WHITE|VIS_DQ);
} }
} }

1
gmid.h
View File

@ -469,5 +469,6 @@ EVP_PKEY *ssl_load_pkey(const uint8_t *, size_t);
struct vhost *new_vhost(void); struct vhost *new_vhost(void);
struct location *new_location(void); struct location *new_location(void);
struct proxy *new_proxy(void); struct proxy *new_proxy(void);
int gmid_strnvis(char *, const char *, size_t, int);
#endif #endif

15
have/broken-strnvis.c Normal file
View File

@ -0,0 +1,15 @@
/*
* public domain
*/
#include <stdio.h>
#include <stdlib.h>
#include <vis.h>
int
main(void)
{
char buf[128];
return strnvis(buf, sizeof(buf), "Hello, world!\n", 0);
}

17
utils.c
View File

@ -22,6 +22,7 @@
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
#include <vis.h> /* for gmid_strnvis() */
#include <openssl/ec.h> #include <openssl/ec.h>
#include <openssl/err.h> #include <openssl/err.h>
@ -496,3 +497,19 @@ new_proxy(void)
p->protocols = TLS_PROTOCOLS_DEFAULT; p->protocols = TLS_PROTOCOLS_DEFAULT;
return p; return p;
} }
/*
* I can't rant enough about this situation. As far as I've understood,
* OpenBSD introduced strnvis(3) with a signature, then NetBSD did it but
* with a incompatible signature. FreeBSD followed NetBSD. libbsd followed
* OpenBSD but now is thinking to switch. WTF?
*/
int
gmid_strnvis(char *dst, const char *src, size_t destsize, int flag)
{
#if HAVE_BROKEN_STRNVIS
return strnvis(dst, destsize, src, flag);
#else
return strnvis(dst, src, destsize, flag);
#endif
}