Skip to content

Commit

Permalink
attempt to deal with the portability fiasco of strnvis(3)
Browse files Browse the repository at this point in the history
  • Loading branch information
omar-polo committed Jun 6, 2024
1 parent 1bc73c3 commit 848189f
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 2 deletions.
9 changes: 9 additions & 0 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ fi
HAVE_ENDIAN_H=0
HAVE_SYS_ENDIAN_H=0
HAVE_MACHINE_ENDIAN=0
HAVE_BROKEN_STRNVIS=0

runtest endian_h ENDIAN_H || \
runtest sys_endian_h SYS_ENDIAN_H || \
Expand Down Expand Up @@ -317,6 +318,12 @@ runtest tree_h TREE_H || true
runtest vasprintf VASPRINTF -D_GNU_SOURCE || 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
COMPATS="compat/arc4random.c ${COMPATS}"
fi
Expand Down Expand Up @@ -648,6 +655,8 @@ cat <<__HEREDOC__
# define HOST_NAME_MAX 255
# endif
#endif
#define HAVE_BROKEN_STRNVIS ${HAVE_BROKEN_STRNVIS}
__HEREDOC__

echo "file config.h: written" 1>&2
Expand Down
2 changes: 1 addition & 1 deletion ge.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ log_request(struct client *c, int code, const char *meta)
strlcpy(cntmp, subj + 4, sizeof(cntmp));
if ((n = strchr(cntmp, '/')) != NULL)
*n = '\0';
strnvis(cn, cntmp, sizeof(cn), VIS_WHITE|VIS_DQ);
gmid_strnvis(cn, cntmp, sizeof(cn), VIS_WHITE|VIS_DQ);
}
}

Expand Down
2 changes: 1 addition & 1 deletion gmid.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ log_request(struct client *c, int code, const char *meta)
strlcpy(cntmp, subj + 4, sizeof(cntmp));
if ((n = strchr(cntmp, '/')) != NULL)
*n = '\0';
strnvis(cn, cntmp, sizeof(cn), VIS_WHITE|VIS_DQ);
gmid_strnvis(cn, cntmp, sizeof(cn), VIS_WHITE|VIS_DQ);
}
}

Expand Down
1 change: 1 addition & 0 deletions gmid.h
Original file line number Diff line number Diff line change
Expand Up @@ -469,5 +469,6 @@ EVP_PKEY *ssl_load_pkey(const uint8_t *, size_t);
struct vhost *new_vhost(void);
struct location *new_location(void);
struct proxy *new_proxy(void);
int gmid_strnvis(char *, const char *, size_t, int);

#endif
15 changes: 15 additions & 0 deletions have/broken-strnvis.c
Original file line number Diff line number Diff line change
@@ -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 changes: 17 additions & 0 deletions utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include <errno.h>
#include <string.h>
#include <vis.h> /* for gmid_strnvis() */

#include <openssl/ec.h>
#include <openssl/err.h>
Expand Down Expand Up @@ -496,3 +497,19 @@ new_proxy(void)
p->protocols = TLS_PROTOCOLS_DEFAULT;
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
}

0 comments on commit 848189f

Please sign in to comment.