Skip to content

Commit eee3db3

Browse files
author
Luca Bruno
authored
Merge pull request #285 from GraveRaven/validVarName
journal: fix validation of key names
2 parents 88bfeed + 013f890 commit eee3db3

File tree

2 files changed

+36
-26
lines changed

2 files changed

+36
-26
lines changed

journal/journal.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ func Print(priority Priority, format string, a ...interface{}) error {
119119
}
120120

121121
func appendVariable(w io.Writer, name, value string) {
122-
if !validVarName(name) {
123-
journalError("variable name contains invalid character, ignoring")
122+
if err := validVarName(name); err != nil {
123+
journalError(err.Error())
124124
}
125125
if strings.ContainsRune(value, '\n') {
126126
/* When the value contains a newline, we write:
@@ -137,16 +137,23 @@ func appendVariable(w io.Writer, name, value string) {
137137
}
138138
}
139139

140-
func validVarName(name string) bool {
141-
/* The variable name must be in uppercase and consist only of characters,
142-
* numbers and underscores, and may not begin with an underscore. (from the docs)
143-
*/
140+
// validVarName validates a variable name to make sure it journald will accept it.
141+
// The variable name must be in uppercase and consist only of characters,
142+
// numbers and underscores, and may not begin with an underscore. (from the docs)
143+
// https://www.freedesktop.org/software/systemd/man/sd_journal_print.html
144+
func validVarName(name string) error {
145+
if name == "" {
146+
return errors.New("Empty variable name")
147+
} else if name[0] == '_' {
148+
return errors.New("Variable name begins with an underscore")
149+
}
144150

145-
valid := name[0] != '_'
146151
for _, c := range name {
147-
valid = valid && ('A' <= c && c <= 'Z') || ('0' <= c && c <= '9') || c == '_'
152+
if !(('A' <= c && c <= 'Z') || ('0' <= c && c <= '9') || c == '_') {
153+
return errors.New("Variable name contains invalid characters")
154+
}
148155
}
149-
return valid
156+
return nil
150157
}
151158

152159
func isSocketSpaceError(err error) bool {

journal/journal_test.go

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,30 @@ import (
2222
"testing"
2323
)
2424

25-
func TestValidaVarName(t *testing.T) {
26-
testCases := []struct {
27-
testcase string
28-
valid bool
29-
}{
30-
{
31-
"TEST",
32-
true,
33-
},
34-
{
35-
"test",
36-
false,
37-
},
25+
func TestValidVarName(t *testing.T) {
26+
validTestCases := []string{
27+
"TEST",
28+
"TE_ST",
29+
"TEST_",
30+
"0TEST0",
31+
}
32+
invalidTestCases := []string{
33+
"test",
34+
"_TEST",
35+
"",
3836
}
3937

40-
for _, tt := range testCases {
41-
valid := validVarName(tt.testcase)
42-
if valid != tt.valid {
43-
t.Fatalf("expected %t, got %t", tt.valid, valid)
38+
for _, tt := range validTestCases {
39+
if err := validVarName(tt); err != nil {
40+
t.Fatalf("\"%s\" should be a valid variable", tt)
41+
}
42+
}
43+
for _, tt := range invalidTestCases {
44+
if err := validVarName(tt); err == nil {
45+
t.Fatalf("\"%s\" should be an invalid variable", tt)
4446
}
4547
}
48+
4649
}
4750

4851
func TestJournalSend(t *testing.T) {

0 commit comments

Comments
 (0)