Skip to content

Commit

Permalink
Merge pull request #378 from chhsiao1981/comments-maxlen
Browse files Browse the repository at this point in the history
defensive programming for maxlen
  • Loading branch information
chhsiao1981 authored Aug 4, 2023
2 parents 05f5a30 + b92fd33 commit e78dcb8
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions ptt/comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/Ptt-official-app/go-pttbbs/ptttype"
"github.com/Ptt-official-app/go-pttbbs/types"
"github.com/Ptt-official-app/go-pttbbs/types/ansi"
"github.com/sirupsen/logrus"
)

func FormatCommentString(user *ptttype.UserecRaw, board *ptttype.BoardHeaderRaw, commentType ptttype.CommentType, content []byte, ip *ptttype.IPv4_t, from []byte) (comment []byte, err error) {
Expand Down Expand Up @@ -45,6 +46,10 @@ func FormatCommentString(user *ptttype.UserecRaw, board *ptttype.BoardHeaderRaw,
maxlen -= 2
maxlen -= len(userBytes)
maxlen -= len(content)
if maxlen < 0 {
logrus.Errorf("FormatCommentString (old) maxlen < 0: userBytes: %v content: %v maxlen: %v", len(userBytes), len(content), maxlen)
maxlen = 0
}

prefix := ansi.ANSIColor("1;31") + "\xa1\xf7 " + ansi.ANSIColor("33")
infix := ansi.ANSIReset() + ansi.ANSIColor("33") + ":"
Expand All @@ -69,6 +74,10 @@ func FormatCommentString(user *ptttype.UserecRaw, board *ptttype.BoardHeaderRaw,

maxlen -= len(userBytes)
maxlen -= len(content)
if maxlen < 0 {
logrus.Errorf("FormatCommentString (new) maxlen < 0: userBytes: %v content: %v maxlen: %v", len(userBytes), len(content), maxlen)
maxlen = 0
}

commentTypeBytes := commentType.Bytes()
prefix := ansi.ANSIColor("33")
Expand Down

0 comments on commit e78dcb8

Please sign in to comment.