char *BUF_strndup(const char *buf, size_t size) { char *ret; size_t alloc_size; if (buf == NULL) { return NULL; } size = BUF_strnlen(buf, size); alloc_size = size + 1; if (alloc_size < size) { /* overflow */ OPENSSL_PUT_ERROR(BUF, BUF_strndup, ERR_R_MALLOC_FAILURE); return NULL; } ret = OPENSSL_malloc(alloc_size); if (ret == NULL) { OPENSSL_PUT_ERROR(BUF, BUF_strndup, ERR_R_MALLOC_FAILURE); return NULL; } memcpy(ret, buf, size); ret[size] = '\0'; return ret; }
char *BUF_strndup(const char *str, size_t siz) { char *ret; if (str == NULL) return NULL; siz = BUF_strnlen(str, siz); if (siz >= INT_MAX) return NULL; ret = OPENSSL_malloc(siz + 1); if (ret == NULL) { BUFerr(BUF_F_BUF_STRNDUP, ERR_R_MALLOC_FAILURE); return NULL; } memcpy(ret, str, siz); ret[siz] = '\0'; return (ret); }