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

Trim null characters in Value data format #2049

Merged
merged 4 commits into from
Nov 16, 2016
Merged

Conversation

zan-xhipe
Copy link
Contributor

@zan-xhipe zan-xhipe commented Nov 16, 2016

Required for all PRs:

  • CHANGELOG.md updated (we recommend not updating this until the PR has been approved by a maintainer)
  • Sign CLA (if not already signed)
  • README.md updated (if adding a new plugin)

Some producers (such as the paho embedded c mqtt client) add a null
character "\x00" to the end of a message. The Value parser fails on
any message from such a producer.

Some producers (such as the paho embedded c mqtt client) add a null
character "\x00" to the end of a message.  The Value parser would fail on
any message from such a producer.
@@ -23,6 +23,7 @@ func (v *ValueParser) Parse(buf []byte) ([]telegraf.Metric, error) {
if v.DataType == "string" {
vStr = strings.TrimSpace(string(buf))
} else {
buf = bytes.Trim(buf, "\x00")
Copy link
Contributor

Choose a reason for hiding this comment

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

does bytes.TrimSpace trim null characters? maybe we should just do buf = bytes.TrimSpace(buf) as the first step in this function, then we can also remove the strings.TrimSpace if the data type is a string as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

bytes.TrimSpace does not trim null characters

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Though I think maybe we should trim whitespace from none string values as well, In which case we could call bytes.TrimSpace and bytes.Trim(buf, "\x00") first thing in the function

Copy link
Contributor

Choose a reason for hiding this comment

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

yes, please do that, in that case the first step of the function should be to do buf = bytes.TrimSpace(bytes.Trim(buf, "\x00"))

@sparrc sparrc added the bug unexpected problem or unintended behavior label Nov 16, 2016
if v.DataType == "string" {
vStr = strings.TrimSpace(string(buf))
} else {
vStr := string(buf)
Copy link
Contributor

Choose a reason for hiding this comment

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

rather than reassigning the variable, you can just do vStr := string(bytes.TrimSpace(bytes.Trim(buf, "\x00")))

@sparrc sparrc added this to the 1.2.0 milestone Nov 16, 2016
@sparrc
Copy link
Contributor

sparrc commented Nov 16, 2016

thanks, please update the changelog and I'll merge

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug unexpected problem or unintended behavior
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants