From 0598ba19d18da4568b32415e60a9629061b3c45c Mon Sep 17 00:00:00 2001 From: Mathias Buus Date: Tue, 25 May 2021 13:28:48 +0200 Subject: [PATCH] fix .. in encodingLength --- index.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 2321857..77da2ab 100644 --- a/index.js +++ b/index.js @@ -74,7 +74,7 @@ name.decode = function (buf, offset) { name.decode.bytes = 0 name.encodingLength = function (n) { - if (n === '.') return 1 + if (n === '.' || n === '..') return 1 return Buffer.byteLength(n.replace(/^\.|\.$/gm, '')) + 2 } @@ -689,7 +689,8 @@ exports.AUTHENTIC_DATA = 1 << 5 exports.CHECKING_DISABLED = 1 << 4 exports.encode = function (result, buf, offset) { - if (!buf) buf = Buffer.allocUnsafe(exports.encodingLength(result)) + var allocing = !buf + if (allocing) buf = Buffer.allocUnsafe(exports.encodingLength(result)) if (!offset) offset = 0 var oldOffset = offset @@ -709,6 +710,11 @@ exports.encode = function (result, buf, offset) { exports.encode.bytes = offset - oldOffset + // just a quick sanity check + if (allocing && exports.encode.bytes !== buf.length) { + return buf.slice(0, exports.encode.bytes) + } + return buf }