Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Index Out of Range Panic in CrLfToLfTransformer #20

Merged
merged 2 commits into from
Dec 28, 2023
Merged

Conversation

masa23
Copy link

@masa23 masa23 commented Dec 28, 2023

Hi.

I've encountered an "index out of range" panic in the CrLfToLfTransformer.Transform method of the github.com/d--j/go-milter/milterutil package. This issue arises when a \r\n sequence appears at the boundary of the src slice during transformation, leading to an attempt to access an invalid index in dst.

The current implementation seems to anticipate carriage returns (\r) and replaces them with line feeds (\n). However, when \r\n is split across the boundary of src, it results in dst[nDst-1] accessing an out-of-range index.

package main

import (
        "fmt"

        "github.com/d--j/go-milter/milterutil"
)

func main() {
        s := "aaaaaaaaaaaaaaaaaaaaaaaa\r\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\nbbbbbbb"

        a := milterutil.CrLfToLf(s)
        fmt.Println(a)
}
panic: runtime error: index out of range [-1]

goroutine 1 [running]:
github.com/d--j/go-milter/milterutil.(*CrLfToLfTransformer).Transform(0xc000006101?, {0xc00010c180?, 0x433c92?, 0x5198e8?}, {0xc00010c080?, 0xc00007cf70?, 0x440991?}, 0xa0?)
        /root/go/pkg/mod/github.com/d--j/go-milter@v0.8.3/milterutil/transformer.go:25 +0x10f
golang.org/x/text/transform.String({0x4b9288, 0xc000018090}, {0x4a3189, 0x89})
        /root/go/pkg/mod/golang.org/x/text@v0.9.0/transform/transform.go:652 +0x842
github.com/d--j/go-milter/milterutil.CrLfToLf(...)
        /root/go/pkg/mod/github.com/d--j/go-milter@v0.8.3/milterutil/transformer.go:61
main.main()
        /root/work/hoge/hoge.go:12 +0x45
exit status 2

I have revised the existing code in the CrLfToLfTransformer.Transform method to address an issue with index out of range errors. It appears that carriage returns (\r) are already being transformed into line feeds (\n) in the existing code. Therefore, I have removed the line dst[nDst-1] = lf, as it seemed unnecessary and was causing the index out of range issue.

t.prevCR = c == cr
if t.prevCR {
    c = lf
}
dst[nDst] = c

Could you please review this change and confirm if it aligns with the intended behavior and resolves the identified issue?

@coveralls
Copy link
Collaborator

coveralls commented Dec 28, 2023

Coverage Status

coverage: 85.852% (+0.06%) from 85.796%
when pulling dba02c6 on masa23:main
into 6cc737f on d--j:main.

@d--j d--j self-assigned this Dec 28, 2023
@d--j d--j self-requested a review December 28, 2023 16:18
Copy link
Owner

@d--j d--j left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right – I have no idea why I wanted to re-set the lf there since we already convert all cr to lf in this Transformer anyway.

@d--j d--j merged commit 3268197 into d--j:main Dec 28, 2023
@masa23
Copy link
Author

masa23 commented Dec 29, 2023

Thank you for merging my pull request!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants