Skip to content

Commit

Permalink
fix #519: New compiler warning for v.c
Browse files Browse the repository at this point in the history
  • Loading branch information
tavmem committed May 16, 2018
1 parent 8d615e8 commit 4c71cdf
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/v.c
Original file line number Diff line number Diff line change
Expand Up @@ -327,17 +327,15 @@ K rotate_mod(K a, K b)
R (b->t < 1)?rotate(a,b):mod(a,b);
}

static K enumerate_charvec(C *pth)
{
Z 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);
C *s="ls ";
strncpy(kC(p),strcat(s,pth),len+3);
z = popen_charvec(kC(p));
cd(p);
R z;
}
R z; }

K enumerate(K a)
{
Expand Down

1 comment on commit 4c71cdf

@bakul
Copy link
Contributor

@bakul bakul commented on 4c71cdf May 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Please sign in to comment.