HTTPサーバーを作ってみる#20
Open
hiroki-horiguchi-dev wants to merge 1 commit into
Open
Conversation
nodchip
reviewed
May 26, 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; } |
| 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; } |
There was a problem hiding this comment.
失敗した場合に res を freeaddrinfo() していない点が気になりました。関数の末尾に、リソースが確保されている場合は解放するコードを書き、途中で失敗した場合は、そこまで goto で飛ばす、という書き方を時々見かけます。賛否はあるものの、参考まで。
nodchip
reviewed
May 26, 2026
| 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; } |
There was a problem hiding this comment.
個人的には、生でメモリを確保し、解放する際は、解放したあとにポインターに NULL を代入します。理由は、悪意ある者が、メモリのダンプを覗いたとき、ポインターの値から、解放済みのメモリ領域を覗き、価値のあるデータを盗むのを防ぎたいためです。趣味の範囲かもしれません。
Owner
Author
|
@nodchip |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
CSZAP 4th 課題