From bd84615252b8655ef7bdce9d6ac5d6e319592501 Mon Sep 17 00:00:00 2001 From: Mathias Buus Date: Tue, 25 May 2021 13:24:03 +0200 Subject: [PATCH] check for .. also in encodingLength --- index.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 9b9b4de..641ea60 100644 --- a/index.js +++ b/index.js @@ -77,7 +77,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 } @@ -1442,7 +1442,9 @@ exports.CHECKING_DISABLED = 1 << 4 exports.DNSSEC_OK = 1 << 15 exports.encode = function (result, buf, offset) { - if (!buf) buf = Buffer.allocUnsafe(exports.encodingLength(result)) + const allocing = !buf + + if (allocing) buf = Buffer.allocUnsafe(exports.encodingLength(result)) if (!offset) offset = 0 const oldOffset = offset @@ -1462,6 +1464,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 }