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);
}
['static int test_PACKET_strndup()\n{\n    char buf[10], buf2[10];\n    char *data = NULL;\n    PACKET pkt;\n    memset(buf, \'x\', 10);\n    memset(buf2, \'y\', 10);\n    buf2[5] = \'\\0\';\n    if (       !PACKET_buf_init(&pkt, (unsigned char*)buf, 10)\n            || !PACKET_strndup(&pkt, &data)\n            ||  strlen(data) != 10\n            ||  strncmp(data, buf, 10)\n            || !PACKET_buf_init(&pkt, (unsigned char*)buf2, 10)\n            || !PACKET_strndup(&pkt, &data)\n            ||  strlen(data) != 5\n            ||  strcmp(data, buf2)) {\n        fprintf(stderr, "test_PACKET_strndup failed\\n");\n        OPENSSL_free(data);\n        return 0;\n    }\n    OPENSSL_free(data);\n    return 1;\n}', "char *BUF_strndup(const char *str, size_t siz)\n{\n    char *ret;\n    if (str == NULL)\n        return NULL;\n    siz = BUF_strnlen(str, siz);\n    if (siz >= INT_MAX)\n        return NULL;\n    ret = OPENSSL_malloc(siz + 1);\n    if (ret == NULL) {\n        BUFerr(BUF_F_BUF_STRNDUP, ERR_R_MALLOC_FAILURE);\n        return NULL;\n    }\n    memcpy(ret, str, siz);\n    ret[siz] = '\\0';\n    return (ret);\n}", "size_t BUF_strnlen(const char *str, size_t maxlen)\n{\n    const char *p;\n    for (p = str; maxlen-- != 0 && *p != '\\0'; ++p) ;\n    return p - str;\n}"]