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

allocate space for buf on heap #1324

Closed
wants to merge 1 commit into from
Closed

allocate space for buf on heap #1324

wants to merge 1 commit into from

Conversation

kraj
Copy link

@kraj kraj commented Dec 25, 2019

Avoids
| src/epggrab/module/xmltv.c:204:47: error: '%s' directive output may be truncated writing between 2 and 2147483645 bytes into a region of size 115 [-Werror=format-truncation=]
| 204 | snprintf(buf, sizeof(buf)-1, "ddprogid://%s/%s", mod->id, s);
| | ^~

Upstream-Status: Pending
Signed-off-by: Khem Raj raj.khem@gmail.com

Avoids
| src/epggrab/module/xmltv.c:204:47: error: '%s' directive output may be truncated writing between 2 and 2147483645 bytes into a region of size 115 [-Werror=format-truncation=]
|   204 |   snprintf(buf, sizeof(buf)-1, "ddprogid://%s/%s", mod->id, s);
|       |                                               ^~

Upstream-Status: Pending
Signed-off-by: Khem Raj <raj.khem@gmail.com>
if (strlen(s) < 2) return;

char* buf = (char *)malloc(strlen(s) + strlen(mod->id) + 13);
buf[strlen(s) + strlen(mod->id) + 12] = '\0';
Copy link

@yafiyogi yafiyogi Jan 23, 2020

Choose a reason for hiding this comment

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

There are a few things that can be improved in your change:

  1. There is no need to null terminate buf[] (line 203) when using snprintf(), as it always null terminates.
  2. Line 215 can be removed as snprintf() returns the number of characters put into buf[] (excluding the training '\0').
  3. There are multiple calls to strlen() on the same strings: strlen(s) happens three times & strlen(mod->id) happens twice.

Consider doing this:

const int s_len = strlen(s);
if( s_len < 2) return;

const int buf_size = s_len + strlen(mod->id) + 13;
char * buf = (char *) malloc( buf_size);
/* Raw URI */
int e = snprintf( buf, buf_size, "ddprogid://%s/%s", mod->id, s);

...and then lines 215 & 216 become:

 while( --e && buf[e] != '.') {}

Copy link
Member

Choose a reason for hiding this comment

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

@yafiyogi Not sure if you didn't catch this one or if it was a wanted decision: If e is the length of the string, buf[e] would be the null/terminating char, so we would do an unnecessary comparison there. We could start with e-1 directly and do one less comparison in 215/216.

Choose a reason for hiding this comment

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

@Flole998 Yes, by having int e = snprintf( buf, buf_size, "ddprogid://%s/%s", mod->id, s) - 1;, it means means line 215 can be removed & 216 can remain the same, which is in general a good thing.

I don't like having '- 1' at the end of a longish line. It can get overlooked when looking through code.
My code avoids a comparison with the null terminator by doing --e before comparing buf[e] == '.'.

Copy link
Member

Choose a reason for hiding this comment

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

Ah yeah now I see it ;) That was one of my first thoughts aswell but I then thought it could get overlooked easily, and apparently that is true. Anyways, I like that solution and if this PR doesn't get updated I will put your solution in when I have time to do so (unless you want to make it a PR which I would instantly merge ;) ).

/* Raw URI */
snprintf(buf, sizeof(buf)-1, "ddprogid://%s/%s", mod->id, s);
snprintf(buf, strlen(s) + strlen(mod->id) + 12, "ddprogid://%s/%s", mod->id, s);

Choose a reason for hiding this comment

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

Here, by using + 12 rather than + 13, the last character of string s will not be copied to buf[]. snprintf() copies size - 1 characters an then null terminates. This appears to be a bug.

Copy link
Member

@Flole998 Flole998 left a comment

Choose a reason for hiding this comment

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

I agree with @yafiyogi here.

@kraj Could you please address those comments?

@Flole998
Copy link
Member

Flole998 commented Jun 9, 2020

Changes are in, thanks @yafiyogi for your help!

@Flole998 Flole998 closed this Jun 9, 2020
@yafiyogi
Copy link

@Flole998 you're welcome.

dave-p pushed a commit to dave-p/tvheadend that referenced this pull request Feb 6, 2024
dave-p pushed a commit to dave-p/tvheadend that referenced this pull request Feb 6, 2024
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.

None yet

3 participants