@@ -119,8 +119,8 @@ func Print(priority Priority, format string, a ...interface{}) error {
119119}
120120
121121func 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
152159func isSocketSpaceError (err error ) bool {
0 commit comments