GeezerVison is showing github.com — src: Malloc/Calloc size 0 returns non-null pointer · nodejs/node@62e83b3
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/util-inl.h +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -234,11 +234,13 @@ void* Realloc(void* pointer, size_t size) { | 234 | 234 |
| 235 | 235 | // As per spec realloc behaves like malloc if passed nullptr. | 236 | 236 | void* Malloc(size_t size) { | | 237 | + if (size == 0) size = 1; | 237 | 238 | return Realloc(nullptr, size); | 238 | 239 | } | 239 | 240 |
| 240 | 241 | void* Calloc(size_t n, size_t size) { | 241 | | - if ((n == 0) || (size == 0)) return nullptr; | | 242 | + if (n == 0) n = 1; | | 243 | + if (size == 0) size = 1; | 242 | 244 | CHECK_GE(n * size, n); // Overflow guard. | 243 | 245 | return calloc(n, size); | 244 | 246 | } | |
Collapse file Copy file name to clipboardExpand all lines: test/cctest/util.cc +14Lines changed: 14 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -74,3 +74,17 @@ TEST(UtilTest, ToLower) { | 74 | 74 | EXPECT_EQ('a', ToLower('a')); | 75 | 75 | EXPECT_EQ('a', ToLower('A')); | 76 | 76 | } | | 77 | + | | 78 | +TEST(UtilTest, Malloc) { | | 79 | + using node::Malloc; | | 80 | + EXPECT_NE(nullptr, Malloc(0)); | | 81 | + EXPECT_NE(nullptr, Malloc(1)); | | 82 | +} | | 83 | + | | 84 | +TEST(UtilTest, Calloc) { | | 85 | + using node::Calloc; | | 86 | + EXPECT_NE(nullptr, Calloc(0, 0)); | | 87 | + EXPECT_NE(nullptr, Calloc(1, 0)); | | 88 | + EXPECT_NE(nullptr, Calloc(0, 1)); | | 89 | + EXPECT_NE(nullptr, Calloc(1, 1)); | | 90 | +} |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-crypto-pbkdf2.js +8Lines changed: 8 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -79,3 +79,11 @@ assert.throws(function() { | 79 | 79 | assert.throws(function() { | 80 | 80 | crypto.pbkdf2('password', 'salt', 1, -1, common.fail); | 81 | 81 | }, /Bad key length/); | | 82 | + | | 83 | +// Should not get FATAL ERROR with empty password and salt | | 84 | +// https://github.com/nodejs/node/issues/8571 | | 85 | +assert.doesNotThrow(() => { | | 86 | + crypto.pbkdf2('', '', 1, 32, 'sha256', common.mustCall((e) => { | | 87 | + assert.ifError(e); | | 88 | + })); | | 89 | +}); |
|