From cec61c693ddb639ba195384eb924407be42ce85b Mon Sep 17 00:00:00 2001 From: 0LEG0 Date: Tue, 5 Oct 2021 14:52:25 +0300 Subject: [PATCH 1/2] Update lib/defs.js types.cstring MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Prevents a crash when a сstring PDU filed contains a value of a different type. For example, if the address field contains a number. --- lib/defs.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/defs.js b/lib/defs.js index fda85f7..a1657ee 100644 --- a/lib/defs.js +++ b/lib/defs.js @@ -47,14 +47,14 @@ var types = { return buffer.toString('ascii', offset, offset + length); }, write: function(value, buffer, offset) { - buffer.writeUInt8(value.length, offset++); - if (typeof value == 'string') { - value = Buffer.from(value, 'ascii'); + if (!Buffer.isBuffer(value)) { + value = Buffer.from(String(value), 'ascii'); } + buffer.writeUInt8(value.length, offset++); value.copy(buffer, offset); }, size: function(value) { - return value.length + 1; + return (value.length || String(value).length) + 1; }, default: '' }, @@ -67,14 +67,14 @@ var types = { return buffer.toString('ascii', offset, offset + length); }, write: function(value, buffer, offset) { - if (typeof value == 'string') { - value = Buffer.from(value, 'ascii'); + if (!Buffer.isBuffer(value)) { + value = Buffer.from(String(value), 'ascii'); } value.copy(buffer, offset); buffer[offset + value.length] = 0; }, size: function(value) { - return value.length + 1; + return (value.length || String(value).length) + 1; }, default: '' }, From 11bd6a2c2a9c1438addda3a8a352e874acc7ab3b Mon Sep 17 00:00:00 2001 From: Anton <58663761+0LEG0@users.noreply.github.com> Date: Wed, 8 Mar 2023 17:36:44 +0300 Subject: [PATCH 2/2] Fix wrong message types when calling PDU.toBuffer method --- lib/defs.js | 2 +- test/filters.js | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/defs.js b/lib/defs.js index a1657ee..9f09232 100644 --- a/lib/defs.js +++ b/lib/defs.js @@ -505,7 +505,7 @@ filters.message = { if (Buffer.isBuffer(value)) { return value; } - var message = typeof value === 'string' ? value : value.message; + var message = typeof value === 'string' ? value : (typeof value === 'object' ? value.message : String(value)); if (typeof message === 'string' && message) { var encoded = false; if (value.udh) { diff --git a/test/filters.js b/test/filters.js index ea9f87b..beeda0a 100644 --- a/test/filters.js +++ b/test/filters.js @@ -35,12 +35,18 @@ describe('message GSM', function() { data_coding: 0 }; var value = 'This is a Test'; + var value0 = true; + var value1 = 3.145; var value2 = {message: value}; var value3 = {message: Buffer.from(value)}; var encoded = Buffer.from(value); + var encoded0 = Buffer.from(String(value0)); + var encoded1 = Buffer.from(String(value1)); describe('#encode()', function() { it('should encode a high-level formatted short message to a low-level buffer', function() { assert.deepEqual(filters.message.encode.call(pdu, value), encoded); + assert.deepEqual(filters.message.encode.call(pdu, value0), encoded0); + assert.deepEqual(filters.message.encode.call(pdu, value1), encoded1); assert.deepEqual(filters.message.encode.call(pdu, value2), encoded); assert.deepEqual(filters.message.encode.call(pdu, value3), encoded); });