File:  [DVB] / dietlibc / libugly / strndup.c
Revision 1.5: download - view: text, annotated - select for diffs
Tue Aug 15 21:34:40 2006 UTC (17 years, 9 months ago) by leitner
Branches: MAIN
CVS tags: HEAD
make strndup also secure if WANT_MALLOC_ZERO is defined

#define _GNU_SOURCE
/* *puke* */
#include <string.h>
#include <stdlib.h>
#include "dietfeatures.h"

char *strndup(const char *s,size_t n) {
  /* This relies on the fact that our malloc(0) returns NULL.
   * Otherwise this would be an exploitable integer overflow! */
#ifdef WANT_MALLOC_ZERO
  char *tmp=!(n+1)?0:(char *)malloc(n+1);
#else
  char *tmp=(char *)malloc(n+1);
#endif
  if (!tmp) return 0;
  strncpy(tmp,s,n);
  tmp[n]=0;
  return tmp;
}

LinuxTV legacy CVS <linuxtv.org/cvs>