util: trim "\x00" characters from the kernel version

It seems that convering the release component from the unix.Utsrelease
type leaves some trailing "\x00" characters.

While splitting the string to compare kernel versions, these additional
characters might prevent converting the string to an int. Strip the
additional characters before returning the string.

Note:
  "\x00" characters are not visible when printing to a file or screen.
  They can be seen in hex-editors, or sending the output through 'xxd'.

Fixes: #1167
Signed-off-by: Niels de Vos <ndevos@redhat.com>
This commit is contained in:
Niels de Vos 2020-06-17 20:34:12 +02:00 committed by mergify[bot]
parent 1b0b26a7a6
commit ebe378c79a
2 changed files with 5 additions and 1 deletions

View File

@ -148,7 +148,7 @@ func KernelVersion() (string, error) {
if err != nil { if err != nil {
return "", err return "", err
} }
return string(utsname.Release[:]), nil return strings.TrimRight(string(utsname.Release[:]), "\x00"), nil
} }
// GenerateVolID generates a volume ID based on passed in parameters and version, to be returned // GenerateVolID generates a volume ID based on passed in parameters and version, to be returned

View File

@ -16,6 +16,7 @@ limitations under the License.
package util package util
import ( import (
"strings"
"testing" "testing"
) )
@ -151,4 +152,7 @@ func TestKernelVersion(t *testing.T) {
if version == "" { if version == "" {
t.Error("version is empty, this is unexpected?!") t.Error("version is empty, this is unexpected?!")
} }
if strings.HasSuffix(version, "\x00") {
t.Error("version ends with \\x00 byte(s)")
}
} }