Mark strlcat() and strlcpy() as weak functions, for libsyslog

The strlcat() and strlcpy() functions are only intended to be used
by syslog.c internally (and syslogd), when building libsyslog.

A user linking with libsyslog may have another library that provides
strlcat() or strlcpy() replacements.  We must therefore mark ours as
weak functions so they can be overridden.

This patch also add a convenience library for libsyslog, to control
the build deps. for libsyslog.  This is where external dependencies
should be addded (explicitly) when syslog.c is updated from NetBSD.
If you add new deps you likely want to mark them too as weak refs.

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
Joachim Nilsson 2019-11-04 10:59:45 +01:00
parent 8f66822b2a
commit 58da3b6bd2
5 changed files with 36 additions and 3 deletions

View File

@ -16,6 +16,10 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/ */
#include <config.h>
#include <compat.h>
#ifndef HAVE_STRLCAT
#include <sys/types.h> #include <sys/types.h>
#include <string.h> #include <string.h>
@ -27,7 +31,7 @@
* If retval >= dsize, truncation occurred. * If retval >= dsize, truncation occurred.
*/ */
size_t size_t
strlcat(char *dst, const char *src, size_t dsize) __strlcat(char *dst, const char *src, size_t dsize)
{ {
const char *odst = dst; const char *odst = dst;
const char *osrc = src; const char *osrc = src;
@ -53,3 +57,7 @@ strlcat(char *dst, const char *src, size_t dsize)
return(dlen + (src - osrc)); /* count does not include NUL */ return(dlen + (src - osrc)); /* count does not include NUL */
} }
weak_alias(__strlcat, strlcat);
#endif /* HAVE_STRLCAT */

View File

@ -16,6 +16,10 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/ */
#include <config.h>
#include <compat.h>
#ifndef HAVE_STRLCPY
#include <sys/types.h> #include <sys/types.h>
#include <string.h> #include <string.h>
@ -25,7 +29,7 @@
* Returns strlen(src); if retval >= dsize, truncation occurred. * Returns strlen(src); if retval >= dsize, truncation occurred.
*/ */
size_t size_t
strlcpy(char *dst, const char *src, size_t dsize) __strlcpy(char *dst, const char *src, size_t dsize)
{ {
const char *osrc = src; const char *osrc = src;
size_t nleft = dsize; size_t nleft = dsize;
@ -48,3 +52,7 @@ strlcpy(char *dst, const char *src, size_t dsize)
return(src - osrc - 1); /* count does not include NUL */ return(src - osrc - 1); /* count does not include NUL */
} }
weak_alias(__strlcpy, strlcpy);
#endif /* HAVE_STRLCPY */

1
src/.gitignore vendored
View File

@ -6,5 +6,6 @@ logger
syslogd syslogd
syslog_tst syslog_tst
tsyslogd tsyslogd
libcompat.la
libsyslog.la libsyslog.la
libsyslog.pc libsyslog.pc

View File

@ -19,6 +19,7 @@
bin_PROGRAMS = logger bin_PROGRAMS = logger
sbin_PROGRAMS = syslogd klogd sbin_PROGRAMS = syslogd klogd
lib_LTLIBRARIES = libsyslog.la lib_LTLIBRARIES = libsyslog.la
noinst_LTLIBRARIES = libcompat.la
AM_CFLAGS = -W -Wall -Wextra AM_CFLAGS = -W -Wall -Wextra
AM_CFLAGS += -Wno-unused-result -Wno-unused-parameter -fno-strict-aliasing AM_CFLAGS += -Wno-unused-result -Wno-unused-parameter -fno-strict-aliasing
@ -39,11 +40,15 @@ logger_CPPFLAGS = $(AM_CPPFLAGS) -D_XOPEN_SOURCE=600
logger_LDADD = $(LIBS) $(LIBOBJS) logger_LDADD = $(LIBS) $(LIBOBJS)
logger_LDADD += libsyslog.la logger_LDADD += libsyslog.la
# Convenience library for libsyslog instead of linking with $(LTLIBOBJS),
# which would pull in pidfile() and other (strong) symbols as well.
libcompat_la_SOURCES = ../lib/strlcpy.c ../lib/strlcat.c
pkgconfigdir = $(libdir)/pkgconfig pkgconfigdir = $(libdir)/pkgconfig
pkgincludedir = $(includedir)/syslog pkgincludedir = $(includedir)/syslog
pkgconfig_DATA = libsyslog.pc pkgconfig_DATA = libsyslog.pc
pkginclude_HEADERS = syslog.h pkginclude_HEADERS = syslog.h
libsyslog_la_SOURCES = syslog.c syslog.h libsyslog_la_SOURCES = syslog.c syslog.h
libsyslog_la_CPPFLAGS = $(AM_CPPFLAGS) -D_XOPEN_SOURCE=600 libsyslog_la_CPPFLAGS = $(AM_CPPFLAGS) -D_XOPEN_SOURCE=600
libsyslog_la_LIBADD = $(LTLIBOBJS)
libsyslog_la_LDFLAGS = $(AM_LDFLAGS) -version-info 0:0:0 libsyslog_la_LDFLAGS = $(AM_LDFLAGS) -version-info 0:0:0
libsyslog_la_LIBADD = libcompat.la

View File

@ -46,6 +46,17 @@
*/ */
#define __UNCONST(a) ((void *)(unsigned long)(const void *)(a)) #define __UNCONST(a) ((void *)(unsigned long)(const void *)(a))
/*
* The functions strlcat() and strlcpy() may be available in either
* the C library or another library the user links their application
* with. So we must declare them as "weak" symbols in libsyslog.
*/
#ifndef weak_alias
# define weak_alias(name, aliasname) _weak_alias (name, aliasname)
# define _weak_alias(name, aliasname) \
extern __typeof (name) aliasname __attribute__ ((weak, alias (#name)))
#endif
/* Pthread wrapper for BSD LWP mutexes */ /* Pthread wrapper for BSD LWP mutexes */
typedef pthread_mutex_t mutex_t; typedef pthread_mutex_t mutex_t;