Skip to content

Commit d21777d

Browse files
authored
sapi/cli: Refactor process title setting code (#17177)
1 parent 6972612 commit d21777d

File tree

3 files changed

+33
-22
lines changed

3 files changed

+33
-22
lines changed

sapi/cli/php_cli_process_title.c

+2-4
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,12 @@ PHP_FUNCTION(cli_set_process_title)
2727
{
2828
char *title = NULL;
2929
size_t title_len;
30-
int rc;
3130

3231
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &title, &title_len) == FAILURE) {
3332
RETURN_THROWS();
3433
}
3534

36-
rc = set_ps_title(title);
35+
ps_title_status rc = set_ps_title(title, title_len);
3736
if (rc == PS_TITLE_SUCCESS) {
3837
RETURN_TRUE;
3938
}
@@ -48,13 +47,12 @@ PHP_FUNCTION(cli_get_process_title)
4847
{
4948
size_t length = 0;
5049
const char* title = NULL;
51-
int rc;
5250

5351
if (zend_parse_parameters_none() == FAILURE) {
5452
RETURN_THROWS();
5553
}
5654

57-
rc = get_ps_title(&length, &title);
55+
ps_title_status rc = get_ps_title(&length, &title);
5856
if (rc != PS_TITLE_SUCCESS) {
5957
php_error_docref(NULL, E_WARNING, "cli_get_process_title had an error: %s", ps_title_errno(rc));
6058
RETURN_NULL();

sapi/cli/ps_title.c

+19-9
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ char** save_ps_args(int argc, char** argv)
284284
* and the init function was called.
285285
* Otherwise returns NOT_AVAILABLE or NOT_INITIALIZED
286286
*/
287-
int is_ps_title_available(void)
287+
ps_title_status is_ps_title_available(void)
288288
{
289289
#ifdef PS_USE_NONE
290290
return PS_TITLE_NOT_AVAILABLE; /* disabled functionality */
@@ -304,7 +304,7 @@ int is_ps_title_available(void)
304304
/*
305305
* Convert error codes into error strings
306306
*/
307-
const char* ps_title_errno(int rc)
307+
const char* ps_title_errno(ps_title_status rc)
308308
{
309309
switch(rc)
310310
{
@@ -320,11 +320,16 @@ const char* ps_title_errno(int rc)
320320
case PS_TITLE_BUFFER_NOT_AVAILABLE:
321321
return "Buffer not contiguous";
322322

323-
#ifdef PS_USE_WIN32
323+
case PS_TITLE_TOO_LONG:
324+
// TODO Indicate max length?
325+
return "Too long";
326+
324327
case PS_TITLE_WINDOWS_ERROR:
328+
#ifdef PS_USE_WIN32
325329
snprintf(windows_error_details, sizeof(windows_error_details), "Windows error code: %lu", GetLastError());
326330
return windows_error_details;
327331
#endif
332+
return "Windows error";
328333
}
329334

330335
return "Unknown error code";
@@ -337,14 +342,19 @@ const char* ps_title_errno(int rc)
337342
* save_ps_args() was not called.
338343
* Else returns 0 on success.
339344
*/
340-
int set_ps_title(const char* title)
345+
ps_title_status set_ps_title(const char *title, size_t title_len)
341346
{
342-
int rc = is_ps_title_available();
347+
if (title_len >= ps_buffer_size) {
348+
return PS_TITLE_TOO_LONG;
349+
}
350+
351+
ps_title_status rc = is_ps_title_available();
343352
if (rc != PS_TITLE_SUCCESS)
344353
return rc;
345354

346-
size_t title_len = strlcpy(ps_buffer, title, ps_buffer_size);
347-
ps_buffer_cur_len = (title_len >= ps_buffer_size) ? ps_buffer_size - 1 : title_len;
355+
/* Include final null byte */
356+
memcpy(ps_buffer, title, title_len+1);
357+
ps_buffer_cur_len = title_len;
348358

349359
#ifdef PS_USE_SETPROCTITLE
350360
setproctitle("%s", ps_buffer);
@@ -394,9 +404,9 @@ int set_ps_title(const char* title)
394404
* length into *displen.
395405
* The return code indicates the error.
396406
*/
397-
int get_ps_title(size_t *displen, const char** string)
407+
ps_title_status get_ps_title(size_t *displen, const char** string)
398408
{
399-
int rc = is_ps_title_available();
409+
ps_title_status rc = is_ps_title_available();
400410
if (rc != PS_TITLE_SUCCESS)
401411
return rc;
402412

sapi/cli/ps_title.h

+12-9
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,24 @@
1717
#ifndef PS_TITLE_HEADER
1818
#define PS_TITLE_HEADER
1919

20-
#define PS_TITLE_SUCCESS 0
21-
#define PS_TITLE_NOT_AVAILABLE 1
22-
#define PS_TITLE_NOT_INITIALIZED 2
23-
#define PS_TITLE_BUFFER_NOT_AVAILABLE 3
24-
#define PS_TITLE_WINDOWS_ERROR 4
20+
typedef enum {
21+
PS_TITLE_SUCCESS = 0,
22+
PS_TITLE_NOT_AVAILABLE = 1,
23+
PS_TITLE_NOT_INITIALIZED = 2,
24+
PS_TITLE_BUFFER_NOT_AVAILABLE = 3,
25+
PS_TITLE_WINDOWS_ERROR = 4,
26+
PS_TITLE_TOO_LONG = 5,
27+
} ps_title_status;
2528

2629
extern char** save_ps_args(int argc, char** argv);
2730

28-
extern int set_ps_title(const char* new_str);
31+
extern ps_title_status set_ps_title(const char *title, size_t title_len);
2932

30-
extern int get_ps_title(size_t* displen, const char** string);
33+
extern ps_title_status get_ps_title(size_t* displen, const char** string);
3134

32-
extern const char* ps_title_errno(int rc);
35+
extern const char* ps_title_errno(ps_title_status rc);
3336

34-
extern int is_ps_title_available(void);
37+
extern ps_title_status is_ps_title_available(void);
3538

3639
extern void cleanup_ps_args(char **argv);
3740

0 commit comments

Comments
 (0)