diff options
author | Guy Harris <guy@alum.mit.edu> | 2019-02-01 11:24:16 -0800 |
---|---|---|
committer | Guy Harris <guy@alum.mit.edu> | 2019-02-01 20:35:05 +0000 |
commit | b3bb4aa8f4ee948041442f2e35fb0a4aa3d28667 (patch) | |
tree | 80330ba41e64f61a529dc23f0506c72091c93e82 | |
parent | 961d8bfc5d37edf5f3361f4cf0f4e251175de1fc (diff) |
Have win32strerror() return interned strings.
That's what g_strerror() does, and it means that the caller doesn't need
to free the string (it's kept around, and if another call to
win32strerror() generates the same string, the interned string is
returned).
Change-Id: I564bb700fabe2629131fb1c6468494dd5f5fc9e3
Reviewed-on: https://code.wireshark.org/review/31854
Petri-Dish: Guy Harris <guy@alum.mit.edu>
Tested-by: Petri Dish Buildbot
Reviewed-by: Guy Harris <guy@alum.mit.edu>
-rw-r--r-- | capchild/capture_sync.c | 30 | ||||
-rw-r--r-- | wsutil/win32-utils.c | 37 | ||||
-rw-r--r-- | wsutil/win32-utils.h | 2 |
3 files changed, 29 insertions, 40 deletions
diff --git a/capchild/capture_sync.c b/capchild/capture_sync.c index ece0ae00ad..c3bd19cc5e 100644 --- a/capchild/capture_sync.c +++ b/capchild/capture_sync.c @@ -206,7 +206,6 @@ sync_pipe_start(capture_options *capture_opts, capture_session *cap_session, inf PROCESS_INFORMATION pi; char control_id[ARGV_NUMBER_LEN]; gchar *signal_pipe_name; - char *errmsg; #else char errmsg[1024+1]; int sync_pipe[2]; /* pipe used to send messages from child to parent */ @@ -434,9 +433,8 @@ sync_pipe_start(capture_options *capture_opts, capture_session *cap_session, inf /* (increase this value if you have trouble while fast capture file switches) */ if (! CreatePipe(&sync_pipe_read, &sync_pipe_write, &sa, 5120)) { /* Couldn't create the pipe between parent and child. */ - errmsg = win32strerror(GetLastError()); - report_failure("Couldn't create sync pipe: %s", errmsg); - g_free(errmsg); + report_failure("Couldn't create sync pipe: %s", + win32strerror(GetLastError())); free_argv(argv, argc); return FALSE; } @@ -466,9 +464,8 @@ sync_pipe_start(capture_options *capture_opts, capture_session *cap_session, inf if (signal_pipe == INVALID_HANDLE_VALUE) { /* Couldn't create the signal pipe between parent and child. */ - errmsg = win32strerror(GetLastError()); - report_failure("Couldn't create signal pipe: %s", errmsg); - g_free(errmsg); + report_failure("Couldn't create signal pipe: %s", + win32strerror(GetLastError())); ws_close(sync_pipe_read_fd); /* Should close sync_pipe_read */ CloseHandle(sync_pipe_write); free_argv(argv, argc); @@ -526,10 +523,8 @@ sync_pipe_start(capture_options *capture_opts, capture_session *cap_session, inf /* call dumpcap */ if(!win32_create_process(argv[0], args->str, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi)) { - errmsg = win32strerror(GetLastError()); report_failure("Couldn't run %s in child process: %s", - args->str, errmsg); - g_free(errmsg); + args->str, win32strerror(GetLastError())); ws_close(sync_pipe_read_fd); /* Should close sync_pipe_read */ CloseHandle(sync_pipe_write); CloseHandle(signal_pipe); @@ -652,7 +647,6 @@ sync_pipe_open_command(char* const argv[], int *data_read_fd, STARTUPINFO si; PROCESS_INFORMATION pi; int i; - char *errmsg; #else char errmsg[1024+1]; int sync_pipe[2]; /* pipe used to send messages from child to parent */ @@ -681,9 +675,8 @@ sync_pipe_open_command(char* const argv[], int *data_read_fd, /* (increase this value if you have trouble while fast capture file switches) */ if (! CreatePipe(&sync_pipe[PIPE_READ], &sync_pipe[PIPE_WRITE], &sa, 5120)) { /* Couldn't create the message pipe between parent and child. */ - errmsg = win32strerror(GetLastError()); - *msg = g_strdup_printf("Couldn't create sync pipe: %s", errmsg); - g_free(errmsg); + *msg = g_strdup_printf("Couldn't create sync pipe: %s", + win32strerror(GetLastError())); return -1; } @@ -706,9 +699,8 @@ sync_pipe_open_command(char* const argv[], int *data_read_fd, /* (increase this value if you have trouble while fast capture file switches) */ if (! CreatePipe(&data_pipe[PIPE_READ], &data_pipe[PIPE_WRITE], &sa, 5120)) { /* Couldn't create the message pipe between parent and child. */ - errmsg = win32strerror(GetLastError()); - *msg = g_strdup_printf("Couldn't create data pipe: %s", errmsg); - g_free(errmsg); + *msg = g_strdup_printf("Couldn't create data pipe: %s", + win32strerror(GetLastError())); ws_close(*message_read_fd); /* Should close sync_pipe[PIPE_READ] */ CloseHandle(sync_pipe[PIPE_WRITE]); return -1; @@ -760,10 +752,8 @@ sync_pipe_open_command(char* const argv[], int *data_read_fd, /* call dumpcap */ if(!win32_create_process(argv[0], args->str, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi)) { - errmsg = win32strerror(GetLastError()); *msg = g_strdup_printf("Couldn't run %s in child process: %s", - args->str, errmsg); - g_free(errmsg); + args->str, win32strerror(GetLastError())); ws_close(*data_read_fd); /* Should close data_pipe[PIPE_READ] */ CloseHandle(data_pipe[PIPE_WRITE]); ws_close(*message_read_fd); /* Should close sync_pipe[PIPE_READ] */ diff --git a/wsutil/win32-utils.c b/wsutil/win32-utils.c index 56b6df74bb..3850868e80 100644 --- a/wsutil/win32-utils.c +++ b/wsutil/win32-utils.c @@ -83,8 +83,7 @@ protect_arg (const gchar *argv) } /* - * Generate a UTF-8 string for a Win32 error. - * The string must be freed with g_free(). + * Generate a UTF-8 string for a Windows error. */ /* @@ -93,13 +92,14 @@ protect_arg (const gchar *argv) * did Microsoft bother supporting a minimum buffer size?) */ #define ERRBUF_SIZE 128 -char * +const char * win32strerror(DWORD error) { DWORD retval; WCHAR *utf16_message; - char *msg; char *utf8_message; + char *tempmsg; + const char *msg; /* * XXX - what language ID to use? @@ -119,8 +119,10 @@ win32strerror(DWORD error) (LPTSTR)&utf16_message, ERRBUF_SIZE, NULL); if (retval == 0) { /* Failed. */ - msg = g_strdup_printf("Couldn't get error message for error (%lu) (because %lu)", - error, GetLastError()); + tempmsg = g_strdup_printf("Couldn't get error message for error (%lu) (because %lu)", + error, GetLastError()); + msg = g_intern_string(tempmsg); + g_free(tempmsg); return msg; } @@ -128,12 +130,16 @@ win32strerror(DWORD error) LocalFree(utf16_message); if (utf8_message == NULL) { /* Conversion failed. */ - msg = g_strdup_printf("Couldn't convert error message for error to UTF-8 (%lu) (because %lu)", - error, GetLastError()); + tempmsg = g_strdup_printf("Couldn't convert error message for error to UTF-8 (%lu) (because %lu)", + error, GetLastError()); + msg = g_intern_string(tempmsg); + g_free(tempmsg); return msg; } - msg = g_strdup_printf("%s (%lu)", utf8_message, error); + tempmsg = g_strdup_printf("%s (%lu)", utf8_message, error); g_free(utf8_message); + msg = g_intern_string(tempmsg); + g_free(tempmsg); return msg; } @@ -189,15 +195,12 @@ win32strexception(DWORD exception) static void win32_kill_child_on_exit(HANDLE child_handle) { static HANDLE cjo_handle = NULL; - char *errmsg; if (!cjo_handle) { cjo_handle = CreateJobObject(NULL, _T("Local\\Wireshark child process cleanup")); if (!cjo_handle) { - errmsg = win32strerror(GetLastError()); g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Could not create child cleanup job object: %s", - errmsg); - g_free(errmsg); + win32strerror(GetLastError())); return; } @@ -206,19 +209,15 @@ static void win32_kill_child_on_exit(HANDLE child_handle) { BOOL sijo_ret = SetInformationJobObject(cjo_handle, JobObjectExtendedLimitInformation, &cjo_jel_info, sizeof(cjo_jel_info)); if (!sijo_ret) { - errmsg = win32strerror(GetLastError()); g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Could not set child cleanup limits: %s", - errmsg); - g_free(errmsg); + win32strerror(GetLastError())); } } BOOL aptjo_ret = AssignProcessToJobObject(cjo_handle, child_handle); if (!aptjo_ret) { - errmsg = win32strerror(GetLastError()); g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Could not assign child cleanup process: %s", - errmsg); - g_free(errmsg); + win32strerror(GetLastError())); } } diff --git a/wsutil/win32-utils.h b/wsutil/win32-utils.h index 13d06c591c..7f7e401d38 100644 --- a/wsutil/win32-utils.h +++ b/wsutil/win32-utils.h @@ -55,7 +55,7 @@ gchar * protect_arg (const gchar *argv); * it must be freed with g_free(). */ WS_DLL_PUBLIC -char * win32strerror(DWORD error); +const char * win32strerror(DWORD error); /** Generate a string for a Win32 exception code. * |