From afc05a3ab0ad2ab79385f3e3f029eb34849eda0e Mon Sep 17 00:00:00 2001 From: Matthew Taylor Date: Thu, 17 Aug 2017 18:25:29 +0100 Subject: [PATCH] unix: modify argv[0] when process title is set Ensure that argv[0] is changed when uv_set_process_title() is called. Previously, on some unix systems uv__set_process_title() was being called, but argv[0] was not modified. Partial revert of 78c17238f48d9083359206a2215fc63dd7a0283d. Refs: https://github.com/libuv/libuv/pull/1396 PR-URL: https://github.com/libuv/libuv/pull/1487 Reviewed-By: Colin Ihrig --- src/unix/proctitle.c | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/src/unix/proctitle.c b/src/unix/proctitle.c index 70e91bfc..2ed0b21c 100644 --- a/src/unix/proctitle.c +++ b/src/unix/proctitle.c @@ -48,14 +48,12 @@ char** uv_setup_args(int argc, char** argv) { for (i = 0; i < argc; i++) size += strlen(argv[i]) + 1; - process_title.str = uv__strdup(argv[0]); - if (process_title.str == NULL) - return argv; - #if defined(__MVS__) /* argv is not adjacent. So just use argv[0] */ - process_title.len = strlen(process_title.str); + process_title.str = argv[0]; + process_title.len = strlen(argv[0]); #else + process_title.str = argv[0]; process_title.len = argv[argc - 1] + strlen(argv[argc - 1]) - argv[0]; assert(process_title.len + 1 == size); /* argv memory should be adjacent. */ #endif @@ -83,15 +81,11 @@ char** uv_setup_args(int argc, char** argv) { int uv_set_process_title(const char* title) { - char* new_title; - /* Copy the title into our own buffer. We don't want to free the pointer - * on libuv shutdown because the program might still be using it. */ - new_title = uv__strdup(title); - if (new_title == NULL) - return -ENOMEM; - uv__free(process_title.str); - process_title.str = new_title; - process_title.len = strlen(new_title); + if (process_title.len == 0) + return 0; + + /* No need to terminate, byte after is always '\0'. */ + strncpy(process_title.str, title, process_title.len); uv__set_process_title(title); return 0;