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

staging: fix crashes when some external APIs fail #186

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

staging: fix crashes when some external APIs fail #186

wants to merge 5 commits into from

Conversation

ZhouyangJia
Copy link
Contributor

Some potential API bugs may cause crashes, which are mainly caused by insufficient error handling.
This patch fixes these reported potential bugs by adding error handling code.

Signed-off-by: Zhouyang Jia [email protected]

When bind fails, print error messages and return.
When fopen fails, print error message and return.
When the external APIs fail, print error message.
Copy link
Member

@tklauser tklauser left a comment

Choose a reason for hiding this comment

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

Please add a Signed-off-by line to your commit message, see the file SubmittingPatches for details.

Please also prefix the subject of the commit (i.e. the first line) with "mausezahn:".

Copy link
Member

@tklauser tklauser left a comment

Choose a reason for hiding this comment

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

Please add a Signed-off-by line to your commit message, see the file SubmittingPatches for details.

Please also prefix the subject of the commit (i.e. the first line) with "mausezahn:".

@@ -311,7 +311,11 @@ int get_dev_params (char *name)
psock.sll_hatype = 0; // unsigned short - Header type //ARPHRD_ETHER
psock.sll_pkttype = 0; // unsigned char - Packet type
psock.sll_halen = 6; // unsigned char - Length of address
bind(ps, (const struct sockaddr *) &psock, sizeof(psock)); // <= !!!
if (bind(ps, (const struct sockaddr *) &psock, sizeof(psock))==-1) { // <= !!!
Copy link
Member

Choose a reason for hiding this comment

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

Please add spaces around " == " and get rid of the trailing comment.

@@ -228,7 +228,11 @@ int get_dev_params (char *name)

// 2. find network, gateway, and mask

fd = fopen("/proc/net/route", "r");
fd = fopen("/proc/net/route", "r");
if (fd==NULL) {
Copy link
Member

Choose a reason for hiding this comment

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

Please add spaces around "==".

Copy link
Member

@tklauser tklauser left a comment

Choose a reason for hiding this comment

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

Please add a Signed-off-by line to your commit message, see the file SubmittingPatches for details.

Please also prefix the subject of the commit (i.e. the first line) with "mausezahn:".

staging/cli.c Outdated
@@ -525,6 +525,9 @@ int cli(void)

// Create a socket
s = socket(AF_INET, SOCK_STREAM, 0);
if (s==-1) {
Copy link
Member

Choose a reason for hiding this comment

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

Spaces around "==" please.

staging/cli.c Outdated
@@ -525,6 +525,9 @@ int cli(void)

// Create a socket
s = socket(AF_INET, SOCK_STREAM, 0);
if (s==-1) {
perror("socket");
Copy link
Member

Choose a reason for hiding this comment

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

I think we should return here instead of keep on working on the invalid socket s further down.

staging/cli.c Outdated
@@ -525,6 +525,9 @@ int cli(void)

// Create a socket
s = socket(AF_INET, SOCK_STREAM, 0);
if (s==-1) {
perror("socket");
}
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
Copy link
Member

Choose a reason for hiding this comment

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

setsockopt can also fail, so it's return value should be checked as well.

staging/cli.c Outdated
@@ -541,14 +544,18 @@ int cli(void)
servaddr.sin_family = AF_INET;
inet_aton(mz_listen_addr, &servaddr.sin_addr);
servaddr.sin_port = htons(mz_port);
bind(s, (struct sockaddr *)&servaddr, sizeof(servaddr));
if (bind(s, (struct sockaddr *)&servaddr, sizeof(servaddr))==-1) {
Copy link
Member

Choose a reason for hiding this comment

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

Spaces around "==" please.

staging/cli.c Outdated
@@ -541,14 +544,18 @@ int cli(void)
servaddr.sin_family = AF_INET;
inet_aton(mz_listen_addr, &servaddr.sin_addr);
servaddr.sin_port = htons(mz_port);
bind(s, (struct sockaddr *)&servaddr, sizeof(servaddr));
if (bind(s, (struct sockaddr *)&servaddr, sizeof(servaddr))==-1) {
perror("bind");
Copy link
Member

Choose a reason for hiding this comment

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

I think we should return here in case of an error.

staging/cli.c Outdated

if (!quiet) {
fprintf(stderr, "Mausezahn accepts incoming Telnet connections on %s:%i.\n", mz_listen_addr, mz_port);
}

// Wait for a connection
listen(s, 50);
if (listen(s, 50)==-1) {
Copy link
Member

Choose a reason for hiding this comment

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

Spaces around "==", plase.

staging/cli.c Outdated

if (!quiet) {
fprintf(stderr, "Mausezahn accepts incoming Telnet connections on %s:%i.\n", mz_listen_addr, mz_port);
}

// Wait for a connection
listen(s, 50);
if (listen(s, 50)==-1) {
perror("listen");
Copy link
Member

Choose a reason for hiding this comment

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

I think we should return here in case of an error.

When the external APIs fail, print error message.

Signed-off-by: Zhouyang Jia [email protected]
When the APIs fail, check their return value and return.

Signed-off-by: Zhouyang Jia [email protected]
@tklauser
Copy link
Member

tklauser commented Mar 8, 2018

Your original commits (lacking the Signed-off-by lines) still appear in the PR. Please squash them with the following commits. I think they can all actually be in one single commit as it all deals with the same issue.

staging/cli.c Outdated
@@ -525,10 +525,12 @@ int cli(void)

// Create a socket
s = socket(AF_INET, SOCK_STREAM, 0);
if (s==-1) {
perror("socket");
Copy link
Member

@tklauser tklauser Mar 8, 2018

Choose a reason for hiding this comment

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

The perror call can stay here (also in the changes below). Please also check the return value of the cli function in the caller:

You could also add an error message like "failed to create socket for mausezahn CLI" and return an accordingly (which is then again handled in the caller.

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