fix(core): BytesUtils.twoBytesToInt 屏蔽符号位#535
Merged
Conversation
twoBytesToInt OR'd each byte without & 0xFF, so any low byte >= 128 (e.g. value 255) sign-extended and decoded to a negative int; bytesToInt just below already masks correctly. The method had no production callers, so the new round-trip BytesUtilsTest (primitive codecs, subBytes/concat, bit ops) guards the fix.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题
BytesUtils.twoBytesToInt在合并两个字节时直接|=,未对低字节做& 0xFF,导致任何 >= 128 的低字节(如数值 255)被符号扩展,解码出负数。紧邻其下的bytesToInt已正确做了掩码。改动
对参与拼接的字节加
& 0xFF掩码。该方法当前无生产调用方,新增的往返(round-trip)测试用于守护此修复。验证
新增
BytesUtilsTest(基础类型编解码、subBytes/concat、位运算往返)共 10 例通过;spotless 通过。说明
基于
master的独立分支,独立测试(不依赖BenchmarkTestBase)。