Skip to content

Commit f82b1dd

Browse files
zeripath6543
andauthored
Prevent adding nil label to .AddedLabels or .RemovedLabels (#14623)
* Prevent adding nil label to .AddedLabels or .RemovedLabels There are possibly a few old databases out there with malmigrated data that can cause panics with empty labels being migrated. This PR adds a few tests to prevent nil labels being added. Fix #14466 Signed-off-by: Andrew Thornton <[email protected]> * Add doctor command to remove the broken label comments Signed-off-by: Andrew Thornton <[email protected]> Co-authored-by: 6543 <[email protected]>
1 parent 30f7ddb commit f82b1dd

File tree

4 files changed

+39
-5
lines changed

4 files changed

+39
-5
lines changed

models/consistency.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,3 +305,13 @@ func CountWrongUserType() (int64, error) {
305305
func FixWrongUserType() (int64, error) {
306306
return x.Where(builder.Eq{"type": 0}.And(builder.Neq{"num_teams": 0})).Cols("type").NoAutoTime().Update(&User{Type: 1})
307307
}
308+
309+
// CountCommentTypeLabelWithEmptyLabel count label comments with empty label
310+
func CountCommentTypeLabelWithEmptyLabel() (int64, error) {
311+
return x.Where(builder.Eq{"type": CommentTypeLabel, "label_id": 0}).Count(new(Comment))
312+
}
313+
314+
// FixCommentTypeLabelWithEmptyLabel count label comments with empty label
315+
func FixCommentTypeLabelWithEmptyLabel() (int64, error) {
316+
return x.Where(builder.Eq{"type": CommentTypeLabel, "label_id": 0}).Delete(new(Comment))
317+
}

modules/doctor/dbconsistency.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,24 @@ func checkDBConsistency(logger log.Logger, autofix bool) error {
111111
}
112112
}
113113

114+
// find label comments with empty labels
115+
count, err = models.CountCommentTypeLabelWithEmptyLabel()
116+
if err != nil {
117+
logger.Critical("Error: %v whilst counting label comments with empty labels")
118+
return err
119+
}
120+
if count > 0 {
121+
if autofix {
122+
updatedCount, err := models.FixCommentTypeLabelWithEmptyLabel()
123+
if err != nil {
124+
logger.Critical("Error: %v whilst removing label comments with empty labels")
125+
return err
126+
}
127+
logger.Info("%d label comments with empty labels removed", updatedCount)
128+
} else {
129+
logger.Warn("%d label comments with empty labels", count)
130+
}
131+
}
114132
// TODO: function to recalc all counters
115133

116134
return nil

modules/templates/helper.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,10 @@ func NewFuncMap() []template.FuncMap {
371371
"RenderLabels": func(labels []*models.Label) template.HTML {
372372
html := `<span class="labels-list">`
373373
for _, label := range labels {
374+
// Protect against nil value in labels - shouldn't happen but would cause a panic if so
375+
if label == nil {
376+
continue
377+
}
374378
html += fmt.Sprintf("<div class='ui label' style='color: %s; background-color: %s'>%s</div> ",
375379
label.ForegroundColor(), label.Color, RenderEmoji(label.Name))
376380
}

routers/repo/issue.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2464,7 +2464,7 @@ func combineLabelComments(issue *models.Issue) {
24642464
if i == 0 || cur.Type != models.CommentTypeLabel ||
24652465
(prev != nil && prev.PosterID != cur.PosterID) ||
24662466
(prev != nil && cur.CreatedUnix-prev.CreatedUnix >= 60) {
2467-
if cur.Type == models.CommentTypeLabel {
2467+
if cur.Type == models.CommentTypeLabel && cur.Label != nil {
24682468
if cur.Content != "1" {
24692469
cur.RemovedLabels = append(cur.RemovedLabels, cur.Label)
24702470
} else {
@@ -2474,10 +2474,12 @@ func combineLabelComments(issue *models.Issue) {
24742474
continue
24752475
}
24762476

2477-
if cur.Content != "1" {
2478-
prev.RemovedLabels = append(prev.RemovedLabels, cur.Label)
2479-
} else {
2480-
prev.AddedLabels = append(prev.AddedLabels, cur.Label)
2477+
if cur.Label != nil {
2478+
if cur.Content != "1" {
2479+
prev.RemovedLabels = append(prev.RemovedLabels, cur.Label)
2480+
} else {
2481+
prev.AddedLabels = append(prev.AddedLabels, cur.Label)
2482+
}
24812483
}
24822484
prev.CreatedUnix = cur.CreatedUnix
24832485
issue.Comments = append(issue.Comments[:i], issue.Comments[i+1:]...)

0 commit comments

Comments
 (0)