Skip to content

Conversation

@AvyChanna
Copy link

if condition in the given strdup was wrong.
memcpy should be done when malloc returns non-null value

For reference, here is the strdup inplementation in glibc -

/* Duplicate S, returning an identical malloc'd string.  */
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);
}

If condition in the given strdup was wrong.
memcpy should be done when malloc returns non-null value

For reference, here is the strdup inplementation in glibc -

```
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);
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant