From 315ea327a63195d86082eecd502b830a3b2ece5e Mon Sep 17 00:00:00 2001 From: Gurucharan Shetty Date: Fri, 14 Feb 2014 08:12:32 -0800 Subject: [PATCH] util: Pre-allocate buffer for ovs_lasterror_to_string(). This lets us call ovs_lasterror_to_string() and not having to do an extra call of LocalFree() on the returned string. Signed-off-by: Gurucharan Shetty Acked-by: Ben Pfaff --- lib/entropy.c | 4 +--- lib/lockfile.c | 4 +--- lib/socket-util.c | 19 +++---------------- lib/util.c | 25 +++++++++++++++---------- lib/util.h | 1 + 5 files changed, 21 insertions(+), 32 deletions(-) diff --git a/lib/entropy.c b/lib/entropy.c index 53f7e72a6..f9808553d 100644 --- a/lib/entropy.c +++ b/lib/entropy.c @@ -60,10 +60,8 @@ get_entropy(void *buffer, size_t n) CryptAcquireContext(&crypt_prov, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT); if (!CryptGenRandom(crypt_prov, n, buffer)) { - char *msg_buf = ovs_lasterror_to_string(); + VLOG_ERR("CryptGenRandom: read error (%s)", ovs_lasterror_to_string()); error = EINVAL; - VLOG_ERR("CryptGenRandom: read error (%s)", msg_buf); - LocalFree(msg_buf); } CryptReleaseContext(crypt_prov, 0); diff --git a/lib/lockfile.c b/lib/lockfile.c index 2774177e0..779b74eb3 100644 --- a/lib/lockfile.c +++ b/lib/lockfile.c @@ -285,9 +285,7 @@ lockfile_try_lock_windows(const char *name, pid_t *pidp, retval = LockFileEx(lock_handle, LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY, 0, 1, 0, &overl); if (!retval) { - char *msg_buf = ovs_lasterror_to_string(); - VLOG_WARN("Failed to lock file : %s", msg_buf); - LocalFree(msg_buf); + VLOG_WARN("Failed to lock file : %s", ovs_lasterror_to_string()); return EEXIST; } diff --git a/lib/socket-util.c b/lib/socket-util.c index 6a7d50e7f..d83ddf1ef 100644 --- a/lib/socket-util.c +++ b/lib/socket-util.c @@ -65,13 +65,6 @@ VLOG_DEFINE_THIS_MODULE(socket_util); * space for a null terminator. */ #define MAX_UN_LEN (sizeof(((struct sockaddr_un *) 0)->sun_path) - 1) -#ifdef _WIN32 -/* Buffer used by sock_strerror(). */ -DEFINE_STATIC_PER_THREAD_DATA(struct { char s[128]; }, - sockerror_buffer, - { "" }); -#endif - static int getsockopt_int(int fd, int level, int option, const char *optname, int *valuep); @@ -1355,21 +1348,15 @@ ss_length(const struct sockaddr_storage *ss) /* For Windows socket calls, 'errno' is not set. One has to call * WSAGetLastError() to get the error number and then pass it to - * FormatMessage() (through this function) to get the correct error string. - + * this function to get the correct error string. + * * ovs_strerror() calls strerror_r() and would not get the correct error * string for Windows sockets, but is good for POSIX. */ const char * sock_strerror(int error) { #ifdef _WIN32 - enum { BUFSIZE = sizeof sockerror_buffer_get()->s }; - char *buffer = sockerror_buffer_get()->s; - - FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM - | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, error, 0, - buffer, BUFSIZE, NULL); - return buffer; + return ovs_format_message(error); #else return ovs_strerror(error); #endif diff --git a/lib/util.c b/lib/util.c index 45efdb709..911cc3e34 100644 --- a/lib/util.c +++ b/lib/util.c @@ -50,7 +50,7 @@ DEFINE_PER_THREAD_MALLOCED_DATA(char *, subprogram_name); /* --version option output. */ static char *program_version; -/* Buffer used by ovs_strerror(). */ +/* Buffer used by ovs_strerror() and ovs_format_message(). */ DEFINE_STATIC_PER_THREAD_DATA(struct { char s[128]; }, strerror_buffer, { "" }); @@ -1666,17 +1666,22 @@ exit: #ifdef _WIN32 -/* Calls FormatMessage() with GetLastError() as an argument. Returns - * pointer to a buffer that receives the null-terminated string that specifies - * the formatted message and that has to be freed by the caller with - * LocalFree(). */ char * -ovs_lasterror_to_string(void) +ovs_format_message(int error) { - char *buffer; - FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM - | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), 0, - (char *)&buffer, 0, NULL); + enum { BUFSIZE = sizeof strerror_buffer_get()->s }; + char *buffer = strerror_buffer_get()->s; + + FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, error, 0, buffer, BUFSIZE, NULL); return buffer; } + +/* Returns a null-terminated string that explains the last error. + * Use this function to get the error string for WINAPI calls. */ +char * +ovs_lasterror_to_string(void) +{ + return ovs_format_message(GetLastError()); +} #endif diff --git a/lib/util.h b/lib/util.h index c80e8f987..9afe10e61 100644 --- a/lib/util.h +++ b/lib/util.h @@ -491,6 +491,7 @@ uint64_t bitwise_get(const void *src, unsigned int src_len, #ifdef _WIN32 +char *ovs_format_message(int error); char *ovs_lasterror_to_string(void); #endif -- 2.43.0