Skip to content

Commit

Permalink
Fix bad array access when an AFTER trigger raises an exception in Sav…
Browse files Browse the repository at this point in the history
…eChanges (#3049)

Fixes #3007

(cherry picked from commit 34e6242)
  • Loading branch information
roji committed Jan 5, 2024
1 parent c698389 commit bbcdde6
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/EFCore.PG/Update/Internal/NpgsqlModificationCommandBatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,16 @@ await ThrowAggregateUpdateConcurrencyExceptionAsync(reader, commandIndex, 1, 0,
}
catch (Exception ex) when (ex is not DbUpdateException and not OperationCanceledException)
{
// If the commandIndex points after the last command, attribute the error to the last command.
// This can happen when an AFTER INSERT trigger raises an exception - the insertion itself is successful, and the error comes
// afterwards, as if belonging to the next command. When there's indeed a next command, there's no way to know whether the
// error indeed belongs to it or comes from a trigger on the previous (we assume the former), but when we're the last command,
// at least avoid indexing beyond the end of the array. See #3007.
if (commandIndex == ModificationCommands.Count)
{
commandIndex--;
}

throw new DbUpdateException(
RelationalStrings.UpdateStoreException,
ex,
Expand Down

0 comments on commit bbcdde6

Please sign in to comment.