Trim to 255 runes instead of bytes (#12150)

* Trim to 255 runes instead of bytes

Prevents invalid UTF-8 encoding for Description and Website. Refs #7905

* Apply suggestions from code review

Co-authored-by: zeripath <art27@cantab.net>

Co-authored-by: techknowlogick <matti@mdranta.net>
Co-authored-by: zeripath <art27@cantab.net>
Co-authored-by: Lauris BH <lauris@nix.lv>
This commit is contained in:
Jürgen Hötzel 2020-07-07 23:35:52 +02:00 committed by Matti R
parent d9c18cbba0
commit a680c911e4
No known key found for this signature in database
GPG Key ID: 9D8A57ADAA232E95
1 changed files with 5 additions and 4 deletions

View File

@ -11,6 +11,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"html/template" "html/template"
"unicode/utf8"
// Needed for jpeg support // Needed for jpeg support
_ "image/jpeg" _ "image/jpeg"
@ -1384,11 +1385,11 @@ func GetRepositoriesByForkID(forkID int64) ([]*Repository, error) {
func updateRepository(e Engine, repo *Repository, visibilityChanged bool) (err error) { func updateRepository(e Engine, repo *Repository, visibilityChanged bool) (err error) {
repo.LowerName = strings.ToLower(repo.Name) repo.LowerName = strings.ToLower(repo.Name)
if len(repo.Description) > 255 { if utf8.RuneCountInString(repo.Description) > 255 {
repo.Description = repo.Description[:255] repo.Description = string([]rune(repo.Description)[:255])
} }
if len(repo.Website) > 255 { if utf8.RuneCountInString(repo.Website) > 255 {
repo.Website = repo.Website[:255] repo.Website = string([]rune(repo.Website)[:255])
} }
if _, err = e.ID(repo.ID).AllCols().Update(repo); err != nil { if _, err = e.ID(repo.ID).AllCols().Update(repo); err != nil {