Skip to content

Commit

Permalink
Fix null dereference issues and improve error handling (#1220)
Browse files Browse the repository at this point in the history
* fix: remove potential null dereference

Also add new unit tests and change return value in case of errors to an empty string.

* fix: remove deprecated URL constructor

* fix: add a couple of null checks

* fix: add some null warning suppressions
  • Loading branch information
lupino3 authored Feb 8, 2025
1 parent d03a42e commit aa3ca3c
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/main/java/org/edumips64/core/IOManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ public int write(int fd, long address, int count) throws IOManagerException, Wri
}

byte[] bytes_array = new byte[count];
MemoryElement memEl = null;
StringBuffer buff = null ;
MemoryElement memEl = null;

try {
buff = new StringBuffer();
Expand All @@ -222,6 +222,7 @@ public int write(int fd, long address, int count) throws IOManagerException, Wri
address += 8;
}

@SuppressWarnings("null") // memEl will never be null if we enter the for loop.
byte rb = (byte) memEl.readByte(posInWord++);
bytes_array[i] = rb;
buff.append((char) rb);
Expand Down Expand Up @@ -251,6 +252,7 @@ public void write(int fd, String textToBeWritten) throws WriteException {
* @param address the address to write the data to
* @param count the number of bytes to read
*/
@SuppressWarnings("null") // memEl will never be null.
public int read(int fd, long address, int count) throws IOManagerException, IOException, ReadException {
if (!ins.containsKey(fd)) {
logger.info("File descriptor " + fd + " not valid for reading");
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/edumips64/core/SymbolTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,10 @@ public void setInstructionLabel(int address, String label) throws SameLabelsExce
if (temp == null) {
// TODO: throw exception?
logger.severe("No instruction at address " + address);
} else {
// TODO: attualmente l'istruzione si prende l'ultima etichetta
temp.setLabel(label);
}
// TODO: attualmente l'istruzione si prende l'ultima etichetta
temp.setLabel(label);
logger.info("Added instruction label " + label + " to address " + address);
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/org/edumips64/core/fpu/FPInstructionUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,8 @@ public String doubleDivision(String value1, String value2) throws FPInvalidOpera
/**
* Returns a string with a double value or the name of a special value
* it is recommended the use of this method only for the visualisation of the double value because it may return an alphanumeric value
*
* If the value is not a valid string encoding a 64-bit binary value, it returns an empty string.
*
* @param value the 64 bit binary string in the IEEE754 format to convert
* @return the double value or the special values "Quiet NaN","Signaling NaN", "Positive infinity", "Negative infinity","Positive zero","Negative zero"
Expand All @@ -466,6 +468,7 @@ public static String binToDouble(String value) {
new_value_d = Double.longBitsToDouble(Converter.binToLong(value, false));
} catch (IrregularStringOfBitsException ex) {
ex.printStackTrace();
return "";
}

return new_value_d.toString();
Expand All @@ -474,7 +477,7 @@ public static String binToDouble(String value) {
return new_value;
}

return null;
return "";
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/edumips64/core/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -1017,6 +1017,7 @@ private void writeDoubleInMemory(int row, int i, String line, String instr) thro
* @param numBit
* @param name type of data
*/
@SuppressWarnings("null") // tmpMem will never be null.
private void writeIntegerInMemory(int row, int i, String line, String instr, int numBit, String name) throws MemoryElementNotFoundException {
int posInWord = 0; //position of byte to write into a doubleword
String value[] = instr.split(",");
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/org/edumips64/ui/swing/GUIHelp.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

import java.awt.*;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.logging.Level;
Expand Down Expand Up @@ -60,7 +62,10 @@ public static void showHelp(Window parent, URL helpSetUrl, ConfigStore cfg) {
URL cleanUrl;

try {
cleanUrl = new URL(clean);
cleanUrl = new URI(clean).toURL();
} catch (URISyntaxException e) {
log.log(Level.SEVERE, "Could not parse Help URL_" + clean, e);
return;
} catch (MalformedURLException e) {
log.log(Level.SEVERE, "Could not parse Help URL_" + clean, e);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ private void paintRow(Graphics g, int row) {
}

// paint the dragged cell if we are dragging
if (draggedColumnIndex != -1 && draggedCellRect != null) {
if (draggedColumnIndex != -1 && draggedCellRect != null && header != null) {
draggedCellRect.x += header.getDraggedDistance();

// Fill the background
Expand Down
64 changes: 64 additions & 0 deletions src/test/java/org/edumips64/core/fpu/FPInstructionUtilsTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.edumips64.core.fpu;

import static org.junit.Assert.assertEquals;

import org.edumips64.BaseTest;
import org.edumips64.core.FCSRRegister;

Expand Down Expand Up @@ -47,4 +49,66 @@ public void TryToParseAWrongNumber() throws Exception {
fcsr.setFPExceptions(FCSRRegister.FPExceptions.INVALID_OPERATION, true);
fp.doubleToBin("--10");
}

@Test
public void testBinToDoubleZero() throws Exception {
String zeroBits = "0000000000000000000000000000000000000000000000000000000000000000";
assertEquals("Positive zero", FPInstructionUtils.binToDouble(zeroBits));
}

@Test
public void testBinToDoubleOne() throws Exception {
// IEEE 754 representation of 1.0
String oneBits = "0011111111110000000000000000000000000000000000000000000000000000";
assertEquals("1.0", FPInstructionUtils.binToDouble(oneBits));
}

@Test
public void testBinToDoubleNegative() throws Exception {
// IEEE 754 representation of -1.0
String negOneBits = "1011111111110000000000000000000000000000000000000000000000000000";
assertEquals("-1.0", FPInstructionUtils.binToDouble(negOneBits));
}
@Test
public void testBinToDoubleSmallest() throws Exception {
// Smallest positive normalized double
String smallestBits = "0000000000010000000000000000000000000000000000000000000000000000";
assertEquals("2.2250738585072014E-308", FPInstructionUtils.binToDouble(smallestBits));
}

@Test
public void testBinToDoubleLargest() throws Exception {
// Largest finite double
String largestBits = "0111111111101111111111111111111111111111111111111111111111111111";
assertEquals("1.7976931348623157E308", FPInstructionUtils.binToDouble(largestBits));
}

@Test
public void testBinToDoubleInvalidLength() throws Exception {
assertEquals("", FPInstructionUtils.binToDouble("101")); // Too short
}

@Test
public void testBinToDoubleInvalidChars() throws Exception {
String s = FPInstructionUtils.binToDouble("001234567890000000000000000000000000000000000000000000000000000000");
assertEquals("", s);
}

@Test
public void testBinToDoubleSpecialValues() throws Exception {
// Positive infinity
String posInfBits = "0111111111110000000000000000000000000000000000000000000000000000";
assertEquals("Positive infinity", FPInstructionUtils.binToDouble(posInfBits));

// Negative infinity
String negInfBits = "1111111111110000000000000000000000000000000000000000000000000000";
assertEquals("Negative infinity", FPInstructionUtils.binToDouble(negInfBits));
}

@Test
public void testBinToDoubleNaN() throws Exception {
// NaN representation
String nanBits = "0111111111111000000000000000000000000000000000000000000000000000";
assertEquals("Signaling NaN", FPInstructionUtils.binToDouble(nanBits));
}
}

0 comments on commit aa3ca3c

Please sign in to comment.