strncat Wformat-overflow warning when using gcc 8.2.1












0















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?










share|improve this question




















  • 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













  • 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


















0















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?










share|improve this question




















  • 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













  • 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
















0












0








0








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?










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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
















  • 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













  • 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














3 Answers
3






active

oldest

votes


















2














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.






share|improve this answer































    1














    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.






    share|improve this answer

































      0














      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.






      share|improve this answer























        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
        });


        }
        });














        draft saved

        draft discarded


















        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









        2














        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.






        share|improve this answer




























          2














          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.






          share|improve this answer


























            2












            2








            2







            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.






            share|improve this answer













            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.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 21 '18 at 9:34









            HolyBlackCatHolyBlackCat

            16.7k33468




            16.7k33468

























                1














                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.






                share|improve this answer






























                  1














                  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.






                  share|improve this answer




























                    1












                    1








                    1







                    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.






                    share|improve this answer















                    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.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 21 '18 at 9:30

























                    answered Nov 21 '18 at 9:25









                    SwordfishSwordfish

                    1




                    1























                        0














                        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.






                        share|improve this answer




























                          0














                          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.






                          share|improve this answer


























                            0












                            0








                            0







                            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.






                            share|improve this answer













                            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.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 21 '18 at 9:36









                            Yomna KahwaYomna Kahwa

                            1




                            1






























                                draft saved

                                draft discarded




















































                                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.




                                draft saved


                                draft discarded














                                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





















































                                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







                                Popular posts from this blog

                                How to change which sound is reproduced for terminal bell?

                                Can I use Tabulator js library in my java Spring + Thymeleaf project?

                                Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents