{"action":"created","comment":{"url":"https://api.github.com/repos/openssl/openssl/pulls/comments/45399170","id":45399170,"diff_hunk":"@@ -361,71 +198,136 @@ void *CRYPTO_realloc_clean(void *str, int old_len, int num, const char *file,\n     if (str == NULL)\n         return CRYPTO_malloc(num, file, line);\n \n-    if (num <= 0)\n+    if (num == 0) {\n+        CRYPTO_clear_free(str, old_len);\n         return NULL;\n+    }\n \n-    /*\n-     * We don't support shrinking the buffer. Note the memcpy that copies\n-     * |old_len| bytes to the new buffer, below.\n-     */\n-    if (num < old_len)\n-        return NULL;\n+    /* Can't shrink the buffer since memcpy below copies |old_len| bytes. */\n+    if (num < old_len) {\n+        memset((char*)str + num, 0, old_len - num);\n+        return str;\n+    }\n \n-    if (realloc_debug_func != NULL)\n-        realloc_debug_func(str, NULL, num, file, line, 0);\n-    ret = malloc_ex_func(num, file, line);\n-    if (ret) {\n-        memcpy(ret, str, old_len);\n-        OPENSSL_clear_free(str, old_len);\n+    /* Allocate new memory.  Call malloc and do a copy, so that we can\n+     * cleanse the old buffer. */\n+#ifdef CRYPTO_MDEBUG\n+    if (call_malloc_debug) {\n+        CRYPTO_mem_debug_realloc(str, NULL, num, file, line, 0);\n+        ret = malloc(num);\n+        CRYPTO_mem_debug_realloc(str, ret, num, file, line, 1);\n+    } else {\n+        ret = malloc(num);\n     }\n-#ifdef LEVITTE_DEBUG_MEM\n-    fprintf(stderr,\n-            \"LEVITTE_DEBUG_MEM:         | 0x%p -> 0x%p (%d)\\n\",\n-            str, ret, num);\n+#else\n+    (void)file;\n+    (void)line;\n+    ret = malloc(num);\n #endif\n-    if (realloc_debug_func != NULL)\n-        realloc_debug_func(str, ret, num, file, line, 1);\n \n+    if (ret)\n+        memcpy(ret, str, old_len);\n+    CRYPTO_clear_free(str, old_len);\n     return ret;\n }\n \n void CRYPTO_free(void *str)\n {\n-    if (free_debug_func != NULL)\n-        free_debug_func(str, 0);\n-#ifdef LEVITTE_DEBUG_MEM\n-    fprintf(stderr, \"LEVITTE_DEBUG_MEM:         < 0x%p\\n\", str);\n+#ifdef CRYPTO_MDEBUG\n+    if (call_malloc_debug) {\n+        CRYPTO_mem_debug_free(str, 0);\n+        free(str);\n+        CRYPTO_mem_debug_free(NULL, 1);\n+    } else {\n+        free(str);\n+    }\n+#else\n+    free(str);\n #endif\n-    free_func(str);\n-    if (free_debug_func != NULL)\n-        free_debug_func(NULL, 1);\n }\n \n void CRYPTO_clear_free(void *str, size_t num)\n {\n-    if (!str)\n+    if (str == NULL)\n         return;\n     if (num)\n         OPENSSL_cleanse(str, num);\n     CRYPTO_free(str);\n }\n \n-void *CRYPTO_remalloc(void *a, int num, const char *file, int line)\n+char *CRYPTO_strdup(const char *str, const char* file, int line)\n {\n-    OPENSSL_free(a);\n-    a = OPENSSL_malloc(num);\n-    return (a);\n+    char *ret;\n+\n+    if (str == NULL)\n+        return NULL;\n+    ret = CRYPTO_malloc(strlen(str) + 1, file, line);\n+    strcpy(ret, str);\n+    return ret;\n+}\n+\n+char *CRYPTO_strndup(const char *str, size_t s, const char* file, int line)\n+{\n+    const char *cp;\n+    size_t maxlen;\n+    char *ret;\n+\n+    if (str == NULL)\n+        return NULL;\n+\n+    /* Get length. */\n+    for (cp = str, maxlen = s; maxlen-- != 0 && *cp != '\\0'; ++cp)\n+        continue;\n+    maxlen = cp - str;\n+\n+    ret = CRYPTO_malloc(maxlen + 1, file, line);\n+    if (ret) {\n+        memcpy(ret, str, maxlen);\n+        ret[maxlen] = '\\0';\n+    }\n+    return ret;\n }\n \n-void CRYPTO_set_mem_debug_options(long bits)\n+void *OPENSSL_memdup(const void *data, size_t siz)\n {\n-    if (set_debug_options_func != NULL)\n-        set_debug_options_func(bits);\n+    void *ret;\n+\n+    if (data == NULL || siz >= INT_MAX)\n+        return NULL;\n+\n+    ret = OPENSSL_malloc(siz);\n+    if (ret == NULL) {\n+        BUFerr(BUF_F_OPENSSL_MEMDUP, ERR_R_MALLOC_FAILURE);","path":"crypto/mem.c","position":478,"original_position":478,"commit_id":"e47f65053e5454a43c4647377911c055736b683c","original_commit_id":"e47f65053e5454a43c4647377911c055736b683c","user":{"login":"kaduk","id":1894506,"avatar_url":"https://avatars.githubusercontent.com/u/1894506?v=3","gravatar_id":"","url":"https://api.github.com/users/kaduk","html_url":"https://github.com/kaduk","followers_url":"https://api.github.com/users/kaduk/followers","following_url":"https://api.github.com/users/kaduk/following{/other_user}","gists_url":"https://api.github.com/users/kaduk/gists{/gist_id}","starred_url":"https://api.github.com/users/kaduk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kaduk/subscriptions","organizations_url":"https://api.github.com/users/kaduk/orgs","repos_url":"https://api.github.com/users/kaduk/repos","events_url":"https://api.github.com/users/kaduk/events{/privacy}","received_events_url":"https://api.github.com/users/kaduk/received_events","type":"User","site_admin":false},"body":"Is BUF_F_OPENSSL_MEMDUP the best function code here?","created_at":"2015-11-19T20:58:31Z","updated_at":"2015-11-19T20:58:31Z","html_url":"https://github.com/openssl/openssl/pull/450#discussion_r45399170","pull_request_url":"https://api.github.com/repos/openssl/openssl/pulls/450","_links":{"self":{"href":"https://api.github.com/repos/openssl/openssl/pulls/comments/45399170"},"html":{"href":"https://github.com/openssl/openssl/pull/450#discussion_r45399170"},"pull_request":{"href":"https://api.github.com/repos/openssl/openssl/pulls/450"}}},"pull_request":{"url":"https://api.github.com/repos/openssl/openssl/pulls/450","id":48677188,"html_url":"https://github.com/openssl/openssl/pull/450","diff_url":"https://github.com/openssl/openssl/pull/450.diff","patch_url":"https://github.com/openssl/openssl/pull/450.patch","issue_url":"https://api.github.com/repos/openssl/openssl/issues/450","number":450,"state":"open","locked":false,"title":"mem functions cleanup","user":{"login":"richsalz","id":7332500,"avatar_url":"https://avatars.githubusercontent.com/u/7332500?v=3","gravatar_id":"","url":"https://api.github.com/users/richsalz","html_url":"https://github.com/richsalz","followers_url":"https://api.github.com/users/richsalz/followers","following_url":"https://api.github.com/users/richsalz/following{/other_user}","gists_url":"https://api.github.com/users/richsalz/gists{/gist_id}","starred_url":"https://api.github.com/users/richsalz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/richsalz/subscriptions","organizations_url":"https://api.github.com/users/richsalz/orgs","repos_url":"https://api.github.com/users/richsalz/repos","events_url":"https://api.github.com/users/richsalz/events{/privacy}","received_events_url":"https://api.github.com/users/richsalz/received_events","type":"User","site_admin":false},"body":"Only two macros CRYPTO_MDEBUG and CRYPTO_MDEBUG_ABORT to control this.\r\n        (Tested with and without it #define'd)\r\nIf CRYPTO_MDEBUG is not set, #ifdef out the whole debug machinery.\r\n        (Thanks to Jakob Bohm for the suggestion!)\r\nMake the \"change wrapper functions\" be the only paradigm.\r\nWrote documentation!\r\nFormat the 'set func' functions so their paramlists are legible.\r\nFormat some multi-line comments.\r\nRemove LEVITTE_DEBUG_MEM.\r\nRemove ability to get/set the \"set options\" functions at runtime.\r\nRemove ability to get/set the \"memory debug\" functions at runtme.\r\nRemove CRYPTO_pop_all.\r\nRemove MemCheck_* and CRYPTO_malloc_debug_init macros.\r\nRemove memleak_callback stuff.\r\nRemove {OPENSSL,CRYPTO}_remalloc.\r\nRemove BUF_strdup, BUF_strndup.\r\nAdd CRYPTO_mem_debug(int flag) function.\r\nAdd test/memleaktest.\r\nRename CRYPTO_dbg_xxx to CRYPTO_mem_debug_xxx\r\nRename CRYPTO_{push,pop}_info to CRYPTO_mem_debug_{push,pop}\r\nRename CRYPTO_malloc_init to OPENSSL_malloc_init; remove needless calls.\r\nRename *_realloc_clear to *_clear_realloc, like *_clear_free","created_at":"2015-10-25T12:07:20Z","updated_at":"2015-11-19T20:58:31Z","closed_at":null,"merged_at":null,"merge_commit_sha":"40f6377d4a6b8419cf03daa2acb8aac7b3500fb1","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/openssl/openssl/pulls/450/commits","review_comments_url":"https://api.github.com/repos/openssl/openssl/pulls/450/comments","review_comment_url":"https://api.github.com/repos/openssl/openssl/pulls/comments{/number}","comments_url":"https://api.github.com/repos/openssl/openssl/issues/450/comments","statuses_url":"https://api.github.com/repos/openssl/openssl/statuses/e47f65053e5454a43c4647377911c055736b683c","head":{"label":"richsalz:mem-cleanup","ref":"mem-cleanup","sha":"e47f65053e5454a43c4647377911c055736b683c","user":{"login":"richsalz","id":7332500,"avatar_url":"https://avatars.githubusercontent.com/u/7332500?v=3","gravatar_id":"","url":"https://api.github.com/users/richsalz","html_url":"https://github.com/richsalz","followers_url":"https://api.github.com/users/richsalz/followers","following_url":"https://api.github.com/users/richsalz/following{/other_user}","gists_url":"https://api.github.com/users/richsalz/gists{/gist_id}","starred_url":"https://api.github.com/users/richsalz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/richsalz/subscriptions","organizations_url":"https://api.github.com/users/richsalz/orgs","repos_url":"https://api.github.com/users/richsalz/repos","events_url":"https://api.github.com/users/richsalz/events{/privacy}","received_events_url":"https://api.github.com/users/richsalz/received_events","type":"User","site_admin":false},"repo":{"id":44908226,"name":"openssl","full_name":"richsalz/openssl","owner":{"login":"richsalz","id":7332500,"avatar_url":"https://avatars.githubusercontent.com/u/7332500?v=3","gravatar_id":"","url":"https://api.github.com/users/richsalz","html_url":"https://github.com/richsalz","followers_url":"https://api.github.com/users/richsalz/followers","following_url":"https://api.github.com/users/richsalz/following{/other_user}","gists_url":"https://api.github.com/users/richsalz/gists{/gist_id}","starred_url":"https://api.github.com/users/richsalz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/richsalz/subscriptions","organizations_url":"https://api.github.com/users/richsalz/orgs","repos_url":"https://api.github.com/users/richsalz/repos","events_url":"https://api.github.com/users/richsalz/events{/privacy}","received_events_url":"https://api.github.com/users/richsalz/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/richsalz/openssl","description":"","fork":true,"url":"https://api.github.com/repos/richsalz/openssl","forks_url":"https://api.github.com/repos/richsalz/openssl/forks","keys_url":"https://api.github.com/repos/richsalz/openssl/keys{/key_id}","collaborators_url":"https://api.github.com/repos/richsalz/openssl/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/richsalz/openssl/teams","hooks_url":"https://api.github.com/repos/richsalz/openssl/hooks","issue_events_url":"https://api.github.com/repos/richsalz/openssl/issues/events{/number}","events_url":"https://api.github.com/repos/richsalz/openssl/events","assignees_url":"https://api.github.com/repos/richsalz/openssl/assignees{/user}","branches_url":"https://api.github.com/repos/richsalz/openssl/branches{/branch}","tags_url":"https://api.github.com/repos/richsalz/openssl/tags","blobs_url":"https://api.github.com/repos/richsalz/openssl/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/richsalz/openssl/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/richsalz/openssl/git/refs{/sha}","trees_url":"https://api.github.com/repos/richsalz/openssl/git/trees{/sha}","statuses_url":"https://api.github.com/repos/richsalz/openssl/statuses/{sha}","languages_url":"https://api.github.com/repos/richsalz/openssl/languages","stargazers_url":"https://api.github.com/repos/richsalz/openssl/stargazers","contributors_url":"https://api.github.com/repos/richsalz/openssl/contributors","subscribers_url":"https://api.github.com/repos/richsalz/openssl/subscribers","subscription_url":"https://api.github.com/repos/richsalz/openssl/subscription","commits_url":"https://api.github.com/repos/richsalz/openssl/commits{/sha}","git_commits_url":"https://api.github.com/repos/richsalz/openssl/git/commits{/sha}","comments_url":"https://api.github.com/repos/richsalz/openssl/comments{/number}","issue_comment_url":"https://api.github.com/repos/richsalz/openssl/issues/comments{/number}","contents_url":"https://api.github.com/repos/richsalz/openssl/contents/{+path}","compare_url":"https://api.github.com/repos/richsalz/openssl/compare/{base}...{head}","merges_url":"https://api.github.com/repos/richsalz/openssl/merges","archive_url":"https://api.github.com/repos/richsalz/openssl/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/richsalz/openssl/downloads","issues_url":"https://api.github.com/repos/richsalz/openssl/issues{/number}","pulls_url":"https://api.github.com/repos/richsalz/openssl/pulls{/number}","milestones_url":"https://api.github.com/repos/richsalz/openssl/milestones{/number}","notifications_url":"https://api.github.com/repos/richsalz/openssl/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/richsalz/openssl/labels{/name}","releases_url":"https://api.github.com/repos/richsalz/openssl/releases{/id}","created_at":"2015-10-25T12:01:24Z","updated_at":"2015-10-25T12:01:45Z","pushed_at":"2015-11-19T15:02:20Z","git_url":"git://github.com/richsalz/openssl.git","ssh_url":"git@github.com:richsalz/openssl.git","clone_url":"https://github.com/richsalz/openssl.git","svn_url":"https://github.com/richsalz/openssl","homepage":null,"size":71525,"stargazers_count":0,"watchers_count":0,"language":"C","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"openssl:master","ref":"master","sha":"fa49924659f21454fba3d70b4f60ce76ee6059f8","user":{"login":"openssl","id":3279138,"avatar_url":"https://avatars.githubusercontent.com/u/3279138?v=3","gravatar_id":"","url":"https://api.github.com/users/openssl","html_url":"https://github.com/openssl","followers_url":"https://api.github.com/users/openssl/followers","following_url":"https://api.github.com/users/openssl/following{/other_user}","gists_url":"https://api.github.com/users/openssl/gists{/gist_id}","starred_url":"https://api.github.com/users/openssl/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/openssl/subscriptions","organizations_url":"https://api.github.com/users/openssl/orgs","repos_url":"https://api.github.com/users/openssl/repos","events_url":"https://api.github.com/users/openssl/events{/privacy}","received_events_url":"https://api.github.com/users/openssl/received_events","type":"Organization","site_admin":false},"repo":{"id":7634677,"name":"openssl","full_name":"openssl/openssl","owner":{"login":"openssl","id":3279138,"avatar_url":"https://avatars.githubusercontent.com/u/3279138?v=3","gravatar_id":"","url":"https://api.github.com/users/openssl","html_url":"https://github.com/openssl","followers_url":"https://api.github.com/users/openssl/followers","following_url":"https://api.github.com/users/openssl/following{/other_user}","gists_url":"https://api.github.com/users/openssl/gists{/gist_id}","starred_url":"https://api.github.com/users/openssl/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/openssl/subscriptions","organizations_url":"https://api.github.com/users/openssl/orgs","repos_url":"https://api.github.com/users/openssl/repos","events_url":"https://api.github.com/users/openssl/events{/privacy}","received_events_url":"https://api.github.com/users/openssl/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/openssl/openssl","description":"","fork":false,"url":"https://api.github.com/repos/openssl/openssl","forks_url":"https://api.github.com/repos/openssl/openssl/forks","keys_url":"https://api.github.com/repos/openssl/openssl/keys{/key_id}","collaborators_url":"https://api.github.com/repos/openssl/openssl/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/openssl/openssl/teams","hooks_url":"https://api.github.com/repos/openssl/openssl/hooks","issue_events_url":"https://api.github.com/repos/openssl/openssl/issues/events{/number}","events_url":"https://api.github.com/repos/openssl/openssl/events","assignees_url":"https://api.github.com/repos/openssl/openssl/assignees{/user}","branches_url":"https://api.github.com/repos/openssl/openssl/branches{/branch}","tags_url":"https://api.github.com/repos/openssl/openssl/tags","blobs_url":"https://api.github.com/repos/openssl/openssl/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/openssl/openssl/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/openssl/openssl/git/refs{/sha}","trees_url":"https://api.github.com/repos/openssl/openssl/git/trees{/sha}","statuses_url":"https://api.github.com/repos/openssl/openssl/statuses/{sha}","languages_url":"https://api.github.com/repos/openssl/openssl/languages","stargazers_url":"https://api.github.com/repos/openssl/openssl/stargazers","contributors_url":"https://api.github.com/repos/openssl/openssl/contributors","subscribers_url":"https://api.github.com/repos/openssl/openssl/subscribers","subscription_url":"https://api.github.com/repos/openssl/openssl/subscription","commits_url":"https://api.github.com/repos/openssl/openssl/commits{/sha}","git_commits_url":"https://api.github.com/repos/openssl/openssl/git/commits{/sha}","comments_url":"https://api.github.com/repos/openssl/openssl/comments{/number}","issue_comment_url":"https://api.github.com/repos/openssl/openssl/issues/comments{/number}","contents_url":"https://api.github.com/repos/openssl/openssl/contents/{+path}","compare_url":"https://api.github.com/repos/openssl/openssl/compare/{base}...{head}","merges_url":"https://api.github.com/repos/openssl/openssl/merges","archive_url":"https://api.github.com/repos/openssl/openssl/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/openssl/openssl/downloads","issues_url":"https://api.github.com/repos/openssl/openssl/issues{/number}","pulls_url":"https://api.github.com/repos/openssl/openssl/pulls{/number}","milestones_url":"https://api.github.com/repos/openssl/openssl/milestones{/number}","notifications_url":"https://api.github.com/repos/openssl/openssl/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/openssl/openssl/labels{/name}","releases_url":"https://api.github.com/repos/openssl/openssl/releases{/id}","created_at":"2013-01-15T22:34:48Z","updated_at":"2015-11-19T20:43:02Z","pushed_at":"2015-11-19T15:20:39Z","git_url":"git://github.com/openssl/openssl.git","ssh_url":"git@github.com:openssl/openssl.git","clone_url":"https://github.com/openssl/openssl.git","svn_url":"https://github.com/openssl/openssl","homepage":null,"size":77919,"stargazers_count":1659,"watchers_count":1659,"language":"C","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1010,"mirror_url":null,"open_issues_count":153,"forks":1010,"open_issues":153,"watchers":1659,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/openssl/openssl/pulls/450"},"html":{"href":"https://github.com/openssl/openssl/pull/450"},"issue":{"href":"https://api.github.com/repos/openssl/openssl/issues/450"},"comments":{"href":"https://api.github.com/repos/openssl/openssl/issues/450/comments"},"review_comments":{"href":"https://api.github.com/repos/openssl/openssl/pulls/450/comments"},"review_comment":{"href":"https://api.github.com/repos/openssl/openssl/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/openssl/openssl/pulls/450/commits"},"statuses":{"href":"https://api.github.com/repos/openssl/openssl/statuses/e47f65053e5454a43c4647377911c055736b683c"}}}}