# HG changeset patch # User Matti Hamalainen # Date 1230127433 -7200 # Node ID e36df57c5b0fa051eabc3b944d8fbf5da6a77c25 # Parent 745f670068dcbaecbdfad9f19475f07d878b5751 Use th_strdup() again. diff -r 745f670068dc -r e36df57c5b0f libnnchat.c --- a/libnnchat.c Thu Dec 18 14:34:44 2008 +0200 +++ b/libnnchat.c Wed Dec 24 16:03:53 2008 +0200 @@ -392,12 +392,12 @@ void addRingBuf(ringbuf_t *buf, const char *str) { if (buf->n < buf->size) { - buf->data[buf->n] = strdup(str); + buf->data[buf->n] = th_strdup(str); buf->n++; } else { th_free(buf->data[0]); memmove(&(buf->data[0]), &(buf->data[1]), buf->size - 1); - buf->data[buf->size - 1] = strdup(str); + buf->data[buf->size - 1] = th_strdup(str); } } diff -r 745f670068dc -r e36df57c5b0f nnchat.c --- a/nnchat.c Thu Dec 18 14:34:44 2008 +0200 +++ b/nnchat.c Wed Dec 24 16:03:53 2008 +0200 @@ -534,7 +534,7 @@ return 0; } else if (!strncmp(buf, "/to ", 4)) { th_free(setTarget); - setTarget = strdup(buf + 4); + setTarget = th_strdup(buf + 4); printMsg("Set prv target to '%s'\n", setTarget); return 0; } else if (!strncmp(buf, "/who", 4)) { diff -r 745f670068dc -r e36df57c5b0f th_string.c --- a/th_string.c Thu Dec 18 14:34:44 2008 +0200 +++ b/th_string.c Wed Dec 24 16:03:53 2008 +0200 @@ -13,6 +13,23 @@ #define LPREV (pNode->pPrev) #define LNEXT (pNode->pNext) + +/* strdup with a NULL check + */ +char *th_strdup(const char *s) +{ + char *res; + if (s == NULL) + return NULL; + + if ((res = th_malloc(strlen(s) + 1)) == NULL) + return NULL; + + strcpy(res, s); + return res; +} + + /* Allocate memory for a string with given length */ char *th_stralloc(const size_t l) diff -r 745f670068dc -r e36df57c5b0f th_string.h --- a/th_string.h Thu Dec 18 14:34:44 2008 +0200 +++ b/th_string.h Wed Dec 24 16:03:53 2008 +0200 @@ -48,8 +48,9 @@ int th_strncasecmp(char *, char *, size_t); void th_strip_ctrlchars(char *); -int th_pstrcpy(char **, char *); -int th_pstrcat(char **, char *); +char *th_strdup(const char *); +int th_pstrcpy(char **, char *); +int th_pstrcat(char **, char *); char *th_findnext(char *, size_t *); char *th_findsep(char *, size_t *, char);