Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
7ab4af04
Commit
7ab4af04
authored
Jan 29, 2012
by
Antoine Pitrou
Browse files
Options
Browse Files
Download
Plain Diff
Issue #13848: open() and the FileIO constructor now check for NUL characters in the file name.
Patch by Hynek Schlawack.
parents
cdc878e5
1334884f
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
41 additions
and
21 deletions
+41
-21
Include/unicodeobject.h
Include/unicodeobject.h
+6
-0
Lib/test/test_fileio.py
Lib/test/test_fileio.py
+5
-0
Lib/test/test_io.py
Lib/test/test_io.py
+5
-0
Misc/NEWS
Misc/NEWS
+3
-0
Modules/_io/fileio.c
Modules/_io/fileio.c
+9
-21
Objects/unicodeobject.c
Objects/unicodeobject.c
+13
-0
No files found.
Include/unicodeobject.h
View file @
7ab4af04
...
...
@@ -1914,6 +1914,12 @@ PyAPI_FUNC(int) PyUnicode_Contains(
PyObject
*
element
/* Element string */
);
/* Checks whether the string contains any NUL characters. */
#ifndef Py_LIMITED_API
PyAPI_FUNC
(
int
)
_PyUnicode_HasNULChars
(
PyObject
*
);
#endif
/* Checks whether argument is a valid identifier. */
PyAPI_FUNC
(
int
)
PyUnicode_IsIdentifier
(
PyObject
*
s
);
...
...
Lib/test/test_fileio.py
View file @
7ab4af04
...
...
@@ -306,6 +306,11 @@ class OtherFileTests(unittest.TestCase):
finally
:
os
.
unlink
(
TESTFN
)
def
testConstructorHandlesNULChars
(
self
):
fn_with_NUL
=
'foo
\
0
bar'
self
.
assertRaises
(
TypeError
,
_FileIO
,
fn_with_NUL
,
'w'
)
self
.
assertRaises
(
TypeError
,
_FileIO
,
bytes
(
fn_with_NUL
,
'ascii'
),
'w'
)
def
testInvalidFd
(
self
):
self
.
assertRaises
(
ValueError
,
_FileIO
,
-
10
)
self
.
assertRaises
(
OSError
,
_FileIO
,
make_bad_fd
())
...
...
Lib/test/test_io.py
View file @
7ab4af04
...
...
@@ -363,6 +363,11 @@ class IOTest(unittest.TestCase):
self
.
assertRaises
(
exc
,
fp
.
seek
,
1
,
self
.
SEEK_CUR
)
self
.
assertRaises
(
exc
,
fp
.
seek
,
-
1
,
self
.
SEEK_END
)
def
test_open_handles_NUL_chars
(
self
):
fn_with_NUL
=
'foo
\
0
bar'
self
.
assertRaises
(
TypeError
,
self
.
open
,
fn_with_NUL
,
'w'
)
self
.
assertRaises
(
TypeError
,
self
.
open
,
bytes
(
fn_with_NUL
,
'ascii'
),
'w'
)
def
test_raw_file_io
(
self
):
with
self
.
open
(
support
.
TESTFN
,
"wb"
,
buffering
=
0
)
as
f
:
self
.
assertEqual
(
f
.
readable
(),
False
)
...
...
Misc/NEWS
View file @
7ab4af04
...
...
@@ -461,6 +461,9 @@ Core and Builtins
Library
-------
-
Issue
#
13848
:
open
()
and
the
FileIO
constructor
now
check
for
NUL
characters
in
the
file
name
.
Patch
by
Hynek
Schlawack
.
-
Issue
#
13806
:
The
size
check
in
audioop
decompression
functions
was
too
strict
and
could
reject
valid
compressed
data
.
Patch
by
Oleg
Plakhotnyuk
.
...
...
Modules/_io/fileio.c
View file @
7ab4af04
...
...
@@ -258,6 +258,12 @@ fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
#ifdef MS_WINDOWS
if
(
PyUnicode_Check
(
nameobj
))
{
int
rv
=
_PyUnicode_HasNULChars
(
nameobj
);
if
(
rv
)
{
if
(
rv
!=
-
1
)
PyErr_SetString
(
PyExc_TypeError
,
"embedded NUL character"
);
return
-
1
;
}
widename
=
PyUnicode_AsUnicode
(
nameobj
);
if
(
widename
==
NULL
)
return
-
1
;
...
...
@@ -265,28 +271,10 @@ fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
#endif
if
(
fd
<
0
)
{
if
(
PyBytes_Check
(
nameobj
)
||
PyByteArray_Check
(
nameobj
))
{
Py_ssize_t
namelen
;
if
(
PyObject_AsCharBuffer
(
nameobj
,
&
name
,
&
namelen
)
<
0
)
return
-
1
;
}
else
{
PyObject
*
u
=
PyUnicode_FromObject
(
nameobj
);
if
(
u
==
NULL
)
return
-
1
;
stringobj
=
PyUnicode_EncodeFSDefault
(
u
);
Py_DECREF
(
u
);
if
(
stringobj
==
NULL
)
return
-
1
;
if
(
!
PyBytes_Check
(
stringobj
))
{
PyErr_SetString
(
PyExc_TypeError
,
"encoder failed to return bytes"
);
goto
error
;
}
name
=
PyBytes_AS_STRING
(
stringobj
);
if
(
!
PyUnicode_FSConverter
(
nameobj
,
&
stringobj
))
{
return
-
1
;
}
name
=
PyBytes_AS_STRING
(
stringobj
);
}
s
=
mode
;
...
...
Objects/unicodeobject.c
View file @
7ab4af04
...
...
@@ -3574,6 +3574,19 @@ PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size)
}
int
_PyUnicode_HasNULChars
(
PyObject
*
s
)
{
static
PyObject
*
nul
=
NULL
;
if
(
nul
==
NULL
)
nul
=
PyUnicode_FromStringAndSize
(
"
\0
"
,
1
);
if
(
nul
==
NULL
)
return
-
1
;
return
PyUnicode_Contains
(
s
,
nul
);
}
int
PyUnicode_FSConverter
(
PyObject
*
arg
,
void
*
addr
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment