0

I'm compiling some C code (will provide link if really necessary) using GCC 8.2.0 on Linux, and using GCC 8.1.0 on Cygwin (the latter is an automated CI build).

On Linux, everything passes fine. On Cygwin, I get - among others - the following warnings:

C:\Users\travis\build\eyalroz\ssb-dbgen\src\bm_utils.c: In function 'e_str':
C:\Users\travis\build\eyalroz\ssb-dbgen\src\bm_utils.c:199:5: warning: 'strncpy' output truncated before terminating nul copying as many bytes from a string as its length [-Wstringop-truncation]
     strncpy(dest + loc, strtmp, len);
     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
C:\Users\travis\build\eyalroz\ssb-dbgen\src\bm_utils.c:197:11: note: length computed here
     len = strlen(strtmp);
           ^~~~~~~~~~~~~~

and

C:\Users\travis\build\eyalroz\ssb-dbgen\src\build.c:697:5: warning: 'strncpy' specified bound 10 equals destination size [-Wstringop-truncation]
     strncpy(d->dayofweek, weekday_names[d->daynuminweek-1],D_DAYWEEK_LEN+1);
     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
C:\Users\travis\build\eyalroz\ssb-dbgen\src\build.c:698:5: warning: 'strncpy' specified bound 10 equals destination size [-Wstringop-truncation]
     strncpy(d->month,month_names[d->monthnuminyear-1],D_MONTH_LEN+1);
     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I'm not doing anything, as far as I can tell, which should cause different compilation flags in both cases. Why am I getting the warnings only in one environment? Could it be the GCC version?

Lundin
  • 195,001
  • 40
  • 254
  • 396
einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • Cygwin uses a different standard lib than gcc/Linux so that would be why. You appear to have numerous bugs caused by the use of `strncpy`. It is safer to use `strcpy` or `memcpy` instead. See [Which functions from the standard library must (should) be avoided?](https://stackoverflow.com/a/46563868/584518) – Lundin Feb 27 '19 at 09:44
  • 1
    I'm not sure it is related to the problem, but normally `strncpy` is used with a length depending on the available space in the buffer. If you copy `strlen(sourcestring)` you do not prevent buffer overflow and you will not copy the terminating `\0`. In the second case if the specified length equals the size of the destination you don't reserve a byte for the terminating `\0`. Both warnings indicate possible problems. (I don't know if the warning level depends on the GCC version.) – Bodo Feb 27 '19 at 09:46
  • @Bodo: I'm adding the +1 and am copying the terminating `\0`. – einpoklum Feb 27 '19 at 10:33

0 Answers0