diff --git a/sapi/cli/php_cli_process_title.c b/sapi/cli/php_cli_process_title.c index d18af8de68671..a786de2b46594 100644 --- a/sapi/cli/php_cli_process_title.c +++ b/sapi/cli/php_cli_process_title.c @@ -27,13 +27,12 @@ PHP_FUNCTION(cli_set_process_title) { char *title = NULL; size_t title_len; - int rc; if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &title, &title_len) == FAILURE) { RETURN_THROWS(); } - rc = set_ps_title(title); + ps_title_status rc = set_ps_title(title, title_len); if (rc == PS_TITLE_SUCCESS) { RETURN_TRUE; } @@ -48,13 +47,12 @@ PHP_FUNCTION(cli_get_process_title) { size_t length = 0; const char* title = NULL; - int rc; if (zend_parse_parameters_none() == FAILURE) { RETURN_THROWS(); } - rc = get_ps_title(&length, &title); + ps_title_status rc = get_ps_title(&length, &title); if (rc != PS_TITLE_SUCCESS) { php_error_docref(NULL, E_WARNING, "cli_get_process_title had an error: %s", ps_title_errno(rc)); RETURN_NULL(); diff --git a/sapi/cli/ps_title.c b/sapi/cli/ps_title.c index 642e2247cfbca..ee977cd88cd1e 100644 --- a/sapi/cli/ps_title.c +++ b/sapi/cli/ps_title.c @@ -284,7 +284,7 @@ char** save_ps_args(int argc, char** argv) * and the init function was called. * Otherwise returns NOT_AVAILABLE or NOT_INITIALIZED */ -int is_ps_title_available(void) +ps_title_status is_ps_title_available(void) { #ifdef PS_USE_NONE return PS_TITLE_NOT_AVAILABLE; /* disabled functionality */ @@ -304,7 +304,7 @@ int is_ps_title_available(void) /* * Convert error codes into error strings */ -const char* ps_title_errno(int rc) +const char* ps_title_errno(ps_title_status rc) { switch(rc) { @@ -320,11 +320,16 @@ const char* ps_title_errno(int rc) case PS_TITLE_BUFFER_NOT_AVAILABLE: return "Buffer not contiguous"; -#ifdef PS_USE_WIN32 + case PS_TITLE_TOO_LONG: + // TODO Indicate max length? + return "Too long"; + case PS_TITLE_WINDOWS_ERROR: +#ifdef PS_USE_WIN32 snprintf(windows_error_details, sizeof(windows_error_details), "Windows error code: %lu", GetLastError()); return windows_error_details; #endif + return "Windows error"; } return "Unknown error code"; @@ -337,14 +342,19 @@ const char* ps_title_errno(int rc) * save_ps_args() was not called. * Else returns 0 on success. */ -int set_ps_title(const char* title) +ps_title_status set_ps_title(const char *title, size_t title_len) { - int rc = is_ps_title_available(); + if (title_len >= ps_buffer_size) { + return PS_TITLE_TOO_LONG; + } + + ps_title_status rc = is_ps_title_available(); if (rc != PS_TITLE_SUCCESS) return rc; - size_t title_len = strlcpy(ps_buffer, title, ps_buffer_size); - ps_buffer_cur_len = (title_len >= ps_buffer_size) ? ps_buffer_size - 1 : title_len; + /* Include final null byte */ + memcpy(ps_buffer, title, title_len+1); + ps_buffer_cur_len = title_len; #ifdef PS_USE_SETPROCTITLE setproctitle("%s", ps_buffer); @@ -394,9 +404,9 @@ int set_ps_title(const char* title) * length into *displen. * The return code indicates the error. */ -int get_ps_title(size_t *displen, const char** string) +ps_title_status get_ps_title(size_t *displen, const char** string) { - int rc = is_ps_title_available(); + ps_title_status rc = is_ps_title_available(); if (rc != PS_TITLE_SUCCESS) return rc; diff --git a/sapi/cli/ps_title.h b/sapi/cli/ps_title.h index a9dc53368bc30..7436c2b25a946 100644 --- a/sapi/cli/ps_title.h +++ b/sapi/cli/ps_title.h @@ -17,21 +17,24 @@ #ifndef PS_TITLE_HEADER #define PS_TITLE_HEADER -#define PS_TITLE_SUCCESS 0 -#define PS_TITLE_NOT_AVAILABLE 1 -#define PS_TITLE_NOT_INITIALIZED 2 -#define PS_TITLE_BUFFER_NOT_AVAILABLE 3 -#define PS_TITLE_WINDOWS_ERROR 4 +typedef enum { + PS_TITLE_SUCCESS = 0, + PS_TITLE_NOT_AVAILABLE = 1, + PS_TITLE_NOT_INITIALIZED = 2, + PS_TITLE_BUFFER_NOT_AVAILABLE = 3, + PS_TITLE_WINDOWS_ERROR = 4, + PS_TITLE_TOO_LONG = 5, +} ps_title_status; extern char** save_ps_args(int argc, char** argv); -extern int set_ps_title(const char* new_str); +extern ps_title_status set_ps_title(const char *title, size_t title_len); -extern int get_ps_title(size_t* displen, const char** string); +extern ps_title_status get_ps_title(size_t* displen, const char** string); -extern const char* ps_title_errno(int rc); +extern const char* ps_title_errno(ps_title_status rc); -extern int is_ps_title_available(void); +extern ps_title_status is_ps_title_available(void); extern void cleanup_ps_args(char **argv);