Addressed issue identified by static code analysis: potential buffer … (#9889)

Summary:
…overrun
Pull Request resolved: https://github.com/pytorch/pytorch/pull/9889

Differential Revision: D9026278

Pulled By: soumith

fbshipit-source-id: ee2ee255f34731ddc581261984c3caf56faa0e12
diff --git a/torch/csrc/Exceptions.cpp b/torch/csrc/Exceptions.cpp
index 306dd3a..4de8a71 100644
--- a/torch/csrc/Exceptions.cpp
+++ b/torch/csrc/Exceptions.cpp
@@ -109,6 +109,10 @@
   static const size_t ERROR_BUF_SIZE = 1024;
   char error_buf[ERROR_BUF_SIZE];
   vsnprintf(error_buf, ERROR_BUF_SIZE, format, fmt_args);
+  
+  // Ensure that the string is null terminated
+  error_buf[sizeof(error_buf) / sizeof(*error_buf) - 1] = 0;
+
   return std::string(error_buf);
 }