Closed Fr0sT-Brutal closed 7 years ago
Regarding test fail: terminator is written over instead of replace. But that's how writeStringNT works. So now they both work in the same way.
Okay so the issue with the test seems to be something else that also affects writeStringNT when using an offset.
var smb = new smbuf(1);
smb.writeString('hello');
smb.writeStringNT('aaa', 0);
SmartBuffer {
buff: <Buffer 61 61 61 68 65 6c 6c 6f 00 00 00 00 00>,
length: 9,
_readOffset: 0,
_writeOffset: 9 }
The problem is with:
this.writeString(value, offset, encoding);
this.writeUInt8(0x00);
When writing with an offset, the writeOffset is not updated to after where the string was inserted (which is correct). However, the writeUInt8 is writing at the current writeOffset and not right after where the string was written.
So a solution would be:
this.writeString(value, offset, encoding);
this.writeUInt8(0x00, (typeof offset === 'number' ? offset + value.length : this._writeOffset));
SmartBuffer {
buff: <Buffer 61 61 61 00 68 65 6c 6c 6f a0 7e 80 03>,
length: 9,
_readOffset: 0,
_writeOffset: 9 }
And the same with writeBufferNT.
I'll work on this later tonight when I get home.
Fixed on #5
writeBufferNT
doesn't allocate a space for null byte; moreover it duplicates code. Code to check the bug: