boxable icon indicating copy to clipboard operation
boxable copied to clipboard

Index out of bounds on DataTable if CSV is missing columns

Open agentgt opened this issue 1 year ago • 2 comments

java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
	at org.apache.commons.csv.CSVRecord.get(CSVRecord.java:87)
	at be.quodlibet.boxable.datatable.DataTable.addCsvToTable(DataTable.java:290)
	at be.quodlibet.boxable.datatable.DataTable.addListToTable(DataTable.java:204)

See

https://github.com/dhorions/boxable/blob/fd0012fa24e7b04320cf537e3163e5ee0c55700c/src/main/java/be/quodlibet/boxable/datatable/DataTable.java#L249

if (line.size() >= i) {
    cellValue = line.get(i)

CSVRecord is zero based indexing so it fails.

Should be line.size() > i

agentgt avatar Jul 06 '22 17:07 agentgt

How this came about is because there is some bad attempt of escaping:

	public void addListToTable(List<List> data, Boolean hasHeader) throws IOException {
		char separator = ';';
		if (data == null || data.isEmpty()) {
			return;
		}
		String output = "";
		// Convert Map of arbitrary objects to a csv String
		for (List inputList : data) {
			for (Object v : inputList) {
				String value = v.toString();
				if (value.contains("" + separator)) {
					// surround value with quotes if it contains the escape
					// character
					value = "\"" + value + "\"";
				}
				output += value + separator;
			}
			// remove the last separator
			output = removeLastChar(output);
			output += "\n";
		}
		addCsvToTable(output, hasHeader, separator);
	}

Consequently our current workaround is:

	private static final char BOXABLE_SEPARATOR = ';';
	private static final CSVFormat BOXABLE_FORMATTER 
		= CSVFormat.Builder.create(CSVFormat.EXCEL).setDelimiter(BOXABLE_SEPARATOR).build();
	
	
	static void addListToTable(DataTable t, List<List<?>> data, Boolean hasHeader) throws IOException {
		if (data.isEmpty()) {
			return;
		}
		StringBuilder b = new StringBuilder();
		for (List<?> inputList : data) {
			String line = BOXABLE_FORMATTER.format(inputList.toArray());
			b.append(line).append("\n");
		}
		String output = b.toString();
		t.addCsvToTable(output, hasHeader, BOXABLE_SEPARATOR);
	}

agentgt avatar Jul 06 '22 18:07 agentgt

@bhupal4all As soon as we can get 1.7.0 released we can start working on bug fixes. Waiting for @dhorions to review.

johnmanko avatar Jul 12 '22 13:07 johnmanko