Loading ...
Sorry, an error occurred while loading the content.

safe_strdup

Expand Messages
  • Jon-o Addleman
    I m digging through the gbuffy source, in the hope of fixing various things, and learning stuff in the process. With my ignorance in mind, can anyone explain
    Message 1 of 2 , Feb 8, 2003
    • 0 Attachment
      I'm digging through the gbuffy source, in the hope of fixing various
      things, and learning stuff in the process.

      With my ignorance in mind, can anyone explain the point of the
      safe_strdup function? What does it do that strdup doesn't? They seem
      nearly identical.. Seems it would simplify things to just use strdup
      instead. (similar issues come up with the geometry setting, which
      currently doesn't work at all - there's an xlib function for parsing
      that...)


      Just for reference, here's strdup from glibc-2.3.1

      char *
      __strdup (const char *s)
      {
      size_t len = strlen (s) + 1;
      void *new = malloc (len);

      if (new == NULL)
      return NULL;

      return (char *) memcpy (new, s, len);
      }



      and here's safe_strdup:

      char *safe_strdup (const char *s)
      {
      char *p;
      size_t l;

      if (!s || !*s) return 0;
      l = strlen (s) + 1;
      p = (char *)malloc (l);
      memcpy (p, s, l);
      return (p);
      }

      --
      Jon-o Addleman
      Curious about the PGP attachments in my e-mail?
      see http://www.st-andrews.ac.uk/scotmid/pgpweb/pgpintro.shtml
      to find out why they're there!
    • Brandon Long
      Well, the obvious difference from the code is that safe_strdup() returns NULL for as well as NULL strings. Its pulled from the mutt source code, I m not
      Message 2 of 2 , Feb 8, 2003
      • 0 Attachment
        Well, the obvious difference from the code is that safe_strdup()
        returns NULL for "" as well as NULL strings. Its pulled from the mutt
        source code, I'm not sure if we ever rely on that behavior in gbuffy,
        however.

        Brandon

        On 02/08/03 Jon-o Addleman uttered the following other thing:
        > I'm digging through the gbuffy source, in the hope of fixing various
        > things, and learning stuff in the process.
        >
        > With my ignorance in mind, can anyone explain the point of the
        > safe_strdup function? What does it do that strdup doesn't? They seem
        > nearly identical.. Seems it would simplify things to just use strdup
        > instead. (similar issues come up with the geometry setting, which
        > currently doesn't work at all - there's an xlib function for parsing
        > that...)
        >
        >
        > Just for reference, here's strdup from glibc-2.3.1
        >
        > char *
        > __strdup (const char *s)
        > {
        > size_t len = strlen (s) + 1;
        > void *new = malloc (len);
        >
        > if (new == NULL)
        > return NULL;
        >
        > return (char *) memcpy (new, s, len);
        > }
        >
        >
        >
        > and here's safe_strdup:
        >
        > char *safe_strdup (const char *s)
        > {
        > char *p;
        > size_t l;
        >
        > if (!s || !*s) return 0;
        > l = strlen (s) + 1;
        > p = (char *)malloc (l);
        > memcpy (p, s, l);
        > return (p);
        > }
        >
        > --
        > Jon-o Addleman
        > Curious about the PGP attachments in my e-mail?
        > see http://www.st-andrews.ac.uk/scotmid/pgpweb/pgpintro.shtml
        > to find out why they're there!



        --
        "Would a precognitive telepath know what you were thinking before
        you thought it?" -- Sherry M. Gottlieb
        http://www.fiction.net/blong/
      Your message has been successfully submitted and would be delivered to recipients shortly.