Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bad array access when an AFTER trigger raises an exception in SaveChanges #3049

Merged
merged 1 commit into from
Jan 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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