library: skip replacement of trailing '\0' in read_unvectored()

Under some circumstances the ksh shell doesn't fork new processes
when executing scripts and the script is interpreted by the
parent process. That makes the execution faster, but it means
ksh needs to reuse the /proc/PID/cmdline for the new script name
and arguments while the file length needs to stay untouched.
The fork is skipped only when the new cmdline is shorter than
the parent's cmdline and the rest of the file is filled
with '\0'. This is perfectly ok until we try to read the cmdline
of such process. As the read_unvectored() function replaces
all zeros with chosen separator, these trailing zeros are replaced
with spaces in case of the ps tool. Consequently it appends
multiple spaces at the end of the arguments string even when these
zeros do not represent any separators and therefore shouldn't
be replaced.
With this commit the read_unvectored() function skips the
replacement of trailing zeros and separates valid content only.

Reference: https://bugzilla.redhat.com/show_bug.cgi?id=1057600
This commit is contained in:
Jaromir Capik 2014-01-24 18:07:34 +01:00
parent 1baf31be9b
commit ae9676a337

View File

@ -686,6 +686,7 @@ static int read_unvectored(char *restrict const dst, unsigned sz, const char* wh
close(fd); close(fd);
if(n){ if(n){
int i=n; int i=n;
while(i && dst[i-1]=='\0') --i; // skip trailing zeroes
while(i--) while(i--)
if(dst[i]=='\n' || dst[i]=='\0') dst[i]=sep; if(dst[i]=='\n' || dst[i]=='\0') dst[i]=sep;
if(dst[n-1]==' ') dst[n-1]='\0'; if(dst[n-1]==' ') dst[n-1]='\0';