Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace hard-coded buffer size with sizeof() #27

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion chap02/time_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ int main() {

printf("Reading request...\n");
char request[1024];
int bytes_received = recv(socket_client, request, 1024, 0);
int bytes_received = recv(socket_client, request, sizeof(request), 0);
printf("Received %d bytes.\n", bytes_received);
//printf("%.*s", bytes_received, request);

Expand Down
2 changes: 1 addition & 1 deletion chap02/time_server_dual.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ int main() {

printf("Reading request...\n");
char request[1024];
int bytes_received = recv(socket_client, request, 1024, 0);
int bytes_received = recv(socket_client, request, sizeof(request), 0);
printf("Received %d bytes.\n", bytes_received);
//printf("%.*s", bytes_received, request);

Expand Down
2 changes: 1 addition & 1 deletion chap02/time_server_ipv6.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ int main() {

printf("Reading request...\n");
char request[1024];
int bytes_received = recv(socket_client, request, 1024, 0);
int bytes_received = recv(socket_client, request, sizeof(request), 0);
printf("Received %d bytes.\n", bytes_received);
//printf("%.*s", bytes_received, request);

Expand Down
4 changes: 2 additions & 2 deletions chap03/tcp_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ int main(int argc, char *argv[]) {

if (FD_ISSET(socket_peer, &reads)) {
char read[4096];
int bytes_received = recv(socket_peer, read, 4096, 0);
int bytes_received = recv(socket_peer, read, sizeof(read), 0);
if (bytes_received < 1) {
printf("Connection closed by peer.\n");
break;
Expand All @@ -120,7 +120,7 @@ int main(int argc, char *argv[]) {
if(FD_ISSET(0, &reads)) {
#endif
char read[4096];
if (!fgets(read, 4096, stdin)) break;
if (!fgets(read, sizeof(read), stdin)) break;
printf("Sending: %s", read);
int bytes_sent = send(socket_peer, read, strlen(read), 0);
printf("Sent %d bytes.\n", bytes_sent);
Expand Down
2 changes: 1 addition & 1 deletion chap03/tcp_serve_chat.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ int main() {

} else {
char read[1024];
int bytes_received = recv(i, read, 1024, 0);
int bytes_received = recv(i, read, sizeof(read), 0);
if (bytes_received < 1) {
FD_CLR(i, &master);
CLOSESOCKET(i);
Expand Down
2 changes: 1 addition & 1 deletion chap03/tcp_serve_toupper.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ int main() {

} else {
char read[1024];
int bytes_received = recv(i, read, 1024, 0);
int bytes_received = recv(i, read, sizeof(read), 0);
if (bytes_received < 1) {
FD_CLR(i, &master);
CLOSESOCKET(i);
Expand Down
2 changes: 1 addition & 1 deletion chap03/tcp_serve_toupper_fork.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ int main() {
CLOSESOCKET(socket_listen);
while(1) {
char read[1024];
int bytes_received = recv(socket_client, read, 1024, 0);
int bytes_received = recv(socket_client, read, sizeof(read), 0);
if (bytes_received < 1) {
CLOSESOCKET(socket_client);
exit(0);
Expand Down
4 changes: 2 additions & 2 deletions chap04/udp_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ int main(int argc, char *argv[]) {

if (FD_ISSET(socket_peer, &reads)) {
char read[4096];
int bytes_received = recv(socket_peer, read, 4096, 0);
int bytes_received = recv(socket_peer, read, sizeof(read), 0);
if (bytes_received < 1) {
printf("Connection closed by peer.\n");
break;
Expand All @@ -120,7 +120,7 @@ int main(int argc, char *argv[]) {
if(FD_ISSET(0, &reads)) {
#endif
char read[4096];
if (!fgets(read, 4096, stdin)) break;
if (!fgets(read, sizeof(read), stdin)) break;
printf("Sending: %s", read);
int bytes_sent = send(socket_peer, read, strlen(read), 0);
printf("Sent %d bytes.\n", bytes_sent);
Expand Down
2 changes: 1 addition & 1 deletion chap04/udp_recvfrom.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ int main() {
socklen_t client_len = sizeof(client_address);
char read[1024];
int bytes_received = recvfrom(socket_listen,
read, 1024,
read, sizeof(read),
0,
(struct sockaddr*) &client_address, &client_len);

Expand Down
2 changes: 1 addition & 1 deletion chap04/udp_serve_toupper.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ int main() {
socklen_t client_len = sizeof(client_address);

char read[1024];
int bytes_received = recvfrom(socket_listen, read, 1024, 0,
int bytes_received = recvfrom(socket_listen, read, sizeof(read), 0,
(struct sockaddr *)&client_address, &client_len);
if (bytes_received < 1) {
fprintf(stderr, "connection closed. (%d)\n",
Expand Down
2 changes: 1 addition & 1 deletion chap04/udp_serve_toupper_simple.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ int main() {
socklen_t client_len = sizeof(client_address);

char read[1024];
int bytes_received = recvfrom(socket_listen, read, 1024, 0,
int bytes_received = recvfrom(socket_listen, read, sizeof(read), 0,
(struct sockaddr *)&client_address, &client_len);
if (bytes_received < 1) {
fprintf(stderr, "connection closed. (%d)\n",
Expand Down
2 changes: 1 addition & 1 deletion chap05/dns_query.c
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ int main(int argc, char *argv[]) {

char read[1024];
int bytes_received = recvfrom(socket_peer,
read, 1024, 0, 0, 0);
read, sizeof(read), 0, 0, 0);

printf("Received %d bytes.\n", bytes_received);

Expand Down
2 changes: 1 addition & 1 deletion chap08/smtp_send.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ int main() {
timeinfo = gmtime(&timer);

char date[128];
strftime(date, 128, "%a, %d %b %Y %H:%M:%S +0000", timeinfo);
strftime(date, sizeof(date), "%a, %d %b %Y %H:%M:%S +0000", timeinfo);

send_format(server, "Date:%s\r\n", date);

Expand Down
4 changes: 2 additions & 2 deletions chap09/tls_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ int main(int argc, char *argv[]) {

if (FD_ISSET(socket_peer, &reads)) {
char read[4096];
int bytes_received = SSL_read(ssl, read, 4096);
int bytes_received = SSL_read(ssl, read, sizeof(read));
if (bytes_received < 1) {
int err;
if ((err = SSL_get_error(ssl, bytes_received)) &&
Expand All @@ -189,7 +189,7 @@ int main(int argc, char *argv[]) {
if(FD_ISSET(0, &reads)) {
#endif
char read[4096];
if (!fgets(read, 4096, stdin)) break;
if (!fgets(read, sizeof(read), stdin)) break;
printf("Sending: %s", read);
int bytes_sent = SSL_write(ssl, read, strlen(read));
printf("Sent %d bytes.\n", bytes_sent);
Expand Down
2 changes: 1 addition & 1 deletion chap10/tls_time_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ int main() {

printf("Reading request...\n");
char request[1024];
int bytes_received = SSL_read(ssl, request, 1024);
int bytes_received = SSL_read(ssl, request, sizeof(request));
printf("Received %d bytes.\n", bytes_received);
//printf("%.*s", bytes_received, request);

Expand Down
2 changes: 1 addition & 1 deletion chap13/error_text.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const char *get_error_text() {
static char message[256] = {0};
FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
0, WSAGetLastError(), 0, message, 256, 0);
0, WSAGetLastError(), 0, message, sizeof(message), 0);
char *nl = strrchr(message, '\n');
if (nl) *nl = 0;
return message;
Expand Down
2 changes: 1 addition & 1 deletion chap13/server_crash.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ int main() {
int bytes_received;
while(1) {
char read[1024];
bytes_received = recv(socket_client, read, 1024, 0);
bytes_received = recv(socket_client, read, sizeof(read), 0);
if (bytes_received < 1) {
break;
} else {
Expand Down