Thanks to visit codestin.com
Credit goes to github.com

Skip to content

UnmarshallBinary produces broken Message part #1

@makickerking

Description

@makickerking

Hi @crewjam,

thanks for making that code available.

I'm currently experimenting with using your library to parse syslog messages that are received via a HTTPS endpoint, i.e. the client is sending messages via a HTTP POST.

In my ServeHTTP function, I'm doing basically this:

        scanner := bufio.NewScanner(req.Body)
        for scanner.Scan() {
            b := scanner.Bytes()
            if len(b) > 0 {
                var m *rfc5424.Message = &rfc5424.Message{}
                if err = m.UnmarshalBinary(b); err != nil {
                    fmt.Printf("cannot parse msg: %v\n", err)
                } else {
                    h.msgQueue <- *m
                }
            }
        }
        req.Body.Close()

But using this, the Message field gets screwed up if more than one syslog line is sent in the request body.

Looking at func (m *Message) UnmarshalBinary(inputBuffer []byte) I fear that the assignment

      m.Message = r.Bytes()

is unsafe and should be replaced by

    m.Message = make([]byte, len(r.Bytes()))
    copy(m.Message, r.Bytes())

Best

Michael

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions