kevinlawler / kona

Open-source implementation of the K programming language
ISC License
1.36k stars 138 forks source link

New compiler warning for v.c after upgrade to Fedora-28 #519

Closed tavmem closed 6 years ago

tavmem commented 6 years ago

cc -g -pthread -O0 -g3 -DDEBUG -Wall -c -o src/v.t.o src/v.c src/v.c: In function ‘enumerate_charvec’: src/v.c:335:3: warning: ‘strncpy’ output truncated before terminating nul copying 3 bytes from a string of the same length [-Wstringop-truncation] strncpy(kC(p),"ls ", 3); ^~~~~~~

This warning occurs with the -DDEBUG option when compiling k_test.

tavmem commented 6 years ago

This compiler warning first shows up in the first of 2 commits 444c19bd6c5051bf2ba9c6bdbd8a47b460a8b3ae made on Feb 9, 2018. The title of this commit is "fix warnings using -Wall with gcc & remove some trailing blanks".

tavmem commented 6 years ago

The reason that this warning first shows up in the commit 444c19bd6c5051bf2ba9c6bdbd8a47b460a8b3ae of Feb 9, 2018 is that beginning with that commit 'Makefile' specifies the display of all Warning messages:

-DEVFLAGS = -O0 -g3 -DDEBUG -Wunused -Wreturn-type -Wimplicit-int #-Wall
+DEVFLAGS = -O0 -g3 -DDEBUG -Wunused -Wreturn-type -Wimplicit-int -Wall
tavmem commented 6 years ago

The code currently causing the warning message exists at least as far back as the commit 41e48b64aefe0db53f904857dfc5f166a0a2479a made on Nov 24, 2011:

static K enumerate_charvec(C *pth)
{
  K z;
  I len=strlen(pth);
  K p=newK(-3,len+3);
  strncpy(kC(p),"ls ", 3);
  strncpy(kC(p)+3,pth,len);
  z = popen_charvec(kC(p));
  cd(p);
  R z;
}
tavmem commented 6 years ago

@bakul wrote:

This code change seems wrong. man strcat man strncat The original code is also wrong unless newK allocates an extra element for the array as strncat tacks on a terminating nul char.

tavmem commented 6 years ago

@bakul also wrote:

s is assigned a literal string. strcat(s,pth) will append the contents of pth at the end of s. This is an undefined operation as per the C standard (and does indeed crash a simple example program for me) as it will overwrite whatever was statically allocated after the literal "ls ".

The original code is fine. I don't know what the compiler was bitching about, but a better change may be to replace original lines 333-336 with

I len=strlen(pth)+3; K p=newK(-3,len); snprintf(kC(p),len,"ls %s", pth);

Thanks !!