Skip to content

Commit

Permalink
[project @ 2007-06-07 09:40:50 by twaugh]
Browse files Browse the repository at this point in the history
2007-06-07  Tim Waugh  <[email protected]>

        * cupsppd.c (cautious_PyUnicode_DecodeUTF8): New function to
        safe-guard against supposed UTF-8 strings that are incorrect (bug
        #242962).

Original author: twaugh
Date: 2007-06-07 09:40:50+00:00

git-svn-id: svn+ssh://svn.fedorahosted.org/svn/pycups/pycups@183 e7545f15-59ab-4934-8816-61942c173d0f
  • Loading branch information
mmcgrath committed Jul 5, 2007
1 parent 3144acf commit f0603af
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
6 changes: 6 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
2007-06-07 Tim Waugh <[email protected]>

* cupsppd.c (cautious_PyUnicode_DecodeUTF8): New function to
safe-guard against supposed UTF-8 strings that are incorrect (bug
#242962).

2007-05-18 Tim Waugh <[email protected]>

* examples/cupstree.py: New file.
Expand Down
32 changes: 30 additions & 2 deletions cupsppd.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "cupsppd.h"
#include "cupsmodule.h"

#include <ctype.h>
#include <iconv.h>
#include <stdbool.h>

Expand Down Expand Up @@ -100,6 +101,33 @@ ppd_encoding_is_utf8 (PPD *ppd)
return 0;
}

static PyObject *
cautious_PyUnicode_DecodeUTF8 (const char *str, size_t len)
{
PyObject *ret = PyUnicode_DecodeUTF8 (str, len, NULL);
if (ret == NULL) {
// It wasn't UTF-8 after all. Just make the string safe for ASCII.
char *safe;
size_t i;

PyErr_Clear ();
safe = malloc (len + 1);
for (i = 0; i < len; i++) {
unsigned char ch = str[i];
if (!isascii (ch))
ch = '?';

safe[i] = ch;
}
safe[i] = '\0';
ret = PyUnicode_DecodeUTF8 (safe, len, NULL);
printf ("Bad UTF-8 string \"%s\" changed to \"%s\"\n", str, safe);
free (safe);
}

return ret;
}

static PyObject *
make_PyUnicode_from_ppd_string (PPD *ppd, const char *ppdstr)
{
Expand All @@ -111,7 +139,7 @@ make_PyUnicode_from_ppd_string (PPD *ppd, const char *ppdstr)
PyObject *ret;

if (ppd_encoding_is_utf8 (ppd))
return PyUnicode_DecodeUTF8 (ppdstr, strlen (ppdstr), NULL);
return cautious_PyUnicode_DecodeUTF8 (ppdstr, strlen (ppdstr));

cdf = *ppd->conv_from;

Expand All @@ -126,7 +154,7 @@ make_PyUnicode_from_ppd_string (PPD *ppd, const char *ppdstr)
return PyErr_SetFromErrno (PyExc_RuntimeError);
}

ret = PyUnicode_DecodeUTF8 (outbuf, origleft - outbytesleft, NULL);
ret = cautious_PyUnicode_DecodeUTF8 (outbuf, origleft - outbytesleft);
free (outbuf);
return ret;
}
Expand Down

0 comments on commit f0603af

Please sign in to comment.