Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fff9530

Browse files
committed
Issue #3696: Error parsing arguments on OpenBSD <= 4.4 and Cygwin.
Patch by Amaury Forgeot d'Arc, reviewed by me.
1 parent 658fad8 commit fff9530

7 files changed

Lines changed: 128 additions & 3 deletions

File tree

Misc/NEWS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ What's New in Python 3.0 release candidate 1
1212
Core and Builtins
1313
-----------------
1414

15+
- Issue #3696: Error parsing arguments on OpenBSD <= 4.4 and Cygwin. On
16+
these systems, the mbstowcs() function is slightly buggy and must be
17+
replaced with strlen() for the purpose of counting of number of wide
18+
characters needed to represent the multi-byte character string.
19+
1520
- Issue #3697: "Fatal Python error: Cannot recover from stack overflow"
1621
could be easily encountered under Windows in debug mode when exercising
1722
the recursion limit checking code, due to bogus handling of recursion

Modules/_localemodule.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ static PyObject *Error;
4949
static PyObject*
5050
str2uni(const char* s)
5151
{
52+
#ifdef HAVE_BROKEN_MBSTOWCS
53+
size_t needed = strlen(s);
54+
#else
5255
size_t needed = mbstowcs(NULL, s, 0);
56+
#endif
5357
size_t res1;
5458
wchar_t smallbuf[30];
5559
wchar_t *dest;
@@ -67,7 +71,11 @@ str2uni(const char* s)
6771
}
6872
/* This shouldn't fail now */
6973
res1 = mbstowcs(dest, s, needed+1);
74+
#ifdef HAVE_BROKEN_MBSTOWCS
75+
assert(res1 != (size_t)-1);
76+
#else
7077
assert(res1 == needed);
78+
#endif
7179
res2 = PyUnicode_FromWideChar(dest, res1);
7280
if (dest != smallbuf)
7381
PyMem_Free(dest);

Modules/python.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,16 @@ main(int argc, char **argv)
4040
oldloc = setlocale(LC_ALL, NULL);
4141
setlocale(LC_ALL, "");
4242
for (i = 0; i < argc; i++) {
43+
#ifdef HAVE_BROKEN_MBSTOWCS
44+
/* Some platforms have a broken implementation of
45+
* mbstowcs which does not count the characters that
46+
* would result from conversion. Use an upper bound.
47+
*/
48+
size_t argsize = strlen(argv[i]);
49+
#else
4350
size_t argsize = mbstowcs(NULL, argv[i], 0);
51+
#endif
52+
size_t count;
4453
if (argsize == (size_t)-1) {
4554
fprintf(stderr, "Could not convert argument %d to string", i);
4655
return 1;
@@ -51,7 +60,11 @@ main(int argc, char **argv)
5160
fprintf(stderr, "out of memory");
5261
return 1;
5362
}
54-
mbstowcs(argv_copy[i], argv[i], argsize+1);
63+
count = mbstowcs(argv_copy[i], argv[i], argsize+1);
64+
if (count == (size_t)-1) {
65+
fprintf(stderr, "Could not convert argument %d to string", i);
66+
return 1;
67+
}
5568
}
5669
setlocale(LC_ALL, oldloc);
5770
res = Py_Main(argc, argv_copy);

Python/frozenmain.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,12 @@ Py_FrozenMain(int argc, char **argv)
4545
oldloc = setlocale(LC_ALL, NULL);
4646
setlocale(LC_ALL, "");
4747
for (i = 0; i < argc; i++) {
48+
#ifdef HAVE_BROKEN_MBSTOWCS
49+
size_t argsize = strlen(argv[i]);
50+
#else
4851
size_t argsize = mbstowcs(NULL, argv[i], 0);
52+
#endif
53+
size_t count;
4954
if (argsize == (size_t)-1) {
5055
fprintf(stderr, "Could not convert argument %d to string", i);
5156
return 1;
@@ -56,7 +61,11 @@ Py_FrozenMain(int argc, char **argv)
5661
fprintf(stderr, "out of memory");
5762
return 1;
5863
}
59-
mbstowcs(argv_copy[i], argv[i], argsize+1);
64+
count = mbstowcs(argv_copy[i], argv[i], argsize+1);
65+
if (count == (size_t)-1) {
66+
fprintf(stderr, "Could not convert argument %d to string", i);
67+
return 1;
68+
}
6069
}
6170
setlocale(LC_ALL, oldloc);
6271

configure

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#! /bin/sh
2-
# From configure.in Revision: 65206 .
2+
# From configure.in Revision: 65857 .
33
# Guess values for system-dependent variables and create Makefiles.
44
# Generated by GNU Autoconf 2.61 for python 3.0.
55
#
@@ -24315,6 +24315,71 @@ _ACEOF
2431524315
fi
2431624316

2431724317

24318+
{ echo "$as_me:$LINENO: checking for broken mbstowcs" >&5
24319+
echo $ECHO_N "checking for broken mbstowcs... $ECHO_C" >&6; }
24320+
if test "$cross_compiling" = yes; then
24321+
ac_cv_broken_mbstowcs=no
24322+
else
24323+
cat >conftest.$ac_ext <<_ACEOF
24324+
/* confdefs.h. */
24325+
_ACEOF
24326+
cat confdefs.h >>conftest.$ac_ext
24327+
cat >>conftest.$ac_ext <<_ACEOF
24328+
/* end confdefs.h. */
24329+
24330+
#include<stdlib.h>
24331+
int main() {
24332+
size_t len = -1;
24333+
const char *str = "text";
24334+
len = mbstowcs(NULL, str, 0);
24335+
return (len != 4);
24336+
}
24337+
24338+
_ACEOF
24339+
rm -f conftest$ac_exeext
24340+
if { (ac_try="$ac_link"
24341+
case "(($ac_try" in
24342+
*\"* | *\`* | *\\*) ac_try_echo=\$ac_try;;
24343+
*) ac_try_echo=$ac_try;;
24344+
esac
24345+
eval "echo \"\$as_me:$LINENO: $ac_try_echo\"") >&5
24346+
(eval "$ac_link") 2>&5
24347+
ac_status=$?
24348+
echo "$as_me:$LINENO: \$? = $ac_status" >&5
24349+
(exit $ac_status); } && { ac_try='./conftest$ac_exeext'
24350+
{ (case "(($ac_try" in
24351+
*\"* | *\`* | *\\*) ac_try_echo=\$ac_try;;
24352+
*) ac_try_echo=$ac_try;;
24353+
esac
24354+
eval "echo \"\$as_me:$LINENO: $ac_try_echo\"") >&5
24355+
(eval "$ac_try") 2>&5
24356+
ac_status=$?
24357+
echo "$as_me:$LINENO: \$? = $ac_status" >&5
24358+
(exit $ac_status); }; }; then
24359+
ac_cv_broken_mbstowcs=no
24360+
else
24361+
echo "$as_me: program exited with status $ac_status" >&5
24362+
echo "$as_me: failed program was:" >&5
24363+
sed 's/^/| /' conftest.$ac_ext >&5
24364+
24365+
( exit $ac_status )
24366+
ac_cv_broken_mbstowcs=yes
24367+
fi
24368+
rm -f core *.core core.conftest.* gmon.out bb.out conftest$ac_exeext conftest.$ac_objext conftest.$ac_ext
24369+
fi
24370+
24371+
24372+
{ echo "$as_me:$LINENO: result: $ac_cv_broken_mbstowcs" >&5
24373+
echo "${ECHO_T}$ac_cv_broken_mbstowcs" >&6; }
24374+
if test "$ac_cv_broken_mbstowcs" = yes
24375+
then
24376+
24377+
cat >>confdefs.h <<\_ACEOF
24378+
#define HAVE_BROKEN_MBSTOWCS 1
24379+
_ACEOF
24380+
24381+
fi
24382+
2431824383

2431924384

2432024385
for h in `(cd $srcdir;echo Python/thread_*.h)`

configure.in

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3571,6 +3571,27 @@ AC_CHECK_TYPE(socklen_t,,
35713571
#endif
35723572
])
35733573

3574+
AC_MSG_CHECKING(for broken mbstowcs)
3575+
AC_TRY_RUN([
3576+
#include<stdlib.h>
3577+
int main() {
3578+
size_t len = -1;
3579+
const char *str = "text";
3580+
len = mbstowcs(NULL, str, 0);
3581+
return (len != 4);
3582+
}
3583+
],
3584+
ac_cv_broken_mbstowcs=no,
3585+
ac_cv_broken_mbstowcs=yes,
3586+
ac_cv_broken_mbstowcs=no)
3587+
AC_MSG_RESULT($ac_cv_broken_mbstowcs)
3588+
if test "$ac_cv_broken_mbstowcs" = yes
3589+
then
3590+
AC_DEFINE(HAVE_BROKEN_MBSTOWCS, 1,
3591+
[Define if mbstowcs(NULL, "text", 0) does not return the number of
3592+
wide chars that would be converted.])
3593+
fi
3594+
35743595
AC_SUBST(THREADHEADERS)
35753596

35763597
for h in `(cd $srcdir;echo Python/thread_*.h)`

pyconfig.h.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,10 @@
803803
/* Define to 1 if you have the `wcsxfrm' function. */
804804
#undef HAVE_WCSXFRM
805805

806+
/* Define if mbstowcs(NULL, "text", 0) does not return the number of
807+
wide chars that would be converted */
808+
#undef HAVE_BROKEN_MBSTOWCS
809+
806810
/* Define if tzset() actually switches the local timezone in a meaningful way.
807811
*/
808812
#undef HAVE_WORKING_TZSET

0 commit comments

Comments
 (0)