This is the mail archive of the libc-alpha@sources.redhat.com mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

[patch] manual/stdio.texi: asprintf doc addition


2000-12-23  Ben Collins  <bcollins@debian.org>

	* manual/stdio.texi: Document the return value of asprintf.
	  Also make the asprintf/snprintf examples a little better
	  (check for some error returns).


-- 
 -----------=======-=-======-=========-----------=====------------=-=------
/  Ben Collins  --  ...on that fantastic voyage...  --  Debian GNU/Linux   \
`  bcollins@debian.org  --  bcollins@openldap.org  --  bcollins@linux.com  '
 `---=========------=======-------------=-=-----=-===-======-------=--=---'
Index: stdio.texi
===================================================================
RCS file: /cvs/glibc/libc/manual/stdio.texi,v
retrieving revision 1.110
diff -u -u -r1.110 stdio.texi
--- stdio.texi	2000/10/15 01:50:08	1.110
+++ stdio.texi	2000/12/23 17:00:46
@@ -1613,6 +1613,9 @@
   int nchars;
 @end group
 @group
+  if (buffer == NULL)
+    return NULL;
+
  /* @r{Try to print in the allocated space.} */
   nchars = snprintf (buffer, size, "value of %s is %s",
                      name, value);
@@ -1622,7 +1625,8 @@
     @{
       /* @r{Reallocate buffer now that we know
          how much space is needed.} */
-      buffer = (char *) xrealloc (buffer, nchars + 1);
+      if ((buffer = (char *) xrealloc (buffer, nchars + 1)) == NULL)
+        return NULL;
 
       /* @r{Try again.} */
       snprintf (buffer, size, "value of %s is %s",
@@ -1659,6 +1663,10 @@
 address of a @code{char *} object, and @code{asprintf} stores a pointer
 to the newly allocated string at that location.
 
+The return value is the number of characters allocated for the buffer, or
+less than zero if an error occured. Usually this means that the buffer
+could not be allocated.
+
 Here is how to use @code{asprintf} to get the same result as the
 @code{snprintf} example, but more easily:
 
@@ -1669,8 +1677,10 @@
 make_message (char *name, char *value)
 @{
   char *result;
-  asprintf (&result, "value of %s is %s", name, value);
-  return result;
+  if (asprintf (&result, "value of %s is %s", name, value) < 0)
+    return NULL;
+  else
+    return result;
 @}
 @end smallexample
 @end deftypefun

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]