Skip to content

Commit 02e91b3

Browse files
committed
Python: Model functions that will raise
on non-existing files.
1 parent f2fbeed commit 02e91b3

File tree

1 file changed

+33
-9
lines changed

1 file changed

+33
-9
lines changed

python/ql/lib/semmle/python/frameworks/Stdlib.qll

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -196,23 +196,36 @@ private module StdlibPrivate {
196196
}
197197

198198
/**
199-
* A call to `os.path.exists` or `os.path.lexists` will check if a file exists on the file system.
200-
* The `os.path` module offers e number of methods for checking if a file exists and/or has certain
199+
* The `os.path` module offers a number of methods for checking if a file exists and/or has certain
201200
* properties, leading to a file system access.
201+
* A call to `os.path.exists` or `os.path.lexists` will check if a file exists on the file system.
202202
* (Although, on some platforms, the check may return `false` due to missing permissions.)
203+
* A call to `os.path.getatime` will raise `OSError` if the file does not exist or is inaccessible.
203204
* See:
204-
* - https://docs.python.org/3/library/os.path.html#os.path.exists
205-
* - https://docs.python.org/3/library/os.path.html#os.path.lexists
206-
* - https://docs.python.org/3/library/os.path.html#os.path.isfile
207-
* - https://docs.python.org/3/library/os.path.html#os.path.isdir
208-
* - https://docs.python.org/3/library/os.path.html#os.path.islink
209-
* - https://docs.python.org/3/library/os.path.html#os.path.ismount
205+
* - https://docs.python.org/3/library/os.path.html#os.path.exists
206+
* - https://docs.python.org/3/library/os.path.html#os.path.lexists
207+
* - https://docs.python.org/3/library/os.path.html#os.path.isfile
208+
* - https://docs.python.org/3/library/os.path.html#os.path.isdir
209+
* - https://docs.python.org/3/library/os.path.html#os.path.islink
210+
* - https://docs.python.org/3/library/os.path.html#os.path.ismount
211+
* - https://docs.python.org/3/library/os.path.html#os.path.getatime
212+
* - https://docs.python.org/3/library/os.path.html#os.path.getmtime
213+
* - https://docs.python.org/3/library/os.path.html#os.path.getctime
214+
* - https://docs.python.org/3/library/os.path.html#os.path.getsize
215+
* - https://docs.python.org/3/library/os.path.html#os.path.realpath
210216
*/
211217
private class OsPathProbingCall extends FileSystemAccess::Range, DataFlow::CallCfgNode {
212218
OsPathProbingCall() {
213219
this =
214220
os::path()
215-
.getMember(["exists", "lexists", "isfile", "isdir", "islink", "ismount"])
221+
.getMember([
222+
// these check if the file exists
223+
"exists", "lexists", "isfile", "isdir", "islink", "ismount",
224+
// these raise errors if the file does not exist
225+
"getatime", "getmtime", "getctime", "getsize",
226+
// this will resolve symlinks
227+
"realpath"
228+
])
216229
.getACall()
217230
}
218231

@@ -221,6 +234,17 @@ private module StdlibPrivate {
221234
}
222235
}
223236

237+
/** A call to `os.path.samefile` will raise an exception if an `os.stat()` call on either pathname fails. */
238+
private class OsPathSamefileCall extends FileSystemAccess::Range, DataFlow::CallCfgNode {
239+
OsPathSamefileCall() { this = os::path().getMember("samefile").getACall() }
240+
241+
override DataFlow::Node getAPathArgument() {
242+
result in [
243+
this.getArg(0), this.getArgByName("path1"), this.getArg(1), this.getArgByName("path2")
244+
]
245+
}
246+
}
247+
224248
/**
225249
* A call to `os.path.normpath`.
226250
* See https://docs.python.org/3/library/os.path.html#os.path.normpath

0 commit comments

Comments
 (0)