ForeverZer0 / SharpNBT

A pure CLS-compliant C# implementation of the Named Binary Tag (NBT) format specification commonly used with Minecraft applications, allowing easy reading/writing streams and serialization to other formats.
MIT License
25 stars 9 forks source link

SNBT Parser cannot parse negative numbers in IntArrays #32

Open Fisch37 opened 2 months ago

Fisch37 commented 2 months ago

Title...

Attempting to parse an integer array fails with a SyntaxError if the array contains a negative number. This effectively means that any SNBT containing a UUID fails as those are length 4 Integer Arrays and UUID format guarantees a negative number here.

A simple check to confirm:

StringNbt.Parse("{uuid:[I; 0, 0, 0, -1]}");

throws

System.Data.SyntaxErrorException: 'Syntax error at index 19: Invalid character '-' in integer array.'
Fisch37 commented 2 months ago

Proposed Fix

The fix for this is trivial. Some light testing shows the following fix works fine:

Alter line 352 of SharpNBT.SNBT.StringNbt from

if (char.IsNumber(c) || c == ',')

to

if (char.IsNumber(c) || c == '-' || c == ',')
ForeverZer0 commented 2 months ago

Well, that is an embarrassing oversight on my part, no clue how I let that happen....

I don't have the code on my current machine to test with, but the fix would likely also require and additional check to ensure the - is the first character, and not just anywhere within the string (e.g. -123 is valid, but 1-2-3- is not). Either way, it will be a trivial fix to implement, thank you for taking the time to report.

Fisch37 commented 2 months ago

the fix would likely also require and additional check to ensure the - is the first character, and not just anywhere within the string

That's true! Good catch! Thanks to you for taking the time to develop this thing! (Like seriously, I think this is the only SNBT parser library for C#)