Skip to content

Commit

Permalink
Return null if no charset matches
Browse files Browse the repository at this point in the history
Fixes a segmentation fault if no charset matches. According to http://icu-project.org/apiref/icu4c/ucsdet_8h.html#aff2633b5055d472cff4108d94f97cf7d, ucsdet_detect() may return NULL if no charset matches.

Fixes: #15
Co-authored-by: chenzhip <letme2010@gmail.com>
  • Loading branch information
sonicdoe and letme2010 committed Nov 6, 2018
1 parent 4997c25 commit 992a110
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ console.log(charsetMatch);
// }
```

detect-character-encoding may return `null` if no charset matches.

## Supported operating systems

- macOS High Sierra
Expand Down
6 changes: 6 additions & 0 deletions icuWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ NAN_METHOD(DetectCharacterEncoding) {
return;
}

if(charsetMatch == NULL) {
info.GetReturnValue().Set(Nan::Null());
ucsdet_close(charsetDetector);
return;
}

const char *charsetName = ucsdet_getName(charsetMatch, &errorCode);

if(U_FAILURE(errorCode)) {
Expand Down
4 changes: 4 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ it('should return a confidence value', () => {
assert(typeof detectCharacterEncoding(getFixture('utf-8.txt')).confidence === 'number');
});

it('should return null if no charset matches', () => {
assert.strictEqual(detectCharacterEncoding(Buffer.from([0xAB])), null);
});

it('should throw a TypeError if argument is not a buffer', () => {
assert.throws(() => {
detectCharacterEncoding('string');
Expand Down

0 comments on commit 992a110

Please sign in to comment.