title: gcc-8 Wstringop-truncation
body: <p>I'm trying to fix some C code where gcc-8 complains about Wstringop-truncation (code is <a href="https://github.com/bnosac/crfsuite/blob/master/src/cqdb/src/cqdb.c" rel="nofollow noreferrer">here</a>)</p>
  
  <p>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:</p>
  
  <pre><code>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-&gt;header.chunkid, (const char*)p, 4);
           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  </code></pre>
  
  <p>I would like to rewrite the strncpy statements to remove these warnings. Am I right that I need to replace in the following lines </p>
  
  <pre><code>strncpy((char*)header.chunkid, CHUNKID, 4);
  with strncpy((char*)header.chunkid, CHUNKID, 5);
  
  and strncpy((char*)db-&gt;header.chunkid, (const char*)p, 4);
  with strncpy((char*)db-&gt;header.chunkid, (const char*)p, 5);
  </code></pre>
  
  <p>The relevant code in cqdb.c is put below. It basically checks if the file is of type '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.</p>
  
  <pre><code>#define CHUNKID             "CQDB"
  
  typedef struct {
      int8_t      chunkid[4]; /**&lt; Chunk identifier, "CQDB". */
      uint32_t    size;       /**&lt; Chunk size including this header. */
      uint32_t    flag;       /**&lt; Global flags. */
      uint32_t    byteorder;  /**&lt; Byte-order indicator. */
      uint32_t    bwd_size;   /**&lt; Number of elements in the backward array. */
      uint32_t    bwd_offset; /**&lt; 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-&gt;buffer = buffer;
      p = db-&gt;buffer;
      strncpy((char*)db-&gt;header.chunkid, (const char*)p, 4);
  ...
  }
  </code></pre>