Skip to content

Close fd/memory leak paths in pg_upgrade file/version probing#1

Draft
Copilot wants to merge 2 commits into
masterfrom
copilot/scan-code-fd-leak-memory-leak
Draft

Close fd/memory leak paths in pg_upgrade file/version probing#1
Copilot wants to merge 2 commits into
masterfrom
copilot/scan-code-fd-leak-memory-leak

Conversation

Copy link
Copy Markdown

Copilot AI commented Mar 18, 2026

针对“扫描代码中的 fd leak、memory leak”问题,此 PR 聚焦 pg_upgrade 的高风险错误路径:在 pg_fatal() 退出前补齐资源清理,避免在失败分支遗留打开的文件描述符和堆内存。

  • exec.c: harden get_bin_version() pipe lifecycle

    • popen()fgets() 失败处理拆分,避免 fgets() 失败时跳过 pclose()
    • 在清理前保存并恢复 errno,保证 %m 错误信息不被 cleanup 覆盖
  • file.c: close/free on all early-fatal branches

    • cloneFile():
      • dest_fd 打开失败时关闭已打开的 src_fd
      • ioctl(FICLONE) 失败时关闭 src_fd/dest_fd 后再报错
    • copyFile():
      • dest_fd 打开失败时关闭 src_fd
      • read()/write() 失败时释放 buffer 并关闭 src_fd/dest_fd
    • copyFileByRange():
      • dest_fd 打开失败时关闭 src_fd
      • copy_file_range() 失败时关闭 src_fd/dest_fd
    • 所有新增 cleanup 分支均保留 errno 语义,避免影响 %m 输出
  • 示例(错误路径清理模式)

    if (fgets(cmd_output, sizeof(cmd_output), output) == NULL)
    {
        int save_errno = errno;
    
        pclose(output);
        errno = save_errno;
        pg_fatal("could not get pg_ctl version data using %s: %m", cmd);
    }

📱 Kick off Copilot coding agent tasks wherever you are with GitHub Mobile, available on iOS and Android.

@cr-gpt
Copy link
Copy Markdown

cr-gpt Bot commented Mar 18, 2026

Seems you are using me but didn't get OPENAI_API_KEY seted in Variables/Secrets for this repo. you could follow readme for more information

Co-authored-by: yjhjstz <3832082+yjhjstz@users.noreply.github.com>
@cr-gpt
Copy link
Copy Markdown

cr-gpt Bot commented Mar 18, 2026

Seems you are using me but didn't get OPENAI_API_KEY seted in Variables/Secrets for this repo. you could follow readme for more information

Copilot AI changed the title [WIP] Fix fd leak and memory leak in code Close fd/memory leak paths in pg_upgrade file/version probing Mar 18, 2026
Copilot AI requested a review from yjhjstz March 18, 2026 22:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants