diff mbox series

[v3,4/8] golang/xenlight: Errors are negative

Message ID 20200117155734.1067550-4-george.dunlap@citrix.com (mailing list archive)
State New, archived
Headers show
Series [v3,1/8] golang/xenlight: Don't try to marshall zero-length arrays in fromC | expand

Commit Message

George Dunlap Jan. 17, 2020, 3:57 p.m. UTC
Commit 871e51d2d4 changed the sign on the xenlight error types (making
the values negative, same as the C-generated constants), but failed to
flip the sign in the Error() string function.  The result is that
ErrorNonspecific.String() prints "libxl error: 1" rather than the
human-readable error message.

Get rid of the whole issue by making libxlErrors a map, and mapping
actual error values to string, falling back to printing the actual
value of the Error type if it's not present.

Signed-off-by: George Dunlap <george.dunlap@citrix.com>
---
v2:
- Convert libxlErrors to a map.

CC: Nick Rosbrook <rosbrookn@ainfosec.com>
---
 tools/golang/xenlight/xenlight.go | 62 +++++++++++++++----------------
 1 file changed, 30 insertions(+), 32 deletions(-)

Comments

Nick Rosbrook Jan. 20, 2020, 11:40 p.m. UTC | #1
> Commit 871e51d2d4 changed the sign on the xenlight error types (making
> the values negative, same as the C-generated constants), but failed to
> flip the sign in the Error() string function.  The result is that
> ErrorNonspecific.String() prints "libxl error: 1" rather than the
> human-readable error message.
>
> Get rid of the whole issue by making libxlErrors a map, and mapping
> actual error values to string, falling back to printing the actual
> value of the Error type if it's not present.
>
> Signed-off-by: George Dunlap <george.dunlap@citrix.com>
Reviewed-by: Nick Rosbrook <rosbrookn@ainfosec.com>
diff mbox series

Patch

diff --git a/tools/golang/xenlight/xenlight.go b/tools/golang/xenlight/xenlight.go
index 1299981713..aa1e63a61a 100644
--- a/tools/golang/xenlight/xenlight.go
+++ b/tools/golang/xenlight/xenlight.go
@@ -36,42 +36,40 @@  import (
 	"unsafe"
 )
 
-var libxlErrors = [...]string{
-	-ErrorNonspecific:                  "Non-specific error",
-	-ErrorVersion:                      "Wrong version",
-	-ErrorFail:                         "Failed",
-	-ErrorNi:                           "Not Implemented",
-	-ErrorNomem:                        "No memory",
-	-ErrorInval:                        "Invalid argument",
-	-ErrorBadfail:                      "Bad Fail",
-	-ErrorGuestTimedout:                "Guest timed out",
-	-ErrorTimedout:                     "Timed out",
-	-ErrorNoparavirt:                   "No Paravirtualization",
-	-ErrorNotReady:                     "Not ready",
-	-ErrorOseventRegFail:               "OS event registration failed",
-	-ErrorBufferfull:                   "Buffer full",
-	-ErrorUnknownChild:                 "Unknown child",
-	-ErrorLockFail:                     "Lock failed",
-	-ErrorJsonConfigEmpty:              "JSON config empty",
-	-ErrorDeviceExists:                 "Device exists",
-	-ErrorCheckpointDevopsDoesNotMatch: "Checkpoint devops does not match",
-	-ErrorCheckpointDeviceNotSupported: "Checkpoint device not supported",
-	-ErrorVnumaConfigInvalid:           "VNUMA config invalid",
-	-ErrorDomainNotfound:               "Domain not found",
-	-ErrorAborted:                      "Aborted",
-	-ErrorNotfound:                     "Not found",
-	-ErrorDomainDestroyed:              "Domain destroyed",
-	-ErrorFeatureRemoved:               "Feature removed",
+var libxlErrors = map[Error]string{
+	ErrorNonspecific:                  "Non-specific error",
+	ErrorVersion:                      "Wrong version",
+	ErrorFail:                         "Failed",
+	ErrorNi:                           "Not Implemented",
+	ErrorNomem:                        "No memory",
+	ErrorInval:                        "Invalid argument",
+	ErrorBadfail:                      "Bad Fail",
+	ErrorGuestTimedout:                "Guest timed out",
+	ErrorTimedout:                     "Timed out",
+	ErrorNoparavirt:                   "No Paravirtualization",
+	ErrorNotReady:                     "Not ready",
+	ErrorOseventRegFail:               "OS event registration failed",
+	ErrorBufferfull:                   "Buffer full",
+	ErrorUnknownChild:                 "Unknown child",
+	ErrorLockFail:                     "Lock failed",
+	ErrorJsonConfigEmpty:              "JSON config empty",
+	ErrorDeviceExists:                 "Device exists",
+	ErrorCheckpointDevopsDoesNotMatch: "Checkpoint devops does not match",
+	ErrorCheckpointDeviceNotSupported: "Checkpoint device not supported",
+	ErrorVnumaConfigInvalid:           "VNUMA config invalid",
+	ErrorDomainNotfound:               "Domain not found",
+	ErrorAborted:                      "Aborted",
+	ErrorNotfound:                     "Not found",
+	ErrorDomainDestroyed:              "Domain destroyed",
+	ErrorFeatureRemoved:               "Feature removed",
 }
 
 func (e Error) Error() string {
-	if 0 < int(e) && int(e) < len(libxlErrors) {
-		s := libxlErrors[e]
-		if s != "" {
-			return s
-		}
+	if s, ok := libxlErrors[e]; ok {
+		return s
 	}
-	return fmt.Sprintf("libxl error: %d", -e)
+
+	return fmt.Sprintf("libxl error: %d", e)
 }
 
 // Context represents a libxl_ctx.