Skip to content

Windows1251Codec.decode() fails with 0x00 bytes (null bytes) in input #19

@vitalii-vov

Description

@vitalii-vov

The Windows1251Codec().decode(...) method throws a runtime error (_TypeError (Null check operator used on a null value) when the input contains null bytes (0x00) — even though 0x00 is a valid byte in Windows-1251 and should decode into the Unicode null character (\u0000).

Example
The following code reproduces the problem:

final input = Uint8List.fromList([
    73, 118, 97, 110, 111, 118, 32, 73, 118, 97, 110, // "Ivanov Ivan"
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  ]);

final result = const Windows1251Codec().decode(input); // throws error
print(result);

Workaround:
Remove null characters from the string beforehand.

Uint8List safeSlice(Uint8List input) {
  final index = input.indexOf(0);
  return index == -1 ? input : input.sublist(0, index);
}

final safe = safeSlice(input);
final result = const Windows1251Codec().decode(safe);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions