diff options
author | wxiaoguang <wxiaoguang@gmail.com> | 2021-12-09 13:41:17 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-09 05:41:17 +0000 |
commit | c7e23401a3b7d1e38aacd857c1ec9be53a2fa63a (patch) | |
tree | c44c00a56704303753f81af6c2324c5fce3b6838 /modules/notification/action/action.go | |
parent | 183175263d9b45af6b27b677a9a0e96b45fbd4d3 (diff) | |
download | gitea-c7e23401a3b7d1e38aacd857c1ec9be53a2fa63a.tar.gz gitea-c7e23401a3b7d1e38aacd857c1ec9be53a2fa63a.zip |
Fix a panic in NotifyCreateIssueComment (caused by string truncation) (#17928)
* Fix a panic in NotifyCreateIssueComment (caused by string truncation)
* more unit tests
* refactor
* fix some edge cases
* use SplitStringAtByteN for comment content
Diffstat (limited to 'modules/notification/action/action.go')
-rw-r--r-- | modules/notification/action/action.go | 16 |
1 files changed, 9 insertions, 7 deletions
diff --git a/modules/notification/action/action.go b/modules/notification/action/action.go index 80f97ad993..81a011fbed 100644 --- a/modules/notification/action/action.go +++ b/modules/notification/action/action.go @@ -15,6 +15,7 @@ import ( "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/notification/base" "code.gitea.io/gitea/modules/repository" + "code.gitea.io/gitea/modules/util" ) type actionNotifier struct { @@ -100,14 +101,15 @@ func (a *actionNotifier) NotifyCreateIssueComment(doer *user_model.User, repo *m IsPrivate: issue.Repo.IsPrivate, } - content := "" - - if len(comment.Content) > 200 { - content = comment.Content[:strings.LastIndex(comment.Content[0:200], " ")] + "…" - } else { - content = comment.Content + truncatedContent, truncatedRight := util.SplitStringAtByteN(comment.Content, 200) + if truncatedRight != "" { + // in case the content is in a Latin family language, we remove the last broken word. + lastSpaceIdx := strings.LastIndex(truncatedContent, " ") + if lastSpaceIdx != -1 && (len(truncatedContent)-lastSpaceIdx < 15) { + truncatedContent = truncatedContent[:lastSpaceIdx] + "…" + } } - act.Content = fmt.Sprintf("%d|%s", issue.Index, content) + act.Content = fmt.Sprintf("%d|%s", issue.Index, truncatedContent) if issue.IsPull { act.OpType = models.ActionCommentPull |