Skip to contentSkip to conversation

ocaml/xenctrl: Make failwith_xc() thread safe

PATCH Applied Andrew Cooper 4 participants 5 messages Jan 28, 2015 – Feb 2, 2015

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ocaml/xenctrl: Make failwith_xc() thread safe

The static error_str[] buffer is not thread-safe, and 1024 bytes is unreasonably large. Reduce to 256 bytes (which is still much larger than any current use), and move it to being a stack variable.

Also, propagate the Noreturn attribute from caml_raise_with_string().

CC Dave Scott <[email protected]>
CC Ian Campbell <[email protected]>
CC Ian Jackson <[email protected]>
CC Wei Liu <[email protected]>
---
 tools/ocaml/libs/xc/xenctrl_stubs.c |   15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
tools/ocaml/libs/xc/xenctrl_stubs.c +8 -7
index 92d064f..6a95528 100644
@@ -51,21 +51,22 @@
 	i1 = (uint32_t) Int64_val(Field(input, 0)); \
 	i2 = ((Field(input, 1) == Val_none) ? 0xffffffff : (uint32_t) Int64_val(Field(Field(input, 1), 0)));
 
-#define ERROR_STRLEN 1024
-void failwith_xc(xc_interface *xch)
+static void Noreturn failwith_xc(xc_interface *xch)
 {
-	static char error_str[ERROR_STRLEN];
+	char error_str[256];
 	if (xch) {
 		const xc_error *error = xc_get_last_error(xch);
 		if (error->code == XC_ERROR_NONE)
-                	snprintf(error_str, ERROR_STRLEN, "%d: %s", errno, strerror(errno));
+			snprintf(error_str, sizeof(error_str),
+				 "%d: %s", errno, strerror(errno));
 		else
-			snprintf(error_str, ERROR_STRLEN, "%d: %s: %s",
-				 error->code,
+			snprintf(error_str, sizeof(error_str),
+				 "%d: %s: %s", error->code,
 				 xc_error_code_to_desc(error->code),
 				 error->message);
 	} else {
-		snprintf(error_str, ERROR_STRLEN, "Unable to open XC interface");
+		snprintf(error_str, sizeof(error_str),
+			 "Unable to open XC interface");
 	}
 	caml_raise_with_string(*caml_named_value("xc.error"), error_str);
 }
Signature · 1 line
1.7.10.4
#1 lore raw

Looks ok to me.

Quoted 59 lines · Andrew Cooper

The static error_str[] buffer is not thread-safe, and 1024 bytes is unreasonably large. Reduce to 256 bytes (which is still much larger than any current use), and move it to being a stack variable.

Also, propagate the Noreturn attribute from caml_raise_with_string().

CC Dave Scott <[email protected]>
CC Ian Campbell <[email protected]>
CC Ian Jackson <[email protected]>
CC Wei Liu <[email protected]>

---
tools/ocaml/libs/xc/xenctrl_stubs.c | 15 ++++++++-------

1 file changed, 8 insertions(+), 7 deletions(-)
tools/ocaml/libs/xc/xenctrl_stubs.c +8 -7
index 92d064f..6a95528 100644
@@ -51,21 +51,22 @@
 	i1 = (uint32_t) Int64_val(Field(input, 0)); \
 	i2 = ((Field(input, 1) == Val_none) ? 0xffffffff : (uint32_t) Int64_val(Field(Field(input, 1), 0)));
 
-#define ERROR_STRLEN 1024
-void failwith_xc(xc_interface *xch)
+static void Noreturn failwith_xc(xc_interface *xch)
 {
-	static char error_str[ERROR_STRLEN];
+	char error_str[256];
 	if (xch) {
 		const xc_error *error = xc_get_last_error(xch);
 		if (error->code == XC_ERROR_NONE)
-                	snprintf(error_str, ERROR_STRLEN, "%d: %s", errno, strerror(errno));
+			snprintf(error_str, sizeof(error_str),
+				 "%d: %s", errno, strerror(errno));
 		else
-			snprintf(error_str, ERROR_STRLEN, "%d: %s: %s",
-				 error->code,
+			snprintf(error_str, sizeof(error_str),
+				 "%d: %s: %s", error->code,
 				 xc_error_code_to_desc(error->code),
 				 error->message);
 	} else {
-		snprintf(error_str, ERROR_STRLEN, "Unable to open XC interface");
+		snprintf(error_str, sizeof(error_str),
+			 "Unable to open XC interface");
 	}
 	caml_raise_with_string(*caml_named_value("xc.error"), error_str);
 }
Signature · 7 lines
1.7.10.4


_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Applied
#4 lore raw
Quoted 5 lines · Dave Scott
Quoted 5 lines · Wei Liu
Quoted 1 line · Dave Scott
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

Looks ok to me.

I think this should be an Acked-by.

Sorry, you’re completely right. Muscle memory strikes again! :)

;-)

Quoted 1 line

Thanks, Applied

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel