Skip to content

Commit ef6aeb4

Browse files
committed
use safe-buffer and Buffer.from to ensure we're actually using a copy of the buffer
1 parent 540abd6 commit ef6aeb4

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

index.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
var Buffer = require('safe-buffer').Buffer
12
module.exports = function reverse (src) {
23
if (typeof src.reverse === 'function') {
3-
return src.reverse()
4+
return Buffer.from(src).reverse()
45
} else {
5-
var buffer = new Buffer(src.length)
6+
var buffer = Buffer.alloc(src.length)
67

78
for (var i = 0, j = src.length - 1; i <= j; ++i, --j) {
89
buffer[i] = src[j]

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,8 @@
3737
"devDependencies": {
3838
"standard": "*",
3939
"tape": "*"
40+
},
41+
"dependencies": {
42+
"safe-buffer": "^5.1.1"
4043
}
4144
}

test/index.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
var Buffer = require('safe-buffer').Buffer
12
var test = require('tape')
23
var reverse = require('../')
34
var reverseInplace = require('../inplace')
@@ -6,7 +7,7 @@ var fixtures = require('./fixtures')
67
test('reverse', function (t) {
78
fixtures.forEach(function (f) {
89
t.test('returns ' + f.expected + ' for ' + f.a, function (t) {
9-
var a = new Buffer(f.a, 'hex')
10+
var a = Buffer.from(f.a, 'hex')
1011
t.same(reverse(a).toString('hex'), f.expected)
1112
t.same(a.toString('hex'), f.a) // input unchanged
1213
t.end()
@@ -19,7 +20,7 @@ test('reverse', function (t) {
1920
test('reverse/inplace', function (t) {
2021
fixtures.forEach(function (f) {
2122
t.test('returns ' + f.expected + ' for ' + f.a, function (t) {
22-
var a = new Buffer(f.a, 'hex')
23+
var a = Buffer.from(f.a, 'hex')
2324
t.same(reverseInplace(a).toString('hex'), f.expected)
2425
t.same(a.toString('hex'), f.expected) // input mutated
2526
t.end()

0 commit comments

Comments
 (0)