Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
tavmem opened this issue May 14, 2018 · 5 comments
Closed

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

tavmem opened this issue May 14, 2018 · 5 comments
Labels

Comments

@tavmem
Copy link
Collaborator

tavmem commented May 14, 2018

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 tavmem added the build label May 14, 2018
@tavmem
Copy link
Collaborator Author

tavmem commented May 14, 2018

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

@tavmem
Copy link
Collaborator Author

tavmem commented May 16, 2018

The reason that this warning first shows up in the commit 444c19b 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
Copy link
Collaborator Author

tavmem commented May 16, 2018

The code currently causing the warning message exists at least as far back as the commit 41e48b6 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 tavmem closed this as completed in 4c71cdf May 16, 2018
@tavmem
Copy link
Collaborator Author

tavmem commented May 17, 2018

@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 tavmem reopened this May 17, 2018
@tavmem
Copy link
Collaborator Author

tavmem commented May 17, 2018

@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 !!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant