Skip to content

Commit

Permalink
Eliminate tmp buffer from packOctetString (#1503)
Browse files Browse the repository at this point in the history
This is exactly the same change as #1429 but for packOctetString.

I have no idea how I missed this given it's the very next function.

Updates #1429
  • Loading branch information
tmthrgd authored Nov 6, 2023
1 parent 9657fe6 commit 6836ba8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
20 changes: 9 additions & 11 deletions msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,30 +501,28 @@ func packTxtString(s string, msg []byte, offset int) (int, error) {
return offset, nil
}

func packOctetString(s string, msg []byte, offset int, tmp []byte) (int, error) {
if offset >= len(msg) || len(s) > len(tmp) {
func packOctetString(s string, msg []byte, offset int) (int, error) {
if offset >= len(msg) || len(s) > 256*4+1 {
return offset, ErrBuf
}
bs := tmp[:len(s)]
copy(bs, s)
for i := 0; i < len(bs); i++ {
for i := 0; i < len(s); i++ {
if len(msg) <= offset {
return offset, ErrBuf
}
if bs[i] == '\\' {
if s[i] == '\\' {
i++
if i == len(bs) {
if i == len(s) {
break
}
// check for \DDD
if isDDD(bs[i:]) {
msg[offset] = dddToByte(bs[i:])
if isDDD(s[i:]) {
msg[offset] = dddToByte(s[i:])
i += 2
} else {
msg[offset] = bs[i]
msg[offset] = s[i]
}
} else {
msg[offset] = bs[i]
msg[offset] = s[i]
}
offset++
}
Expand Down
3 changes: 1 addition & 2 deletions msg_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,8 +461,7 @@ func unpackStringOctet(msg []byte, off int) (string, int, error) {
}

func packStringOctet(s string, msg []byte, off int) (int, error) {
txtTmp := make([]byte, 256*4+1)
off, err := packOctetString(s, msg, off, txtTmp)
off, err := packOctetString(s, msg, off)
if err != nil {
return len(msg), err
}
Expand Down

0 comments on commit 6836ba8

Please sign in to comment.