@@ -400,52 +400,54 @@ get_child_pids_platform(pid_t target_pid, int recursive, pid_array_t *result)
400400 return -1 ;
401401 }
402402
403- /* First pass: count processes */
404- PROCESSENTRY32 pe ;
405- pe . dwSize = sizeof ( PROCESSENTRY32 ) ;
403+ /* Use dynamic arrays for single-pass collection */
404+ pid_array_t all_pids ;
405+ pid_array_t ppids ;
406406
407- size_t count = 0 ;
408- if (Process32First (snapshot , & pe )) {
409- do {
410- count ++ ;
411- } while (Process32Next (snapshot , & pe ));
407+ if (pid_array_init (& all_pids ) < 0 ) {
408+ CloseHandle (snapshot );
409+ return -1 ;
412410 }
413411
414- /* Allocate arrays for PIDs and PPIDs */
415- pid_t * pid_list = (pid_t * )PyMem_Malloc (count * sizeof (pid_t ));
416- pid_t * ppids = (pid_t * )PyMem_Malloc (count * sizeof (pid_t ));
417- if (!pid_list || !ppids ) {
412+ if (pid_array_init (& ppids ) < 0 ) {
418413 CloseHandle (snapshot );
419- PyMem_Free (pid_list );
420- PyMem_Free (ppids );
421- PyErr_NoMemory ();
414+ pid_array_cleanup (& all_pids );
422415 return -1 ;
423416 }
424417
425- /* Second pass: collect PIDs and PPIDs */
418+ /* Single pass: collect PIDs and PPIDs together */
419+ PROCESSENTRY32 pe ;
426420 pe .dwSize = sizeof (PROCESSENTRY32 );
427- size_t idx = 0 ;
428421 if (Process32First (snapshot , & pe )) {
429422 do {
430- pid_list [idx ] = (pid_t )pe .th32ProcessID ;
431- ppids [idx ] = (pid_t )pe .th32ParentProcessID ;
432- idx ++ ;
433- } while (Process32Next (snapshot , & pe ) && idx < count );
423+ if (pid_array_append (& all_pids , (pid_t )pe .th32ProcessID ) < 0 ) {
424+ CloseHandle (snapshot );
425+ pid_array_cleanup (& all_pids );
426+ pid_array_cleanup (& ppids );
427+ return -1 ;
428+ }
429+ if (pid_array_append (& ppids , (pid_t )pe .th32ParentProcessID ) < 0 ) {
430+ CloseHandle (snapshot );
431+ pid_array_cleanup (& all_pids );
432+ pid_array_cleanup (& ppids );
433+ return -1 ;
434+ }
435+ } while (Process32Next (snapshot , & pe ));
434436 }
435437
436438 CloseHandle (snapshot );
437439
438440 /* Find children using BFS */
439441 pid_array_t to_process ;
440442 if (pid_array_init (& to_process ) < 0 ) {
441- PyMem_Free ( pid_list );
442- PyMem_Free ( ppids );
443+ pid_array_cleanup ( & all_pids );
444+ pid_array_cleanup ( & ppids );
443445 return -1 ;
444446 }
445447
446448 if (pid_array_append (& to_process , target_pid ) < 0 ) {
447- PyMem_Free ( pid_list );
448- PyMem_Free ( ppids );
449+ pid_array_cleanup ( & all_pids );
450+ pid_array_cleanup ( & ppids );
449451 pid_array_cleanup (& to_process );
450452 return -1 ;
451453 }
@@ -454,22 +456,22 @@ get_child_pids_platform(pid_t target_pid, int recursive, pid_array_t *result)
454456 while (process_idx < to_process .count ) {
455457 pid_t current_pid = to_process .pids [process_idx ++ ];
456458
457- for (size_t i = 0 ; i < idx ; i ++ ) {
458- if (ppids [i ] == current_pid ) {
459- pid_t child_pid = pid_list [i ];
459+ for (size_t i = 0 ; i < all_pids . count ; i ++ ) {
460+ if (ppids . pids [i ] == current_pid ) {
461+ pid_t child_pid = all_pids . pids [i ];
460462
461463 if (!pid_array_contains (result , child_pid )) {
462464 if (pid_array_append (result , child_pid ) < 0 ) {
463- PyMem_Free ( pid_list );
464- PyMem_Free ( ppids );
465+ pid_array_cleanup ( & all_pids );
466+ pid_array_cleanup ( & ppids );
465467 pid_array_cleanup (& to_process );
466468 return -1 ;
467469 }
468470
469471 if (recursive ) {
470472 if (pid_array_append (& to_process , child_pid ) < 0 ) {
471- PyMem_Free ( pid_list );
472- PyMem_Free ( ppids );
473+ pid_array_cleanup ( & all_pids );
474+ pid_array_cleanup ( & ppids );
473475 pid_array_cleanup (& to_process );
474476 return -1 ;
475477 }
@@ -483,8 +485,8 @@ get_child_pids_platform(pid_t target_pid, int recursive, pid_array_t *result)
483485 }
484486 }
485487
486- PyMem_Free ( pid_list );
487- PyMem_Free ( ppids );
488+ pid_array_cleanup ( & all_pids );
489+ pid_array_cleanup ( & ppids );
488490 pid_array_cleanup (& to_process );
489491 return 0 ;
490492}
0 commit comments