Skip to content

Fixed?#5

Merged
ndev2 merged 1 commit intomasterfrom
bug_fix
Nov 15, 2017
Merged

Fixed?#5
ndev2 merged 1 commit intomasterfrom
bug_fix

Conversation

@ndev2
Copy link
Copy Markdown
Owner

@ndev2 ndev2 commented Nov 10, 2017

Bug fix for malformed rate strings

@ndev2 ndev2 force-pushed the bug_fix branch 5 times, most recently from 1841506 to f24c3d1 Compare November 14, 2017 18:20
tc/tc_util.c Outdated
#include <arpa/inet.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I don't think we need this include anymore.

tc/tc_util.c Outdated
long max_rate_bits;
int ret;
double perc, rate_bits;
char *char_perc, *p;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Let's change char_perc to str_perc to more accurately reflect what it represents.

tc/tc_util.c Outdated

if (perc > 100.0 || perc < 0.0) {

if (perc > 100.0 || perc < 0.0 || errno == ERANGE) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This check for errno == ERANGE is for strtod(), but we call free() between the call to strtod() and here. It's possible that in some implementations free() could set errno.

So let's do the error checking for strtod() in this way:

int saved_errno;

...

perc = strtod(char_perc, &p);
saved_errno = errno;
free(str_perc);

/* Make sure there's only one percent sign and it's at the end. */
if (*p != '%' || *(p++) != '\0')
	goto malf;
if (perc > 100.0 || perc < 0.0 || saved_errno == ERANGE) {
	fprintf(stderr, "Invalid rate specified; should be between [0,100]%% but is %s\n", str);
	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.

This should catch any cases where there are multiple percentage signs, or a percentage sign that's not at the end. Please test these cases.

Copy link
Copy Markdown
Owner Author

@ndev2 ndev2 Nov 14, 2017

Choose a reason for hiding this comment

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

Hey Cody, *p is pointing to char_perc which is allocated a new buffer, so if we free char_perc, and then check for *p, it doesn't lead anywhere (I ran into this issue when I was testing it out), but yes, I'll check for double percentage signs, thanks.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Actually, the current code catches double percentage signs as I'm testing it right now, I guess strtod() set *p to where it stopped converting the string to double, and so, the other (*p++) != '\0' condition catches double percentage signs

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Right, sorry, I should have been clearer -- the code you wrote before already did that, I just added the comment to make it clear what it was doing.

But we definitely need to save errno in a variable so that we can use it after. Doing so also simplifies how we can can call free(str_perc). So please add those parts in as well, following the suggested code that I wrote above.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Right I see, thanks, will make the changes now

@ndev2
Copy link
Copy Markdown
Owner Author

ndev2 commented Nov 14, 2017

Hi Cody, I've made the necessary changes and it looks like the code does handle double percentage signs by showing errors, so I didn't change that part. Do let me know what you think, thanks.

@ndev2
Copy link
Copy Markdown
Owner Author

ndev2 commented Nov 14, 2017

Hi Cody, made the changes, hope the code is good, thanks

tc/tc_util.c Outdated
saved_errno = errno;
free(str_perc);


Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Just remove one of these extra blank lines and I think this patch will be good to go.

@cjdoucette
Copy link
Copy Markdown

Once you've fixed the above comment, I think you can squash these commits back down to just one and add go back to the mailing list.

@ndev2 ndev2 merged commit d89db45 into master Nov 15, 2017
@ndev2 ndev2 deleted the bug_fix branch November 30, 2017 02:44
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