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

Fix compiler warning in call_readline() #10820

Merged
merged 1 commit into from
Nov 30, 2018
Merged

Fix compiler warning in call_readline() #10820

merged 1 commit into from
Nov 30, 2018

Conversation

vstinner
Copy link
Member

Replace strncpy() with memcpy() in call_readline() to fix the
following warning, the NUL byte is written manually just after:

Modules/readline.c: In function ‘call_readline’:
Modules/readline.c:1303:9: warning: ‘strncpy’ output truncated before
terminating nul copying as many bytes from a string as its length
[-Wstringop-truncation]
strncpy(p, q, n);
^~~~~~~~~~~~~~~~
Modules/readline.c:1279:9: note: length computed here
n = strlen(p);
^~~~~~~~~

Replace strncpy() with memcpy() in call_readline() to fix the
following warning, the NUL byte is written manually just after:

Modules/readline.c: In function ‘call_readline’:
Modules/readline.c:1303:9: warning: ‘strncpy’ output truncated before
terminating nul copying as many bytes from a string as its length
[-Wstringop-truncation]
         strncpy(p, q, n);
         ^~~~~~~~~~~~~~~~
Modules/readline.c:1279:9: note: length computed here
     n = strlen(p);
         ^~~~~~~~~
@vstinner
Copy link
Member Author

@serhiy-storchaka: Would you mind to double check this change? I don't want to introduce worse code when trying to fix a security issue/compiler warning :-)

@@ -1240,7 +1240,7 @@ static char *
call_readline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt)
{
size_t n;
char *p, *q;
char *p;
Copy link
Member

Choose a reason for hiding this comment

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

Why the declaration of q has been moved?

Copy link
Member Author

Choose a reason for hiding this comment

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

I like to move variable declaration closer to where they are used. That's now possible thanks to C99 (required by PEP 7 since Python 3.6). @methane and me are moving all variables one by one :-D But only when we already modify something else in the code.

IMHO the main benefit is to see more easily the scope of a variable. Sometimes, it helps to detect that a subfunction should be created.

@serhiy-storchaka
Copy link
Member

Using memcpy instead of strncpy here LGTM.

@vstinner vstinner merged commit 1600f60 into python:master Nov 30, 2018
@vstinner vstinner deleted the readline_strncpy branch November 30, 2018 14:03
vstinner added a commit that referenced this pull request Mar 20, 2019
Replace strncpy() with memcpy() in call_readline() to fix the
following warning, the NUL byte is written manually just after:

Modules/readline.c: In function ‘call_readline’:
Modules/readline.c:1303:9: warning: ‘strncpy’ output truncated before
terminating nul copying as many bytes from a string as its length
[-Wstringop-truncation]
         strncpy(p, q, n);
         ^~~~~~~~~~~~~~~~
Modules/readline.c:1279:9: note: length computed here
     n = strlen(p);
         ^~~~~~~~~

(cherry picked from commit 1600f60)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants