cvs commit: src/lib/libc/stdlib getenv.c

Sean C. Farley scf at
Fri Sep 28 19:12:00 PDT 2007

On Tue, 25 Sep 2007, Bruce Evans wrote:

> On Sat, 22 Sep 2007, Sean C. Farley wrote:
>> On Sat, 22 Sep 2007, Bruce Evans wrote:
>>> ...
>>> Partial analysis:
>>>  - the size_t variable must have a small value that is representable
>>>    as an int (else casting it to int would be a bug and/or printing a
>>>    line of that length would be a style bug).
>> What would be a good maximum that would fit style?  Although still
>> fairly big, NL_TEXTMAX for the entire line looks plausible.
> 79 less the length of all other text on the line :-).

See below.

* snip *

>> Hopefully, no environment variables (name=value string) are anywhere
>> close in size to size_t.  :)
> Ah I see where the value comes from.  A malicous user could probably
> put > INT_MAX bytes in a single string in the environment on machines
> with 32-bit ints, 64 bit address space and lots of RAM, and then
> fork() but not exec().  That's close enough to user input for me.

See below.

* snip *

> A more refined version would use something like strvis(), and could
> use a smaller limit (with long corrupt strings indicated something
> likje debuggers print long binary strings) since this this is only
> debugging code, but *env.c is already too large for me.

OK.  I accepted the challenge to see how hard it would be.  Here is a
small ;) sample program[1] that fits the bill.

- Encodes characters using vis() to protect the tty.
- Limits display to 79 characters taking into account encoding.
- Shows a continuation string "..." if the string is too long.

Unfortunately, it is not small nor simple.  The encoding of characters
really adds complexity to the function due to variable sizes of

Of course, I could have used strvis(), but that would have limited the
number of characters printed to 1/4 of the available space (less than 40
characters based upon the length of the program name) even if no
encoding was necessary.  I think it would have been easier to just put a
'.' in any non-printable character's position.

I will see if there is a better way.

scf at

More information about the cvs-src mailing list