Skip to content

Conversation

@buyaa-n
Copy link
Contributor

@buyaa-n buyaa-n commented Sep 19, 2024

BrotliEncoder.GetMaxCompressedLength returns invalid value for some input because managed implementation differs from native Brotli native implementation that updated many years ago

As discussed in the issue we should use the native implementation since it's the source of truth, and this size needs to remain in sync with what the encoder actually does.

Fixes #35142

public const int Quality_Default = 4;
public const int Quality_Max = 11;
public const int MaxInputSize = int.MaxValue - 515; // 515 is the max compressed extra bytes
public const int MaxInputSize = int.MaxValue - 524_166; // 524_166 is the max compressed extra bytes
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where does this number come from?

Copy link
Contributor Author

@buyaa-n buyaa-n Oct 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before int.MaxValue - 515 = 2_147_483_132 was producing max compressed length, i.e. GetMaxCompressedLength(2_147_483_132)) returned int.MaxValue. Now this produces negative number

After testing with various values now this value equals 524_166 and GetMaxCompressedLength(int.MaxValue - 524_166))producesint.MaxValue`

The comment is not that clear, but do not have a better version

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than having this hardcoded constant, though, couldn't we just call the native method and then throw ArgumentOutOfRangeException if the result was > int.MaxValue? Part of the point of this change was delegating all behavior to the native function, but we're lifting some of the behavior back up into the C# by hardcoding this constant, which in theory could be invalidated the next time we take a Brotli update.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, good point

@buyaa-n
Copy link
Contributor Author

buyaa-n commented Oct 8, 2024

/ba-g test failures in chrome-DebuggerTests are unrelated and has no log (dead-lettered), though there is many issues for same tests probably have same root cause

@buyaa-n buyaa-n merged commit 848cabd into dotnet:main Oct 8, 2024
156 of 161 checks passed
@buyaa-n buyaa-n deleted the max-encode-length branch October 8, 2024 16:54
@github-actions github-actions bot locked and limited conversation to collaborators Nov 10, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BrotliEncoder.GetMaxCompressedLength returns invalid value

2 participants