strncat Wformat-overflow warning when using gcc 8.2.1
I'm using gcc 8.2.1 and trying to build this code:
std::string dir = "Documents";
char * _tempname = static_cast <char*> (malloc( dir.length() + 14));
strncpy (_tempname, dir.c_str(), dir.length()+1 );
strncat (_tempname, "/hellooXXXXXX", 13);
but it gives me this warning:
warning:
'char* strncat(char*, const char*, size_t)'
specified bound
13 equals source length [-Wstringop-overflow=]
After searching I found that it's an overflow problem to have the size_t equals the source length according to the discussion in this link but I couldn't understand why this is considered a problem and why this overflows the destination. And how could I remove this warning without changing my code?
c++ string gcc gcc-warning gcc8
add a comment |
I'm using gcc 8.2.1 and trying to build this code:
std::string dir = "Documents";
char * _tempname = static_cast <char*> (malloc( dir.length() + 14));
strncpy (_tempname, dir.c_str(), dir.length()+1 );
strncat (_tempname, "/hellooXXXXXX", 13);
but it gives me this warning:
warning:
'char* strncat(char*, const char*, size_t)'
specified bound
13 equals source length [-Wstringop-overflow=]
After searching I found that it's an overflow problem to have the size_t equals the source length according to the discussion in this link but I couldn't understand why this is considered a problem and why this overflows the destination. And how could I remove this warning without changing my code?
c++ string gcc gcc-warning gcc8
2
strncat
expects a C-string as a first argument, but you're passing some uninitialized char array to it. Also why you're allocating memory manually when you could simply dostd::string tempname = dir + "/hellooXXXXXX"
?
– HolyBlackCat
Nov 21 '18 at 9:12
I forgot a line of code and I edited it now sorry, regarding the code why I'm not using string, actually I'm maintaining this code not writing it from scratch.
– Amr Essam
Nov 21 '18 at 9:16
add a comment |
I'm using gcc 8.2.1 and trying to build this code:
std::string dir = "Documents";
char * _tempname = static_cast <char*> (malloc( dir.length() + 14));
strncpy (_tempname, dir.c_str(), dir.length()+1 );
strncat (_tempname, "/hellooXXXXXX", 13);
but it gives me this warning:
warning:
'char* strncat(char*, const char*, size_t)'
specified bound
13 equals source length [-Wstringop-overflow=]
After searching I found that it's an overflow problem to have the size_t equals the source length according to the discussion in this link but I couldn't understand why this is considered a problem and why this overflows the destination. And how could I remove this warning without changing my code?
c++ string gcc gcc-warning gcc8
I'm using gcc 8.2.1 and trying to build this code:
std::string dir = "Documents";
char * _tempname = static_cast <char*> (malloc( dir.length() + 14));
strncpy (_tempname, dir.c_str(), dir.length()+1 );
strncat (_tempname, "/hellooXXXXXX", 13);
but it gives me this warning:
warning:
'char* strncat(char*, const char*, size_t)'
specified bound
13 equals source length [-Wstringop-overflow=]
After searching I found that it's an overflow problem to have the size_t equals the source length according to the discussion in this link but I couldn't understand why this is considered a problem and why this overflows the destination. And how could I remove this warning without changing my code?
c++ string gcc gcc-warning gcc8
c++ string gcc gcc-warning gcc8
edited Nov 21 '18 at 9:24
Rai
9441722
9441722
asked Nov 21 '18 at 9:07
Amr EssamAmr Essam
83
83
2
strncat
expects a C-string as a first argument, but you're passing some uninitialized char array to it. Also why you're allocating memory manually when you could simply dostd::string tempname = dir + "/hellooXXXXXX"
?
– HolyBlackCat
Nov 21 '18 at 9:12
I forgot a line of code and I edited it now sorry, regarding the code why I'm not using string, actually I'm maintaining this code not writing it from scratch.
– Amr Essam
Nov 21 '18 at 9:16
add a comment |
2
strncat
expects a C-string as a first argument, but you're passing some uninitialized char array to it. Also why you're allocating memory manually when you could simply dostd::string tempname = dir + "/hellooXXXXXX"
?
– HolyBlackCat
Nov 21 '18 at 9:12
I forgot a line of code and I edited it now sorry, regarding the code why I'm not using string, actually I'm maintaining this code not writing it from scratch.
– Amr Essam
Nov 21 '18 at 9:16
2
2
strncat
expects a C-string as a first argument, but you're passing some uninitialized char array to it. Also why you're allocating memory manually when you could simply do std::string tempname = dir + "/hellooXXXXXX"
?– HolyBlackCat
Nov 21 '18 at 9:12
strncat
expects a C-string as a first argument, but you're passing some uninitialized char array to it. Also why you're allocating memory manually when you could simply do std::string tempname = dir + "/hellooXXXXXX"
?– HolyBlackCat
Nov 21 '18 at 9:12
I forgot a line of code and I edited it now sorry, regarding the code why I'm not using string, actually I'm maintaining this code not writing it from scratch.
– Amr Essam
Nov 21 '18 at 9:16
I forgot a line of code and I edited it now sorry, regarding the code why I'm not using string, actually I'm maintaining this code not writing it from scratch.
– Amr Essam
Nov 21 '18 at 9:16
add a comment |
3 Answers
3
active
oldest
votes
Apparently GCC understands that strncat(_tempname, "/hellooXXXXXX", 13);
is no different from strcat(_tempname, "/hellooXXXXXX");
, and finds it suspicious that you're using former instead of the latter.
If you can change the code, use strcat
instead (or even better, rewrite the thing to use std::string
).
If you can't change the code, use -Wno-stringop-overflow
flag to disable the warning.
add a comment |
The function expects the space left in the destination not the length of the source string, so use
// ...
strncat(_tempname, "/hellooXXXXXX", dir.length() + 14 - strlen(_tempname) - 1);`
instead. No, forget that. Use std::string
instead.
add a comment |
my understanding is that gcc issues this warning only because it's a common mistake for users to set the bound equal to the src length, nothing more.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53408543%2fstrncat-wformat-overflow-warning-when-using-gcc-8-2-1%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Apparently GCC understands that strncat(_tempname, "/hellooXXXXXX", 13);
is no different from strcat(_tempname, "/hellooXXXXXX");
, and finds it suspicious that you're using former instead of the latter.
If you can change the code, use strcat
instead (or even better, rewrite the thing to use std::string
).
If you can't change the code, use -Wno-stringop-overflow
flag to disable the warning.
add a comment |
Apparently GCC understands that strncat(_tempname, "/hellooXXXXXX", 13);
is no different from strcat(_tempname, "/hellooXXXXXX");
, and finds it suspicious that you're using former instead of the latter.
If you can change the code, use strcat
instead (or even better, rewrite the thing to use std::string
).
If you can't change the code, use -Wno-stringop-overflow
flag to disable the warning.
add a comment |
Apparently GCC understands that strncat(_tempname, "/hellooXXXXXX", 13);
is no different from strcat(_tempname, "/hellooXXXXXX");
, and finds it suspicious that you're using former instead of the latter.
If you can change the code, use strcat
instead (or even better, rewrite the thing to use std::string
).
If you can't change the code, use -Wno-stringop-overflow
flag to disable the warning.
Apparently GCC understands that strncat(_tempname, "/hellooXXXXXX", 13);
is no different from strcat(_tempname, "/hellooXXXXXX");
, and finds it suspicious that you're using former instead of the latter.
If you can change the code, use strcat
instead (or even better, rewrite the thing to use std::string
).
If you can't change the code, use -Wno-stringop-overflow
flag to disable the warning.
answered Nov 21 '18 at 9:34
HolyBlackCatHolyBlackCat
16.7k33468
16.7k33468
add a comment |
add a comment |
The function expects the space left in the destination not the length of the source string, so use
// ...
strncat(_tempname, "/hellooXXXXXX", dir.length() + 14 - strlen(_tempname) - 1);`
instead. No, forget that. Use std::string
instead.
add a comment |
The function expects the space left in the destination not the length of the source string, so use
// ...
strncat(_tempname, "/hellooXXXXXX", dir.length() + 14 - strlen(_tempname) - 1);`
instead. No, forget that. Use std::string
instead.
add a comment |
The function expects the space left in the destination not the length of the source string, so use
// ...
strncat(_tempname, "/hellooXXXXXX", dir.length() + 14 - strlen(_tempname) - 1);`
instead. No, forget that. Use std::string
instead.
The function expects the space left in the destination not the length of the source string, so use
// ...
strncat(_tempname, "/hellooXXXXXX", dir.length() + 14 - strlen(_tempname) - 1);`
instead. No, forget that. Use std::string
instead.
edited Nov 21 '18 at 9:30
answered Nov 21 '18 at 9:25
SwordfishSwordfish
1
1
add a comment |
add a comment |
my understanding is that gcc issues this warning only because it's a common mistake for users to set the bound equal to the src length, nothing more.
add a comment |
my understanding is that gcc issues this warning only because it's a common mistake for users to set the bound equal to the src length, nothing more.
add a comment |
my understanding is that gcc issues this warning only because it's a common mistake for users to set the bound equal to the src length, nothing more.
my understanding is that gcc issues this warning only because it's a common mistake for users to set the bound equal to the src length, nothing more.
answered Nov 21 '18 at 9:36
Yomna KahwaYomna Kahwa
1
1
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53408543%2fstrncat-wformat-overflow-warning-when-using-gcc-8-2-1%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
strncat
expects a C-string as a first argument, but you're passing some uninitialized char array to it. Also why you're allocating memory manually when you could simply dostd::string tempname = dir + "/hellooXXXXXX"
?– HolyBlackCat
Nov 21 '18 at 9:12
I forgot a line of code and I edited it now sorry, regarding the code why I'm not using string, actually I'm maintaining this code not writing it from scratch.
– Amr Essam
Nov 21 '18 at 9:16