| 463 | } |
| 464 | |
| 465 | static cbm_subprocess_t *cbm_subprocess_copy_opts(const cbm_proc_opts_t *opts) { |
| 466 | if (!opts || !opts->bin || !opts->bin[0]) { |
| 467 | return NULL; |
| 468 | } |
| 469 | #ifdef _WIN32 |
| 470 | if (opts->windows_cmd_payload && |
| 471 | (!opts->windows_cmd_payload[0] || opts->argv || !cbm_win_cmd_path_is_absolute(opts->bin))) { |
| 472 | return NULL; |
| 473 | } |
| 474 | #else |
| 475 | if (opts->windows_cmd_payload) { |
| 476 | return NULL; |
| 477 | } |
| 478 | #endif |
| 479 | |
| 480 | size_t argc = 1; |
| 481 | if (opts->argv) { |
| 482 | argc = 0; |
| 483 | while (argc < CBM_SUBPROCESS_ARGV_LIMIT && opts->argv[argc]) { |
| 484 | argc++; |
| 485 | } |
| 486 | if (argc == 0 || argc == CBM_SUBPROCESS_ARGV_LIMIT) { |
| 487 | return NULL; |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | cbm_subprocess_t *process = (cbm_subprocess_t *)calloc(1, sizeof(*process)); |
| 492 | if (!process) { |
| 493 | return NULL; |
| 494 | } |
| 495 | process->bin = cbm_strdup(opts->bin); |
| 496 | process->windows_cmd_payload = |
| 497 | opts->windows_cmd_payload ? cbm_strdup(opts->windows_cmd_payload) : NULL; |
| 498 | process->argv = (char **)calloc(argc + 1, sizeof(*process->argv)); |
| 499 | process->argc = argc; |
| 500 | if (!process->bin || !process->argv || |
| 501 | (opts->windows_cmd_payload && !process->windows_cmd_payload)) { |
| 502 | cbm_subprocess_free_config(process); |
| 503 | return NULL; |
| 504 | } |
| 505 | for (size_t i = 0; i < argc; i++) { |
| 506 | const char *arg = opts->argv ? opts->argv[i] : opts->bin; |
| 507 | process->argv[i] = cbm_strdup(arg); |
| 508 | if (!process->argv[i]) { |
| 509 | cbm_subprocess_free_config(process); |
| 510 | return NULL; |
| 511 | } |
| 512 | } |
| 513 | if (opts->log_file) { |
| 514 | process->log_file = cbm_strdup(opts->log_file); |
| 515 | if (!process->log_file) { |
| 516 | cbm_subprocess_free_config(process); |
| 517 | return NULL; |
| 518 | } |
| 519 | } |
| 520 | process->on_log_line = opts->on_log_line; |
| 521 | process->log_ud = opts->log_ud; |
| 522 | process->quiet_timeout_ms = opts->quiet_timeout_ms; |
no test coverage detected