bugGNU nano - Bugs: bug #57360, GCC -Wstringop-truncation warnings


You are not allowed to post comments on this tracker with your current authentication level.

bug #57360: GCC -Wstringop-truncation warnings

Submitted by:  Brand Huntsman <brand>
Submitted on:  Thu 05 Dec 2019 04:57:17 AM UTC
Severity:  2 - Minor Status:  Fixed
Assigned to:  bens Open/Closed:  Closed
* Mandatory Fields

Add a New Comment (Rich Markup)

Comment Type & Canned Response:


( Jump to the original submission )

Sun 08 Dec 2019 10:28:30 AM UTC, comment #8: 

Thanks for confirming.  Pushed to master, commit 76d90617.

Benno Schulenberg <bens>
Project Administrator
Fri 06 Dec 2019 08:49:21 PM UTC, comment #7: 

That patch fixed all the warnings.

Brand Huntsman <brand>
Fri 06 Dec 2019 11:38:20 AM UTC, comment #6: 

Does copying the full 32 bytes of the myhostname array avoid the warning?  That is, does the attached patch avoid all three warnings?

(file #48017)

Benno Schulenberg <bens>
Project Administrator
Thu 05 Dec 2019 07:45:32 PM UTC, comment #5: 

The strncpy for mypwuid->pw_name doesn't warn because it has no statically known size. And always setting myhostname[31] to \0 doesn't help.

Brand Huntsman <brand>
Thu 05 Dec 2019 12:35:48 PM UTC, comment #4: 

As a test, I've replaced the relevant strncpy() with a memcpy(), and have seeded char myhostname[32] with "tralalalalalalalalalalalalalala".  Then I've run nano with --locking and then opened the same file in another terminal with vim.  Vim correctly shows the hostname of my laptop, without any trailing junk.  So I think it's safe to use memcpy() here.

Benno Schulenberg <bens>
Project Administrator
Thu 05 Dec 2019 11:40:27 AM UTC, comment #3: 

Copying any garbage after the \0 byte would probably not be a problem.  But... maybe we should always set myhostname[31] = '\0', not only when errno == ENAMETOOLONG; maybe that gets rid of the warning?  Because, why does the compiler not complain for the two neigbouring strncpy() statements?

Benno Schulenberg <bens>
Project Administrator
Thu 05 Dec 2019 11:12:30 AM UTC, comment #2: 

-Wno-stringop-truncation works but that would suppress actual problems as well.

Changing the strncpy to memcpy gets rid of the warnings, but might not be correct for the first warning. strncpy stops at the \0 but memcpy copies everything. Would it matter if the garbage after \0 in myhostname was copied to lockdata?

Brand Huntsman <brand>
Thu 05 Dec 2019 10:44:27 AM UTC, comment #1: 

(My compiler does not yet know -Wstringop-truncation, so I cannot reproduce.  It only knows -Wstringop-overflow, but even setting this to 4 does not produce any warnings.)

Can we somehow suppress these warnings?  Does adding "-Wno-stringop-truncation" to your CFLAGS work?  Or can we somehow mark these three specific calls to signal to GCC that they are fine?

Benno Schulenberg <bens>
Project Administrator
Thu 05 Dec 2019 04:57:17 AM UTC, original submission:  

GCC 9.2 or Gentoo's version now has -Wstringop-truncation enabled which throws three warnings when compiling nano. I don't see anything wrong with the code. The warnings have been cleaned up for readability.

in files.c write_lockfile() line 247
    strncpy(&lockdata[68], myhostname, 31);
/usr/include/bits/string_fortified.h:106:10: warning: '__builtin_strncpy' output may be truncated copying 31 bytes from a string of length 31 [-Wstringop-truncation]

in text.c indent_a_line() line 101
    strncpy(line->data, indentation, indent_len);
/usr/include/bits/string_fortified.h:106:10: warning: '__builtin_strncpy' output truncated before terminating nul copying as many bytes from a string as its length [-Wstringop-truncation]

in nano.c unpartition_buffer() line 258
    strncpy(openfile->filetop->data, antedata, strlen(antedata));
/usr/include/bits/string_fortified.h:106:10: warning: '__builtin_strncpy' output truncated before terminating nul copying as many bytes from a string as its length [-Wstringop-truncation]

Brand Huntsman <brand>


(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:

Attached Files


Depends on the following items: None found

Items that depend on this one: None found


Carbon-Copy List
  • -email is unavailable- added by bens (Posted a comment)
  • -email is unavailable- added by brand (Submitted the item)

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only logged-in users can vote.





    Follow 6 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2019-12-24 bens Open/ClosedOpen => Closed
    2019-12-08 bens StatusIn Progress => Fixed
    2019-12-06 bens Attached File- => Added 0001-build-avoid-three-compiler-warnings.patch, #48017
        StatusNone => In Progress
        Assigned toNone => bens
    2019-12-05 bens Severity3 - Normal => 2 - Minor

    Back to the top

    Powered by Savane 3.9