]> xenbits.xensource.com Git - xen.git/commitdiff
ocaml/xenctrl: Make failwith_xc() thread safe
authorAndrew Cooper <andrew.cooper3@citrix.com>
Wed, 28 Jan 2015 17:55:32 +0000 (17:55 +0000)
committerIan Jackson <Ian.Jackson@eu.citrix.com>
Wed, 12 Aug 2015 10:57:40 +0000 (11:57 +0100)
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().

Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
CC: Dave Scott <Dave.Scott@eu.citrix.com>
CC: Ian Campbell <Ian.Campbell@citrix.com>
CC: Ian Jackson <Ian.Jackson@eu.citrix.com>
CC: Wei Liu <wei.liu2@citrix.com>
Acked-by: David Scott <dave.scott@citrix.com>
(cherry picked from commit c8945d51613450c19e0898b1b3056c90f4929179)
(cherry picked from commit 032673c8836e28d9e291e0d02235001c41aedaab)
(cherry picked from commit 9702e084d09550495c2e71f2639c1c2c43aeaf63)

tools/ocaml/libs/xc/xenctrl_stubs.c

index c27c897e668db08cf7cfdd6058e3f8ba27bfe0f9..10d4b14b80bc83abc855fd0c26e9939a70916ccc 100644 (file)
        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);
 }