Skip to content

Commit e57ac84

Browse files
authored
Fix potential bugs (#10513)
* use e if it is an option * potential nil so check err first * check err first * m == nil already checked
1 parent 15fbf50 commit e57ac84

File tree

7 files changed

+14
-10
lines changed

7 files changed

+14
-10
lines changed

models/action.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ func (a *Action) getCommentLink(e Engine) string {
213213
return "#"
214214
}
215215
if a.Comment == nil && a.CommentID != 0 {
216-
a.Comment, _ = GetCommentByID(a.CommentID)
216+
a.Comment, _ = getCommentByID(e, a.CommentID)
217217
}
218218
if a.Comment != nil {
219219
return a.Comment.HTMLURL()

models/attachment.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ func GetAttachmentsByCommentID(commentID int64) ([]*Attachment, error) {
199199

200200
func getAttachmentsByCommentID(e Engine, commentID int64) ([]*Attachment, error) {
201201
attachments := make([]*Attachment, 0, 10)
202-
return attachments, x.Where("comment_id=?", commentID).Find(&attachments)
202+
return attachments, e.Where("comment_id=?", commentID).Find(&attachments)
203203
}
204204

205205
// getAttachmentByReleaseIDFileName return a file based on the the following infos:

models/issue_comment.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -765,8 +765,12 @@ func CreateRefComment(doer *User, repo *Repository, issue *Issue, content, commi
765765

766766
// GetCommentByID returns the comment by given ID.
767767
func GetCommentByID(id int64) (*Comment, error) {
768+
return getCommentByID(x, id)
769+
}
770+
771+
func getCommentByID(e Engine, id int64) (*Comment, error) {
768772
c := new(Comment)
769-
has, err := x.ID(id).Get(c)
773+
has, err := e.ID(id).Get(c)
770774
if err != nil {
771775
return nil, err
772776
} else if !has {

models/notification.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ func (n *Notification) loadIssue(e Engine) (err error) {
396396

397397
func (n *Notification) loadComment(e Engine) (err error) {
398398
if n.Comment == nil && n.CommentID > 0 {
399-
n.Comment, err = GetCommentByID(n.CommentID)
399+
n.Comment, err = getCommentByID(e, n.CommentID)
400400
if err != nil {
401401
return fmt.Errorf("GetCommentByID [%d] for issue ID [%d]: %v", n.CommentID, n.IssueID, err)
402402
}

modules/markup/common/linkify.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func (s *linkifyParser) Parse(parent ast.Node, block text.Reader, pc parser.Cont
108108
}
109109
at := bytes.IndexByte(line, '@')
110110
m = []int{0, stop, at, stop - 1}
111-
if m == nil || bytes.IndexByte(line[m[2]:m[3]], '.') < 0 {
111+
if bytes.IndexByte(line[m[2]:m[3]], '.') < 0 {
112112
return nil
113113
}
114114
lastChar := line[m[1]-1]

routers/repo/attachment.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,14 @@ func UploadAttachment(ctx *context.Context) {
7070
func DeleteAttachment(ctx *context.Context) {
7171
file := ctx.Query("file")
7272
attach, err := models.GetAttachmentByUUID(file)
73-
if !ctx.IsSigned || (ctx.User.ID != attach.UploaderID) {
74-
ctx.Error(403)
75-
return
76-
}
7773
if err != nil {
7874
ctx.Error(400, err.Error())
7975
return
8076
}
77+
if !ctx.IsSigned || (ctx.User.ID != attach.UploaderID) {
78+
ctx.Error(403)
79+
return
80+
}
8181
err = models.DeleteAttachment(attach, true)
8282
if err != nil {
8383
ctx.Error(500, fmt.Sprintf("DeleteAttachment: %v", err))

routers/repo/commit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,11 +244,11 @@ func Diff(ctx *context.Context) {
244244
parents := make([]string, commit.ParentCount())
245245
for i := 0; i < commit.ParentCount(); i++ {
246246
sha, err := commit.ParentID(i)
247-
parents[i] = sha.String()
248247
if err != nil {
249248
ctx.NotFound("repo.Diff", err)
250249
return
251250
}
251+
parents[i] = sha.String()
252252
}
253253

254254
ctx.Data["CommitID"] = commitID

0 commit comments

Comments
 (0)