Skip to content

Commit c317606

Browse files
committed
fix writing empty string to buffer. closes briancgh-39
1 parent 6218577 commit c317606

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

lib/writer.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,12 @@ p.addInt16 = function(num) {
3737
}
3838

3939
p.addCString = function(string) {
40-
var string = string || "";
40+
//just write a 0 for empty or null strings
41+
if(!string) {
42+
this._ensure(1);
43+
this.buffer[this.offset++] = 0;
44+
return this;
45+
}
4146
var len = Buffer.byteLength(string) + 1;
4247
this._ensure(len);
4348
this.buffer.write(string, this.offset);

test/unit/writer-tests.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ test('cString', function() {
6666
var result = subject.addCString().join();
6767
assert.equalBuffers(result, [0])
6868
})
69+
70+
test('writes two empty cstrings', function() {
71+
var subject = new Writer();
72+
var result = subject.addCString("").addCString("").join();
73+
assert.equalBuffers(result, [0, 0])
74+
})
75+
6976

7077
test('writes non-empty cstring', function() {
7178
var subject = new Writer();

0 commit comments

Comments
 (0)