classification
Title: os.confstr() does not handle value changing length between calls
Type: behavior Stage:
Components: Extension Modules Versions: Python 3.2, Python 3.1, Python 2.7, Python 2.6
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: baikie, haypo
Priority: normal Keywords:

Created on 2010-08-19 18:44 by baikie, last changed 2010-10-02 21:25 by baikie.

Messages (3)
msg114396 - (view) Author: David Watson (baikie) Date: 2010-08-19 18:44
This came up in relation to issue #9579; there is some discussion
of it there.  Basically, if os.confstr() has to call confstr()
twice because the buffer wasn't big enough the first time, the
existing code assumes the string is the same length that the OS
reported in the first call instead of using the length from the
second call and resizing the buffer if necessary.  This means the
returned value will be truncated or contain trailing garbage if
the string changed its length betweeen calls.

I don't know of an actual environment where configuration strings
can change at runtime, but it's not forbidden by POSIX as far as
I can see (the strings are described as "variables", after all,
and sysconf() values such as CHILD_MAX can change at runtime).
Implementations can also provide additional confstr() variables
not specified by POSIX.

The patch confstr-long-result.diff at issue #9579 would fix this
(for 3.x), but Victor Stinner has expressed concern that a buggy
confstr() could create a near-infinite loop with that patch
applied.
msg117633 - (view) Author: STINNER Victor (haypo) * (Python committer) Date: 2010-09-29 17:32
If I understood correctly, you don't want the value to be truncated if the variable grows between the two calls to confstr(). Which behaviour would you expect? A Python exception?

> but Victor Stinner has expressed concern that a buggy
> confstr() could create a near-infinite loop with that patch
> applied

Yes, I think that two calls to confstr() should be enough.
msg117898 - (view) Author: David Watson (baikie) Date: 2010-10-02 21:25
> If I understood correctly, you don't want the value to be truncated if the variable grows between the two calls to confstr(). Which behaviour would you expect? A Python exception?

A return size larger than the buffer is *supposed* to indicate
that the current value is larger than the supplied buffer, so I
would just expect it to reallocate the buffer, call confstr()
again and return the new value, unless it was known that such a
situation indicated an actual problem.

In other words, I would not expect it to do anything special.  I
didn't write the original patch the way I did in order to fix
this (potential) bug - it just seemed like the most natural way
to write the code.
History
Date User Action Args
2010-10-02 21:25:45baikiesetmessages: + msg117898
2010-09-29 17:32:38hayposetnosy: + haypo
messages: + msg117633
2010-08-19 18:44:52baikiecreate