Skip to content

Commit 299cb3b

Browse files
authored
Merge pull request #333 from julia-vscode/sp/large-file-limit
Large file limits for linting
2 parents 5fb681c + c4a3032 commit 299cb3b

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

src/StaticLint.jl

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ include("scope.jl")
1717
include("subtypes.jl")
1818
include("methodmatching.jl")
1919

20+
const LARGE_FILE_LIMIT = 2_000_000 # bytes
21+
2022
mutable struct Meta
2123
binding::Union{Nothing,Binding}
2224
scope::Union{Nothing,Scope}
@@ -227,6 +229,20 @@ function traverse(x::EXPR, state)
227229
end
228230
end
229231

232+
function check_filesize(x, path)
233+
nb = try
234+
filesize(path)
235+
catch
236+
seterror!(x, FileNotAvailable)
237+
return false
238+
end
239+
240+
toobig = nb > LARGE_FILE_LIMIT
241+
if toobig
242+
seterror!(x, FileTooBig)
243+
end
244+
return !toobig
245+
end
230246

231247
"""
232248
followinclude(x, state)
@@ -245,7 +261,11 @@ function followinclude(x, state::State)
245261
elseif isabspath(path)
246262
if hasfile(state.server, path)
247263
elseif canloadfile(state.server, path)
248-
loadfile(state.server, path)
264+
if check_filesize(x, path)
265+
loadfile(state.server, path)
266+
else
267+
return
268+
end
249269
else
250270
path = ""
251271
end
@@ -255,7 +275,11 @@ function followinclude(x, state::State)
255275
path = joinpath(dirname(getpath(state.file)), path)
256276
elseif canloadfile(state.server, joinpath(dirname(getpath(state.file)), path))
257277
path = joinpath(dirname(getpath(state.file)), path)
258-
loadfile(state.server, path)
278+
if check_filesize(x, path)
279+
loadfile(state.server, path)
280+
else
281+
return
282+
end
259283
else
260284
path = ""
261285
end
@@ -277,8 +301,14 @@ function followinclude(x, state::State)
277301
seterror!(x, IncludeLoop)
278302
return
279303
end
304+
f = getfile(state.server, path)
305+
306+
if f.cst.fullspan > LARGE_FILE_LIMIT
307+
seterror!(x, FileTooBig)
308+
return
309+
end
280310
oldfile = state.file
281-
state.file = getfile(state.server, path)
311+
state.file = f
282312
push!(state.included_files, getpath(state.file))
283313
setroot(state.file, getroot(oldfile))
284314
setscope!(getcst(state.file), nothing)

src/linting/checks.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@
3030
CannotDefineFuncAlreadyHasValue,
3131
DuplicateFuncArgName,
3232
IncludePathContainsNULL,
33-
IndexFromLength
33+
IndexFromLength,
34+
FileTooBig,
35+
FileNotAvailable,
3436
)
3537

3638
const LintCodeDescriptions = Dict{LintCodes,String}(
@@ -62,7 +64,9 @@ const LintCodeDescriptions = Dict{LintCodes,String}(
6264
CannotDefineFuncAlreadyHasValue => "Cannot define function ; it already has a value.",
6365
DuplicateFuncArgName => "Function argument name not unique.",
6466
IncludePathContainsNULL => "Cannot include file, path contains NULL characters.",
65-
IndexFromLength => "Indexing with indices obtained from `length`, `size` etc is discouraged. Use `eachindex` or `axes` instead."
67+
IndexFromLength => "Indexing with indices obtained from `length`, `size` etc is discouraged. Use `eachindex` or `axes` instead.",
68+
FileTooBig => "File too big, not following include.",
69+
FileNotAvailable => "File not available."
6670
)
6771

6872
haserror(m::Meta) = m.error !== nothing

0 commit comments

Comments
 (0)