Quick fix for AppleUtil#getWordValue to handle upper edge of buffer correctly. #126

This commit is contained in:
Rob Greene 2023-10-24 13:37:07 -05:00
parent ee981551b9
commit 0c285a7d46
2 changed files with 12 additions and 1 deletions

View File

@ -60,7 +60,7 @@ public class AppleUtil {
* A word is two bytes, in standard Apple LO/HI format.
*/
public static int getWordValue(byte[] buffer, int offset) {
if (offset+1 > buffer.length) {
if (offset+1 >= buffer.length) {
return 0;
}
return getWordValue(buffer[offset], buffer[offset+1]);

View File

@ -142,4 +142,15 @@ public class AppleUtilTest {
}
}
}
@Test
public void testGetWordValue_beyondEdge() {
byte[] data = new byte[] { 0x01, 0x02, 0x03, 0x04 };
assertEquals(0x0201, AppleUtil.getWordValue(data, 0));
assertEquals(0x0302, AppleUtil.getWordValue(data, 1));
assertEquals(0x0403, AppleUtil.getWordValue(data, 2));
assertEquals(0x0000, AppleUtil.getWordValue(data, 3));
assertEquals(0x0000, AppleUtil.getWordValue(data, 4));
}
}