fix: AfterQuery using safer right trim while clearing from clause's j…#7153
Merged
jinzhu merged 1 commit intogo-gorm:masterfrom Aug 22, 2024
Merged
fix: AfterQuery using safer right trim while clearing from clause's j…#7153jinzhu merged 1 commit intogo-gorm:masterfrom
jinzhu merged 1 commit intogo-gorm:masterfrom
Conversation
…oin added as part of go-gorm#7027
Contributor
Author
|
@liov Could you please kindly review and provide further suggestions? Thanks. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary:
As a fix to #7025 , clearing of
db.Statement.Clauses["FROM"]was in introduced in #7027At https://github.com/go-gorm/gorm/blob/master/callbacks/query.go#L291 , it can cause a panic if
db.Statement.Clauses["FROM"].Joinsslice is being sliced using negative upper bound indexi.e. when
len(db.Statement.Joins) > len(v.Joins). Please observe below the case reproduced in current source codeSteps to reproduce:
In the case above, wrong column name
pets.names(instead of pets.name) is passed as query.When Count() is called after
Find()on the the samegorm.DBinstance with a wrong query, it callsAfterQuerytwice which in turn tries to trim the slice twice which now is empty after first trim. Hence instead of obtaining actual SQL error, it causes a panic due to index out of bound error.Checklist:
What did this pull request do?:
This Pull Request:
Safely trims the
db.Statement.Clauses["FROM"].JoinsUnit test for newly added functions for Safe right trimming of slice
User Case:
In one of my our projects, we are using gorm (just fantastic) as ORM support for Postgres. After upgrading gorm version from
v1.9.16 -> v1.25.11, we identified panic in API due to broken migration which changes a column name causing the query to be wrong.Of course this is a minor fix requirement but much needed to prevent API server error and handle SQL error gracefully.