diff --git a/ocaml/runtime/str.c b/ocaml/runtime/str.c index aa5b650ccf3..59b41b3bc70 100644 --- a/ocaml/runtime/str.c +++ b/ocaml/runtime/str.c @@ -411,7 +411,9 @@ CAMLexport value caml_alloc_sprintf(const char * format, ...) excluding the terminating '\0'. */ n = vsnprintf(buf, sizeof(buf), format, args); va_end(args); - if (n < sizeof(buf)) { + if (n < 0) { + caml_raise_out_of_memory(); + } else if (n < sizeof(buf)) { /* All output characters were written to buf, including the terminating '\0'. Allocate a Caml string with length "n" as computed by vsnprintf, and copy the output of vsnprintf into it. */