Skip to content

HTTPサーバーを作ってみる#20

Open
hiroki-horiguchi-dev wants to merge 1 commit into
mainfrom
socket-programming
Open

HTTPサーバーを作ってみる#20
hiroki-horiguchi-dev wants to merge 1 commit into
mainfrom
socket-programming

Conversation

@hiroki-horiguchi-dev
Copy link
Copy Markdown
Owner

@hiroki-horiguchi-dev hiroki-horiguchi-dev commented May 24, 2026

CSZAP 4th 課題

@hiroki-horiguchi-dev hiroki-horiguchi-dev self-assigned this May 24, 2026
int sfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (sfd < 0) { perror("socket"); return 1; }

if (connect(sfd, res->ai_addr, res->ai_addrlen) < 0) { perror("connect"); return 1; }
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

失敗した場合に sfd を close していない点が気になりました。

if (rv != 0) { fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv)); return 1; }

int sfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (sfd < 0) { perror("socket"); return 1; }
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

失敗した場合に res を freeaddrinfo() していない点が気になりました。関数の末尾に、リソースが確保されている場合は解放するコードを書き、途中で失敗した場合は、そこまで goto で飛ばす、という書き方を時々見かけます。賛否はあるものの、参考まで。

ssize_t total = 0;
while (total < BUF_SIZE - 1) {
ssize_t n = read(fd, req + total, BUF_SIZE - 1 - total);
if (n < 0) { perror("read"); free(req); return; }
Copy link
Copy Markdown

@nodchip nodchip May 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

個人的には、生でメモリを確保し、解放する際は、解放したあとにポインターに NULL を代入します。理由は、悪意ある者が、メモリのダンプを覗いたとき、ポインターの値から、解放済みのメモリ領域を覗き、価値のあるデータを盗むのを防ぎたいためです。趣味の範囲かもしれません。

@hiroki-horiguchi-dev
Copy link
Copy Markdown
Owner Author

@nodchip
コメントいただきありがとうございます、今晩に確認いたします!

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