Skip to content

Commit 312861a

Browse files
authored
Prevent IndexOutOfBoundsException in addNewMediaToWorkpiece by clamping insertion index (#6756)
1 parent d6893c1 commit 312861a

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

Kitodo/src/main/java/org/kitodo/production/services/file/FileService.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,10 +1252,12 @@ private void addNewMediaToWorkpiece(List<String> canonicals, Map<String, Map<Sub
12521252
break;
12531253
}
12541254
}
1255-
workpiece.getPhysicalStructure().getChildren().add(insertionPoint, physicalDivision);
1256-
actualLogicalRoot.getViews().add(insertionPoint, view);
1257-
view.getPhysicalDivision().getLogicalDivisions().add(actualLogicalRoot);
1258-
canonicals.add(insertionPoint, entry.getKey());
1255+
int safeIndexChildren = Math.min(insertionPoint, workpiece.getPhysicalStructure().getChildren().size());
1256+
int safeIndexViews = Math.min(insertionPoint, actualLogicalRoot.getViews().size());
1257+
int safeIndexCanonicals = Math.min(insertionPoint, canonicals.size());
1258+
workpiece.getPhysicalStructure().getChildren().add(safeIndexChildren, physicalDivision);
1259+
actualLogicalRoot.getViews().add(safeIndexViews, view);
1260+
canonicals.add(safeIndexCanonicals, entry.getKey());
12591261
}
12601262
}
12611263
}

0 commit comments

Comments
 (0)