[Python-checkins] r45505 - python/trunk/Modules/posixmodule.c

Neal Norwitz nnorwitz at gmail.com
Tue Apr 18 07:53:50 CEST 2006


> Modified: python/trunk/Modules/posixmodule.c
> ==============================================================================
> --- python/trunk/Modules/posixmodule.c  (original)
> +++ python/trunk/Modules/posixmodule.c  Tue Apr 18 02:49:49 2006
> @@ -6812,17 +6812,19 @@
>      char buffer[64];
>
>      if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
> -        int len = confstr(name, buffer, sizeof(buffer));
> +       int len;
>
>          errno = 0;
> -        if (len == 0) {
> -            if (errno != 0)
> -                posix_error();
> -            else
> -                result = PyString_FromString("");
> +       len = confstr(name, buffer, sizeof(buffer));
> +
> +       if (len == -1) {
> +           posix_error();
> +       }
> +       else if (len == 0) {
> +            result = PyString_FromString("");
>          }
>          else {
> -            if (len >= sizeof(buffer)) {
> +               if ((unsigned int)len >= sizeof(buffer)) {
>                  result = PyString_FromStringAndSize(NULL, len);
>                  if (result != NULL)
>                      confstr(name, PyString_AS_STRING(result), len+1);

This code seems broken (both before and after this patch) based on:
http://www.opengroup.org/onlinepubs/009695399/functions/confstr.html

The return type is size_t and cannot return -1.  Do we know if the
previous code ever worked?  Is there any system that does return -1? 
Also, since sizeof(size_t) != sizeof(int), this code isn't right.  If
size_t is 8 bytes, but int is 4 bytes and len is (1<<32) + 5, we will
try to copy 4+GB.  If len is a size_t this problem goes away, since
the (unsigned int) cast is not necessary.

n


More information about the Python-checkins mailing list