简体   繁体   English

gcc-8 Wstringop截断

[英]gcc-8 Wstringop-truncation

I'm trying to fix some C code where gcc-8 complains about Wstringop-truncation (code is here ) 我正在尝试修复gcc-8抱怨Wstringop-truncation的一些C代码(代码在这里

When compiling that code on a server which I can not control neither can add pragma statements nor can disable Wstringop-truncation diagnostics, the warning which I receive is: 在无法控制的服务器上编译该代码时,既不能添加编译指示也不能禁用Wstringop-truncation诊断,我收到的警告是:

gcc-8  -I"/home/hornik/tmp/R/include" -DNDEBUG -I./cqdb/include -I./crf/src -I./liblbfgs/include -I./include -I"/home/hornik/lib/R/Library/3.6/x86_64-linux-gnu/Rcpp/include" -I/usr/local/include   -fpic  -g -O2 -Wall -pedantic -mtune=native -c cqdb/src/cqdb.c -o cqdb/src/cqdb.o
cqdb/src/cqdb.c: In function ‘cqdb_writer_close’:
cqdb/src/cqdb.c:270:5: warning: ‘strncpy’ output truncated before terminating nul copying 4 bytes from a string of the same length [-Wstringop-truncation]
     strncpy((char*)header.chunkid, CHUNKID, 4);
     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cqdb/src/cqdb.c: In function ‘cqdb_reader’:
cqdb/src/cqdb.c:469:9: warning: ‘strncpy’ specified bound 4 equals destination size [-Wstringop-truncation]
         strncpy((char*)db->header.chunkid, (const char*)p, 4);
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I would like to rewrite the strncpy statements to remove these warnings. 我想重写strncpy语句以删除这些警告。 Am I right that I need to replace in the following lines 我对,我需要在以下几行中替换

strncpy((char*)header.chunkid, CHUNKID, 4);
with strncpy((char*)header.chunkid, CHUNKID, 5);

and strncpy((char*)db->header.chunkid, (const char*)p, 4);
with strncpy((char*)db->header.chunkid, (const char*)p, 5);

The relevant code in cqdb.c is put below. 下面是cqdb.c中的相关代码。 It basically checks if the file is of type 'CQDB'. 它基本上检查文件是否为“ CQDB”类型。 Mark that although I would really love to have access, I do not have access to this machine so I can not test out if the fixes to the C code will work. 标记为,尽管我真的很想获得访问权限,但我无权访问此计算机,因此无法测试对C代码的修复是否有效。

#define CHUNKID             "CQDB"

typedef struct {
    int8_t      chunkid[4]; /**< Chunk identifier, "CQDB". */
    uint32_t    size;       /**< Chunk size including this header. */
    uint32_t    flag;       /**< Global flags. */
    uint32_t    byteorder;  /**< Byte-order indicator. */
    uint32_t    bwd_size;   /**< Number of elements in the backward array. */
    uint32_t    bwd_offset; /**< Offset to the backward array. */
} header_t;

int cqdb_writer_close(cqdb_writer_t* dbw)
{
header_t header;
strncpy((char*)header.chunkid, CHUNKID, 4);
...
}

cqdb_t* cqdb_reader(const void *buffer, size_t size)
{
    cqdb_t* db = NULL;
    /* Check the file chunkid. */
    if (memcmp(buffer, CHUNKID, 4) != 0) {
        return NULL;
    }
    db = (cqdb_t*)calloc(1, sizeof(cqdb_t));
    const uint8_t* p = NULL;
    db->buffer = buffer;
    p = db->buffer;
    strncpy((char*)db->header.chunkid, (const char*)p, 4);
...
}

The usage of strncpy in the question is actually correct (left-justifying some characters in a buffer, right-padding with null bytes), but the warning is because this function is often misused by people trying to copy a null-terminated string. 问题中使用strncpy实际上是正确的(将缓冲区中的某些字符左对齐,使用空字节右填充),但是警告是因为尝试复制以空终止的字符串的人们经常滥用此功能。

For the code shown in the question I would replace the strncpy calls with: 对于问题中显示的代码,我将strncpy调用替换为:

set_chunkid(&header);

where you add a new function: 在其中添加新功能的位置:

void set_chunkid(header_t *hdr)
{
    _Static_assert(sizeof CHUNKID == sizeof hdr->chunkid + 1, "chunk ID not 4 chars");

    memcpy(&hdr->chunkid, CHUNKID, sizeof hdr->chunkid);
}

If there are other use cases that this function doesn't cover then update the question. 如果还有此功能无法涵盖的其他用例,请更新问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM