This is the mail archive of the
gdb-patches@sources.redhat.com
mailing list for the GDB project.
[COMMIT] Add xsnprintf
- From: Mark Kettenis <kettenis at gnu dot org>
- To: gdb-patches at sourceware dot org
- Date: Thu, 17 Mar 2005 23:11:28 +0100 (CET)
- Subject: [COMMIT] Add xsnprintf
Daniel and Kevin considerd this a good idea so I checked it in.
Expect a small flow of patches that actually use this.
Mark
Index: ChangeLog
from Mark Kettenis <kettenis@gnu.org>
* defs.h (xsnprintf): New prototype.
* utils.c (xsnprintf): New function.
Index: defs.h
===================================================================
RCS file: /cvs/src/src/gdb/defs.h,v
retrieving revision 1.181
diff -u -p -r1.181 defs.h
--- defs.h 14 Feb 2005 14:37:37 -0000 1.181
+++ defs.h 17 Mar 2005 22:09:33 -0000
@@ -879,6 +879,10 @@ extern void xvasprintf (char **ret, cons
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. */
Index: utils.c
===================================================================
RCS file: /cvs/src/src/gdb/utils.c,v
retrieving revision 1.158
diff -u -p -r1.158 utils.c
--- utils.c 16 Mar 2005 15:58:41 -0000 1.158
+++ utils.c 17 Mar 2005 22:09:35 -0000
@@ -1079,6 +1079,20 @@ xstrvprintf (const char *format, va_list
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. */