]> granicus.if.org Git - linux-pam/blob - libpam_misc/xstrdup.c
Relevant BUGIDs: 557322
[linux-pam] / libpam_misc / xstrdup.c
1 /* $Id$ */
2
3 #include <malloc.h>
4 #include <string.h>
5 #include <security/pam_misc.h>
6
7 /*
8  * Safe duplication of character strings. "Paranoid"; don't leave
9  * evidence of old token around for later stack analysis.
10  */
11
12 char *xstrdup(const char *x)
13 {
14      register char *new=NULL;
15
16      if (x != NULL) {
17           register int i;
18
19           for (i=0; x[i]; ++i);                       /* length of string */
20           if ((new = malloc(++i)) == NULL) {
21                i = 0;
22           } else {
23                while (i-- > 0) {
24                     new[i] = x[i];
25                }
26           }
27           x = NULL;
28      }
29
30      return new;                 /* return the duplicate or NULL on error */
31 }