From e5bc3b0f4d6c0407492ebdea863483925393e1bc Mon Sep 17 00:00:00 2001 From: Pierre-Marc Fournier Date: Mon, 1 Mar 2010 17:11:45 -0500 Subject: [PATCH] ust_safe_snprintf: add openbsd mbrtowc() function The glibc one calls malloc, which we are trying to avoid. --- libust/buffers.h | 1 + snprintf/Makefile.am | 1 + snprintf/mbrtowc_sb.c | 53 +++++++++++++++++++++++++++++++++++++++++++ snprintf/various.h | 2 ++ snprintf/vfprintf.c | 4 ++-- 5 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 snprintf/mbrtowc_sb.c diff --git a/libust/buffers.h b/libust/buffers.h index db4b708..a2f17a9 100644 --- a/libust/buffers.h +++ b/libust/buffers.h @@ -357,6 +357,7 @@ static __inline__ int ltt_reserve_slot(struct ust_trace *trace, */ /* FIXME: make this rellay per cpu? */ if (unlikely(LOAD_SHARED(ltt_nesting) > 4)) { + DBG("Dropping event because nesting is too deep."); local_inc(&buf->events_lost); return -EPERM; } diff --git a/snprintf/Makefile.am b/snprintf/Makefile.am index 1bd33fa..04148b7 100644 --- a/snprintf/Makefile.am +++ b/snprintf/Makefile.am @@ -8,6 +8,7 @@ libustsnprintf_la_SOURCES = \ fvwrite.c \ fvwrite.h \ local.h \ + mbrtowc_sb.c \ snprintf.c \ various.h \ vfprintf.c \ diff --git a/snprintf/mbrtowc_sb.c b/snprintf/mbrtowc_sb.c new file mode 100644 index 0000000..0abecc8 --- /dev/null +++ b/snprintf/mbrtowc_sb.c @@ -0,0 +1,53 @@ +/* $OpenBSD: mbrtowc_sb.c,v 1.4 2005/11/27 20:03:06 cloder Exp $ */ +/* $NetBSD: multibyte_sb.c,v 1.4 2003/08/07 16:43:04 agc Exp $ */ + +/* + * Copyright (c) 1991 The Regents of the University of California. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include + +/*ARGSUSED*/ +size_t +ust_safe_mbrtowc(wchar_t *pwc, const char *s, size_t n, mbstate_t *ps) +{ + + /* pwc may be NULL */ + /* s may be NULL */ + /* ps appears to be unused */ + + if (s == NULL) + return 0; + if (n == 0) + return (size_t)-1; + if (pwc) + *pwc = (wchar_t)(unsigned char)*s; + return (*s != '\0'); +} diff --git a/snprintf/various.h b/snprintf/various.h index dab78ed..2a18461 100644 --- a/snprintf/various.h +++ b/snprintf/various.h @@ -88,4 +88,6 @@ typedef struct __sFILE { extern int ust_safe_fflush(LFILE *fp); extern int ust_safe_vfprintf(LFILE *fp, const char *fmt0, va_list ap); +extern size_t ust_safe_mbrtowc(wchar_t *pwc, const char *s, size_t n, mbstate_t *ps); + #endif /* UST_SNPRINTF_VARIOUS_H */ diff --git a/snprintf/vfprintf.c b/snprintf/vfprintf.c index 4fc86bf..d4953be 100644 --- a/snprintf/vfprintf.c +++ b/snprintf/vfprintf.c @@ -399,7 +399,7 @@ int ust_safe_vfprintf(LFILE *fp, const char *fmt0, va_list ap) */ for (;;) { cp = fmt; - while ((n = mbrtowc(&wc, fmt, MB_CUR_MAX, &ps)) > 0) { + while ((n = ust_safe_mbrtowc(&wc, fmt, MB_CUR_MAX, &ps)) > 0) { fmt += n; if (wc == '%') { fmt--; @@ -1071,7 +1071,7 @@ __find_arguments(const char *fmt0, va_list ap, union arg **argtable, */ for (;;) { cp = fmt; - while ((n = mbrtowc(&wc, fmt, MB_CUR_MAX, &ps)) > 0) { + while ((n = ust_safe_mbrtowc(&wc, fmt, MB_CUR_MAX, &ps)) > 0) { fmt += n; if (wc == '%') { fmt--; -- 2.34.1