* [9fans] Why getenv replaces \0 with spaces in the returned value?
@ 2017-01-18 16:45 Giacomo Tesio
2017-01-18 18:07 ` Ryan Gonzalez
2017-01-18 19:21 ` Fran. J Ballesteros
0 siblings, 2 replies; 6+ messages in thread
From: Giacomo Tesio @ 2017-01-18 16:45 UTC (permalink / raw)
To: Fans of the OS Plan 9 from Bell Labs
Hi, last night I noticed this strange post processing in 4th edition's
getenv: https://github.com/brho/plan9/blob/master/sys/src/libc/9sys/getenv.c#L34-L41
seek(f, 0, 0);
r = read(f, ans, s);
if(r >= 0) {
ep = ans + s - 1;
for(p = ans; p < ep; p++)
if(*p == '\0')
*p = ' ';
ans[s] = '\0';
}
Anybody know why this replacement is done?
It does not seem a good fix to read/write or read/truncate races, but
I can't find a better explanation.
Giacomo
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [9fans] Why getenv replaces \0 with spaces in the returned value?
2017-01-18 16:45 [9fans] Why getenv replaces \0 with spaces in the returned value? Giacomo Tesio
@ 2017-01-18 18:07 ` Ryan Gonzalez
2017-01-18 19:21 ` Fran. J Ballesteros
1 sibling, 0 replies; 6+ messages in thread
From: Ryan Gonzalez @ 2017-01-18 18:07 UTC (permalink / raw)
To: Fans of the OS Plan 9 from Bell Labs; +Cc: Fans of the OS Plan 9 from Bell Labs
[-- Attachment #1: Type: text/plain, Size: 1393 bytes --]
Just a wild guess, but I think it could be because getenv just returns a null-
terminated string with no indication of its length. If C code were to do
pretty much anything on the environment variable in question, it would always
be truncated. e.g. with VAR=ABC\0DEF, the C string processing functions would
assume it ended after ABC.
As a side note, Linux does nothing and simply returns it as-is. I wonder why
the Posix guys haven't added a getenv_n(var, &length) function yet...
\--
Ryan (ライアン)
Yoko Shimomura > ryo (supercell/EGOIST) > Hiroyuki Sawano >> everyone else
<http://refi64.com/>
On Jan 18 2017, at 10:47 am, Giacomo Tesio <giacomo@tesio.it> wrote:
> Hi, last night I noticed this strange post processing in 4th edition's
getenv:
https://github.com/brho/plan9/blob/master/sys/src/libc/9sys/getenv.c#L34-L41
>
> seek(f, 0, 0);
r = read(f, ans, s);
if(r >= 0) {
ep = ans + s - 1;
for(p = ans; p < ep; p++)
if(*p == '\0')
*p = ' ';
ans[s] = '\0';
}
>
> Anybody know why this replacement is done?
It does not seem a good fix to read/write or read/truncate races, but
I can't find a better explanation.
>
>
Giacomo
[-- Attachment #2: Type: text/html, Size: 2328 bytes --]
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [9fans] Why getenv replaces \0 with spaces in the returned value?
2017-01-18 16:45 [9fans] Why getenv replaces \0 with spaces in the returned value? Giacomo Tesio
2017-01-18 18:07 ` Ryan Gonzalez
@ 2017-01-18 19:21 ` Fran. J Ballesteros
2017-01-18 20:13 ` Charles Forsyth
1 sibling, 1 reply; 6+ messages in thread
From: Fran. J Ballesteros @ 2017-01-18 19:21 UTC (permalink / raw)
To: Fans of the OS Plan 9 from Bell Labs
rc lists?
> El 18 ene 2017, a las 17:45, Giacomo Tesio <giacomo@tesio.it> escribió:
>
> Hi, last night I noticed this strange post processing in 4th edition's
> getenv: https://github.com/brho/plan9/blob/master/sys/src/libc/9sys/getenv.c#L34-L41
>
> seek(f, 0, 0);
> r = read(f, ans, s);
> if(r >= 0) {
> ep = ans + s - 1;
> for(p = ans; p < ep; p++)
> if(*p == '\0')
> *p = ' ';
> ans[s] = '\0';
> }
>
> Anybody know why this replacement is done?
> It does not seem a good fix to read/write or read/truncate races, but
> I can't find a better explanation.
>
>
> Giacomo
>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [9fans] Why getenv replaces \0 with spaces in the returned value?
2017-01-18 19:21 ` Fran. J Ballesteros
@ 2017-01-18 20:13 ` Charles Forsyth
2017-01-18 23:48 ` Giacomo Tesio
0 siblings, 1 reply; 6+ messages in thread
From: Charles Forsyth @ 2017-01-18 20:13 UTC (permalink / raw)
To: Fans of the OS Plan 9 from Bell Labs
[-- Attachment #1: Type: text/plain, Size: 954 bytes --]
Yes, it's the lists. Nothing will cope with \0 in a C string, so it's a
good choice as list of string element separator.
On 18 January 2017 at 19:21, Fran. J Ballesteros <nemo@lsub.org> wrote:
> rc lists?
>
> > El 18 ene 2017, a las 17:45, Giacomo Tesio <giacomo@tesio.it> escribió:
> >
> > Hi, last night I noticed this strange post processing in 4th edition's
> > getenv: https://github.com/brho/plan9/blob/master/sys/src/libc/9sys/
> getenv.c#L34-L41
> >
> > seek(f, 0, 0);
> > r = read(f, ans, s);
> > if(r >= 0) {
> > ep = ans + s - 1;
> > for(p = ans; p < ep; p++)
> > if(*p == '\0')
> > *p = ' ';
> > ans[s] = '\0';
> > }
> >
> > Anybody know why this replacement is done?
> > It does not seem a good fix to read/write or read/truncate races, but
> > I can't find a better explanation.
> >
> >
> > Giacomo
> >
>
>
>
[-- Attachment #2: Type: text/html, Size: 1676 bytes --]
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [9fans] Why getenv replaces \0 with spaces in the returned value?
2017-01-18 20:13 ` Charles Forsyth
@ 2017-01-18 23:48 ` Giacomo Tesio
2017-01-21 8:22 ` Charles Forsyth
0 siblings, 1 reply; 6+ messages in thread
From: Giacomo Tesio @ 2017-01-18 23:48 UTC (permalink / raw)
To: Fans of the OS Plan 9 from Bell Labs
Yes that would be a plausible explanation but actually rc does not use
getenv: it reads /env/ files directly.
I've tried to remove the loop and I can't see any issue.
Giacomo
2017-01-18 21:13 GMT+01:00 Charles Forsyth <charles.forsyth@gmail.com>:
> Yes, it's the lists. Nothing will cope with \0 in a C string, so it's a good
> choice as list of string element separator.
>
> On 18 January 2017 at 19:21, Fran. J Ballesteros <nemo@lsub.org> wrote:
>>
>> rc lists?
>>
>> > El 18 ene 2017, a las 17:45, Giacomo Tesio <giacomo@tesio.it> escribió:
>> >
>> > Hi, last night I noticed this strange post processing in 4th edition's
>> > getenv:
>> > https://github.com/brho/plan9/blob/master/sys/src/libc/9sys/getenv.c#L34-L41
>> >
>> > seek(f, 0, 0);
>> > r = read(f, ans, s);
>> > if(r >= 0) {
>> > ep = ans + s - 1;
>> > for(p = ans; p < ep; p++)
>> > if(*p == '\0')
>> > *p = ' ';
>> > ans[s] = '\0';
>> > }
>> >
>> > Anybody know why this replacement is done?
>> > It does not seem a good fix to read/write or read/truncate races, but
>> > I can't find a better explanation.
>> >
>> >
>> > Giacomo
>> >
>>
>>
>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [9fans] Why getenv replaces \0 with spaces in the returned value?
2017-01-18 23:48 ` Giacomo Tesio
@ 2017-01-21 8:22 ` Charles Forsyth
0 siblings, 0 replies; 6+ messages in thread
From: Charles Forsyth @ 2017-01-21 8:22 UTC (permalink / raw)
To: Fans of the OS Plan 9 from Bell Labs
[-- Attachment #1: Type: text/plain, Size: 1505 bytes --]
Yes but other programs might like to read values left behind by rc, using
getenv
On Wed, 18 Jan 2017, 23:51 Giacomo Tesio, <giacomo@tesio.it> wrote:
> Yes that would be a plausible explanation but actually rc does not use
> getenv: it reads /env/ files directly.
>
> I've tried to remove the loop and I can't see any issue.
>
>
> Giacomo
>
>
> 2017-01-18 21:13 GMT+01:00 Charles Forsyth <charles.forsyth@gmail.com>:
> > Yes, it's the lists. Nothing will cope with \0 in a C string, so it's a
> good
> > choice as list of string element separator.
> >
> > On 18 January 2017 at 19:21, Fran. J Ballesteros <nemo@lsub.org> wrote:
> >>
> >> rc lists?
> >>
> >> > El 18 ene 2017, a las 17:45, Giacomo Tesio <giacomo@tesio.it>
> escribió:
> >> >
> >> > Hi, last night I noticed this strange post processing in 4th edition's
> >> > getenv:
> >> >
> https://github.com/brho/plan9/blob/master/sys/src/libc/9sys/getenv.c#L34-L41
> >> >
> >> > seek(f, 0, 0);
> >> > r = read(f, ans, s);
> >> > if(r >= 0) {
> >> > ep = ans + s - 1;
> >> > for(p = ans; p < ep; p++)
> >> > if(*p == '\0')
> >> > *p = ' ';
> >> > ans[s] = '\0';
> >> > }
> >> >
> >> > Anybody know why this replacement is done?
> >> > It does not seem a good fix to read/write or read/truncate races, but
> >> > I can't find a better explanation.
> >> >
> >> >
> >> > Giacomo
> >> >
> >>
> >>
> >
>
>
[-- Attachment #2: Type: text/html, Size: 3335 bytes --]
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2017-01-21 8:22 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-01-18 16:45 [9fans] Why getenv replaces \0 with spaces in the returned value? Giacomo Tesio
2017-01-18 18:07 ` Ryan Gonzalez
2017-01-18 19:21 ` Fran. J Ballesteros
2017-01-18 20:13 ` Charles Forsyth
2017-01-18 23:48 ` Giacomo Tesio
2017-01-21 8:22 ` Charles Forsyth
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).