This is the mail archive of the gdb-patches@sources.redhat.com mailing list for the GDB project.


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

Re: [rfc] asprintf() -> xasprintf()


Good catch.  No more memory would cause an attempt to free NULL.
(Have you ever considered a FREE macro that tests for NULL pointers?
 Something like that is used in gdbtk).
#define FREEIF(x) if (x != NULL) free((char *) (x))


Maybe you should have "memory exhausted" or something similar in the message text.
The only reason malloc can fail is ENOMEM.
Something like "vasprintf: not enough memory".


The second test doesn't seem necessary, as the worse that will happen is the
message not being printed.
And I am not sure errno is set in this case, so it may be misleading.
Furthermore, the only return code less than zero was -1 and will not exist
in libc starting at 2.1 (accordingly to the man page). 

I would go ahead without the second test and with some reference to lack
of memory in the first message text.

Fernando



Andrew Cagney wrote:
> 
> Hello,
> 
> The attatched replaces all uses of asprintf()/vasprintf() with calls to
> xasprintf() and xvasprintf().  The wrapper functions abort if the return
> value is wrong.  Testing of the patch is continuing.
> 
> I'll also need to update TODO and CONTRIBUTE to reflect this.
> 
>         Andrew

> ChangeLog:
> Mon Nov 13 12:44:06 2000  Andrew Cagney  <cagney@b1.cygnus.com>
> 
>         * utils.c (xvasprintf, xasprintf): New functions.
>         * defs.h (xvasprintf, xasprintf): Add declarations.
> 
>         * remote.c (add_packet_config_cmd): Use function xasprintf instead
>         of asprintf.
>         * utils.c (vfprintf_maybe_filtered, vfprintf_unfiltered): Use
>         function xvasprintf instead of vasprintf.
> 
> mi/ChangeLog:
> Mon Nov 13 12:48:47 2000  Andrew Cagney  <cagney@b1.cygnus.com>
> 
>         * mi-main.c: Replace asprintf with xasprintf.
>         * mi-cmd-var.c (mi_cmd_var_create): Ditto.
> 

> Index: utils.c
> ===================================================================
> RCS file: /cvs/src/src/gdb/utils.c,v
> retrieving revision 1.21
> diff -p -r1.21 utils.c
> *** utils.c     2000/11/04 00:46:46     1.21
> --- utils.c     2000/11/13 05:31:02
> *************** xrealloc (PTR ptr, size_t size)
> *** 1079,1084 ****
> --- 1079,1109 ----
>   }
> 
> 
> + /* Like asprintf/vasprintf but get an internal_error if the call
> +    fails. */
> +
> + void
> + xasprintf (char **ret, const char *format, ...)
> + {
> +   va_list args;
> +   va_start (args, format);
> +   xvasprintf (ret, format, args);
> +   va_end (args);
> + }
> +
> + void
> + xvasprintf (char **ret, const char *format, va_list ap)
> + {
> +   int status = vasprintf (ret, format, ap);
> +   if ((*ret) == NULL)
> +     internal_error ("%s:%d: vasprintf returned NULL buffer (errno %d)",
> +                   __FILE__, __LINE__, errno);
> +   if (status < 0)
> +     internal_error ("%s:%d: vasprintf call failed (errno %d)",
> +                   __FILE__, __LINE__, errno);
> + }
> +
> +
>   /* My replacement for the read system call.
>      Used like `read' but keeps going if `read' returns too soon.  */
> 


-- 
Fernando Nasser
Red Hat - Toronto                       E-Mail:  fnasser@redhat.com
2323 Yonge Street, Suite #300
Toronto, Ontario   M4P 2C9

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