Skip to content

Commit bdac582

Browse files
committed
Fix bad array access when an AFTER trigger raises an exception in SaveChanges
Fixes npgsql#3007
1 parent 159be19 commit bdac582

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/EFCore.PG/Update/Internal/NpgsqlModificationCommandBatch.cs

+10
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,16 @@ await ThrowAggregateUpdateConcurrencyExceptionAsync(reader, commandIndex, 1, 0,
189189
}
190190
catch (Exception ex) when (ex is not DbUpdateException and not OperationCanceledException)
191191
{
192+
// If the commandIndex points after the last command, attribute the error to the last command.
193+
// This can happen when an AFTER INSERT trigger raises an exception - the insertion itself is successful, and the error comes
194+
// afterwards, as if belonging to the next command. When there's indeed a next command, there's no way to know whether the
195+
// error indeed belongs to it or comes from a trigger on the previous (we assume the former), but when we're the last command,
196+
// at least avoid indexing beyond the end of the array. See #3007.
197+
if (commandIndex == ModificationCommands.Count)
198+
{
199+
commandIndex--;
200+
}
201+
192202
throw new DbUpdateException(
193203
RelationalStrings.UpdateStoreException,
194204
ex,

0 commit comments

Comments
 (0)