This is an archive of the discontinued LLVM Phabricator instance.

[NFC][AIX][XCOFF] Fix compile warning on strncpy
ClosedPublic

Authored by nullptr.cpp on Jan 16 2021, 8:40 PM.

Details

Summary

GCC warning:

In file included from /usr/include/string.h:495,
                 from /usr/include/c++/9/cstring:42,
                 from /llvm-project/llvm/include/llvm/ADT/Hashing.h:53,
                 from /llvm-project/llvm/include/llvm/ADT/ArrayRef.h:12,
                 from /llvm-project/llvm/include/llvm/MC/MCAsmBackend.h:12,
                 from /llvm-project/llvm/lib/MC/XCOFFObjectWriter.cpp:14:
In function ‘char* strncpy(char*, const char*, size_t)’,
    inlined from ‘{anonymous}::Section::Section(const char*, llvm::XCOFF::SectionTypeFlags, bool, {anonymous}::CsectGroups)’ at /llvm-project/llvm/lib/MC/XCOFFObjectWriter.cpp:146:12:
/usr/include/x86_64-linux-gnu/bits/string_fortified.h:106:34: warning: ‘char* __builtin_strncpy(char*, const char*, long unsigned int)’ specified bound 8 equals destination size [-Wstringop-truncation]
  106 |   return __builtin___strncpy_chk (__dest, __src, __len, __bos (__dest));
      |          ~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
         ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Diff Detail

Event Timeline

nullptr.cpp created this revision.Jan 16 2021, 8:40 PM
nullptr.cpp requested review of this revision.Jan 16 2021, 8:40 PM
Herald added a project: Restricted Project. · View Herald TranscriptJan 16 2021, 8:40 PM
hubert.reinterpretcast added inline comments.
llvm/lib/MC/XCOFFObjectWriter.cpp
102

D71119 changed the type of a local variable as a concession to a false-positive compiler warning (the NUL termination is not required for a name with NameSize characters).

Changing the interface of a public member of a class is a more intrusive change. Presumably, we can keep the type of Name here as-is and use memcpy to populate Name after using strncpy in the constructor.

nullptr.cpp edited the summary of this revision. (Show Details)

Use memcpy

daltenty added inline comments.Jan 18 2021, 9:32 AM
llvm/lib/MC/XCOFFObjectWriter.cpp
147

It's not clear this is properly null terminated in the other cases. Perhaps we should zero-initialize this field first (especially since we're going to write out the bytes directly)?

nullptr.cpp added inline comments.Jan 18 2021, 3:33 PM
llvm/lib/MC/XCOFFObjectWriter.cpp
143–147

@daltenty
Name() here have already zero-initialized the array.

llvm/lib/MC/XCOFFObjectWriter.cpp
146

I think N did not need to be a null-terminated string (if 8 characters long) before the change. Would be make sense to change N to be a StringRef and use the length from that? I think that makes it clear that just passing the argument either requires an explicit length or a terminating NUL.

Thanks for adding a check here to catch improper usage.

Use StringRef

This revision is now accepted and ready to land.Jan 18 2021, 7:54 PM
This revision was automatically updated to reflect the committed changes.