From: Mark Kettenis Date: Thu, 17 Mar 2005 22:11:09 +0000 (+0000) Subject: * defs.h (xsnprintf): New prototype. X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=bde2058dde90fc3c353df5aace64005d399ee24a;p=binutils-gdb.git * defs.h (xsnprintf): New prototype. * utils.c (xsnprintf): New function. --- diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 3c62956c5fe..d4b85eaa639 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,5 +1,8 @@ 2005-03-17 Mark Kettenis + * defs.h (xsnprintf): New prototype. + * utils.c (xsnprintf): New function. + * corelow.c (get_core_register_section): Replace usage of sprintf and strcpy with xstrprintf and xstrdup. diff --git a/gdb/defs.h b/gdb/defs.h index c14321a42a9..fa012a4d28b 100644 --- a/gdb/defs.h +++ b/gdb/defs.h @@ -879,6 +879,10 @@ extern void xvasprintf (char **ret, const char *format, va_list ap); extern char *xstrprintf (const char *format, ...) ATTR_FORMAT (printf, 1, 2); extern char *xstrvprintf (const char *format, va_list ap); +/* Like snprintf, but throw an error if the output buffer is too small. */ +extern int xsnprintf (char *str, size_t size, const char *format, ...) + ATTR_FORMAT (printf, 3, 4); + extern int parse_escape (char **); /* Message to be printed before the error message, when an error occurs. */ diff --git a/gdb/utils.c b/gdb/utils.c index 7ee20617e38..0c451088890 100644 --- a/gdb/utils.c +++ b/gdb/utils.c @@ -1079,6 +1079,20 @@ xstrvprintf (const char *format, va_list ap) return ret; } +int +xsnprintf (char *str, size_t size, const char *format, ...) +{ + va_list args; + int ret; + + va_start (args, format); + ret = vsnprintf (str, size, format, args); + gdb_assert (ret < size); + va_end (args); + + return ret; +} + /* My replacement for the read system call. Used like `read' but keeps going if `read' returns too soon. */