Merge pull request #3814 from scriptum/fix-3802
Use defer in iterator to avoid possible leaks
This commit is contained in:
commit
cf9d8d6bba
2 changed files with 5 additions and 5 deletions
|
|
@ -774,6 +774,7 @@ iterator walkFiles*(pattern: string): string {.tags: [ReadDirEffect].} =
|
||||||
res: int
|
res: int
|
||||||
res = findFirstFile(pattern, f)
|
res = findFirstFile(pattern, f)
|
||||||
if res != -1:
|
if res != -1:
|
||||||
|
defer: findClose(res)
|
||||||
while true:
|
while true:
|
||||||
if not skipFindData(f) and
|
if not skipFindData(f) and
|
||||||
(f.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) == 0'i32:
|
(f.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) == 0'i32:
|
||||||
|
|
@ -786,7 +787,6 @@ iterator walkFiles*(pattern: string): string {.tags: [ReadDirEffect].} =
|
||||||
pattern[dotPos+1] == '*':
|
pattern[dotPos+1] == '*':
|
||||||
yield splitFile(pattern).dir / extractFilename(ff)
|
yield splitFile(pattern).dir / extractFilename(ff)
|
||||||
if findNextFile(res, f) == 0'i32: break
|
if findNextFile(res, f) == 0'i32: break
|
||||||
findClose(res)
|
|
||||||
else: # here we use glob
|
else: # here we use glob
|
||||||
var
|
var
|
||||||
f: Glob
|
f: Glob
|
||||||
|
|
@ -795,11 +795,11 @@ iterator walkFiles*(pattern: string): string {.tags: [ReadDirEffect].} =
|
||||||
f.gl_pathc = 0
|
f.gl_pathc = 0
|
||||||
f.gl_pathv = nil
|
f.gl_pathv = nil
|
||||||
res = glob(pattern, 0, nil, addr(f))
|
res = glob(pattern, 0, nil, addr(f))
|
||||||
|
defer: globfree(addr(f))
|
||||||
if res == 0:
|
if res == 0:
|
||||||
for i in 0.. f.gl_pathc - 1:
|
for i in 0.. f.gl_pathc - 1:
|
||||||
assert(f.gl_pathv[i] != nil)
|
assert(f.gl_pathv[i] != nil)
|
||||||
yield $f.gl_pathv[i]
|
yield $f.gl_pathv[i]
|
||||||
globfree(addr(f))
|
|
||||||
|
|
||||||
type
|
type
|
||||||
PathComponent* = enum ## Enumeration specifying a path component.
|
PathComponent* = enum ## Enumeration specifying a path component.
|
||||||
|
|
@ -845,6 +845,7 @@ iterator walkDir*(dir: string; relative=false): tuple[kind: PathComponent, path:
|
||||||
var f: WIN32_FIND_DATA
|
var f: WIN32_FIND_DATA
|
||||||
var h = findFirstFile(dir / "*", f)
|
var h = findFirstFile(dir / "*", f)
|
||||||
if h != -1:
|
if h != -1:
|
||||||
|
defer: findClose(h)
|
||||||
while true:
|
while true:
|
||||||
var k = pcFile
|
var k = pcFile
|
||||||
if not skipFindData(f):
|
if not skipFindData(f):
|
||||||
|
|
@ -856,10 +857,10 @@ iterator walkDir*(dir: string; relative=false): tuple[kind: PathComponent, path:
|
||||||
else: dir / extractFilename(getFilename(f))
|
else: dir / extractFilename(getFilename(f))
|
||||||
yield (k, xx)
|
yield (k, xx)
|
||||||
if findNextFile(h, f) == 0'i32: break
|
if findNextFile(h, f) == 0'i32: break
|
||||||
findClose(h)
|
|
||||||
else:
|
else:
|
||||||
var d = opendir(dir)
|
var d = opendir(dir)
|
||||||
if d != nil:
|
if d != nil:
|
||||||
|
defer: discard closedir(d)
|
||||||
while true:
|
while true:
|
||||||
var x = readdir(d)
|
var x = readdir(d)
|
||||||
if x == nil: break
|
if x == nil: break
|
||||||
|
|
@ -883,7 +884,6 @@ iterator walkDir*(dir: string; relative=false): tuple[kind: PathComponent, path:
|
||||||
if S_ISDIR(s.st_mode): k = pcDir
|
if S_ISDIR(s.st_mode): k = pcDir
|
||||||
if S_ISLNK(s.st_mode): k = succ(k)
|
if S_ISLNK(s.st_mode): k = succ(k)
|
||||||
yield (k, y)
|
yield (k, y)
|
||||||
discard closedir(d)
|
|
||||||
|
|
||||||
iterator walkDirRec*(dir: string, filter={pcFile, pcDir}): string {.
|
iterator walkDirRec*(dir: string, filter={pcFile, pcDir}): string {.
|
||||||
tags: [ReadDirEffect].} =
|
tags: [ReadDirEffect].} =
|
||||||
|
|
|
||||||
|
|
@ -2960,9 +2960,9 @@ when not defined(JS): #and not defined(nimscript):
|
||||||
## buffer.add(line.replace("a", "0") & '\x0A')
|
## buffer.add(line.replace("a", "0") & '\x0A')
|
||||||
## writeFile(filename, buffer)
|
## writeFile(filename, buffer)
|
||||||
var f = open(filename, bufSize=8000)
|
var f = open(filename, bufSize=8000)
|
||||||
|
defer: close(f)
|
||||||
var res = TaintedString(newStringOfCap(80))
|
var res = TaintedString(newStringOfCap(80))
|
||||||
while f.readLine(res): yield res
|
while f.readLine(res): yield res
|
||||||
close(f)
|
|
||||||
|
|
||||||
iterator lines*(f: File): TaintedString {.tags: [ReadIOEffect].} =
|
iterator lines*(f: File): TaintedString {.tags: [ReadIOEffect].} =
|
||||||
## Iterate over any line in the file `f`.
|
## Iterate over any line in the file `f`.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue