Skip to content

Commit

Permalink
Add a '& 0xFF' on converting bytes to integers as bytes are signed Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
purejava committed Jan 16, 2022
1 parent f35f030 commit 79dd265
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/main/java/org/purejava/MapEntries.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
public class MapEntries {

private Logger log = LoggerFactory.getLogger(MapEntries.class);
private final Logger log = LoggerFactory.getLogger(MapEntries.class);
private Map<String, String> map = new HashMap<>();
private final byte[] EMPTY_ENTRY = new byte[]{-1, -1, -1, -1};
private final byte[] EMPTY_VALUE = new byte[]{0, 0, 0, 0};
Expand Down Expand Up @@ -154,7 +154,7 @@ public boolean setByteField(byte[] s) {
try (var b = new ByteArrayInputStream(s);
var x = new DataInputStream(b)) {

var mapSize = x.readInt();
var mapSize = x.readInt() & 0xFF;

for (var i = 0; i < mapSize; i++) {
// check if the mext part is a number or an EMPTY_ENTRY
Expand Down Expand Up @@ -200,7 +200,7 @@ public boolean setByteField(byte[] s) {
}

private int fourBytesToInt(byte[] b) {
return ((b[0] << 24) + (b[1] << 16) + (b[2] << 8) + (b[3] << 0));
return ((b[0] << 24) + (b[1] << 16) + (b[2] << 8) + (b[3] << 0)) & 0xFF;
}

@Override
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/org/purejava/KDEWalletTest2.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.*;

Expand Down Expand Up @@ -93,6 +95,15 @@ void testWriteEntry() {
result = (int) response[0];
assertEquals(result, 0);
if (result == 0) log.info("Map with with key '{}' successfully updated in folder '{}'.", key2, folder);
var longString = Stream.iterate("A", s -> s).limit(64).collect(Collectors.joining());
me.storeEntry("E", longString);
response = kwallet.send("writeEntry", "issayis", handle, folder, key2, me.getByteField(), 3, appid);
result = (int) response[0];
assertEquals(result, 0);
response = kwallet.send("readMap", "isss", handle, folder, key2, appid);
me.setByteField((byte[]) response[0]);
assertTrue(me.hasValue("E", longString));
log.info("Successfully stored and retrieved value '{}' with a length gt 63.", me.getValue("E"));
var newName = "newName";
response = kwallet.send("renameEntry", "issss", handle, folder, key1, newName, appid);
var renaming = (int) response[0];
Expand Down

0 comments on commit 79dd265

Please sign in to comment.