Skip to content

Commit

Permalink
Merge pull request #333 from julia-vscode/sp/large-file-limit
Browse files Browse the repository at this point in the history
Large file limits for linting
  • Loading branch information
pfitzseb authored Sep 18, 2023
2 parents 5fb681c + c4a3032 commit 299cb3b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
36 changes: 33 additions & 3 deletions src/StaticLint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ include("scope.jl")
include("subtypes.jl")
include("methodmatching.jl")

const LARGE_FILE_LIMIT = 2_000_000 # bytes

mutable struct Meta
binding::Union{Nothing,Binding}
scope::Union{Nothing,Scope}
Expand Down Expand Up @@ -227,6 +229,20 @@ function traverse(x::EXPR, state)
end
end

function check_filesize(x, path)
nb = try
filesize(path)
catch
seterror!(x, FileNotAvailable)
return false
end

toobig = nb > LARGE_FILE_LIMIT
if toobig
seterror!(x, FileTooBig)
end
return !toobig
end

"""
followinclude(x, state)
Expand All @@ -245,7 +261,11 @@ function followinclude(x, state::State)
elseif isabspath(path)
if hasfile(state.server, path)
elseif canloadfile(state.server, path)
loadfile(state.server, path)
if check_filesize(x, path)
loadfile(state.server, path)
else
return
end
else
path = ""
end
Expand All @@ -255,7 +275,11 @@ function followinclude(x, state::State)
path = joinpath(dirname(getpath(state.file)), path)
elseif canloadfile(state.server, joinpath(dirname(getpath(state.file)), path))
path = joinpath(dirname(getpath(state.file)), path)
loadfile(state.server, path)
if check_filesize(x, path)
loadfile(state.server, path)
else
return
end
else
path = ""
end
Expand All @@ -277,8 +301,14 @@ function followinclude(x, state::State)
seterror!(x, IncludeLoop)
return
end
f = getfile(state.server, path)

if f.cst.fullspan > LARGE_FILE_LIMIT
seterror!(x, FileTooBig)
return
end
oldfile = state.file
state.file = getfile(state.server, path)
state.file = f
push!(state.included_files, getpath(state.file))
setroot(state.file, getroot(oldfile))
setscope!(getcst(state.file), nothing)
Expand Down
8 changes: 6 additions & 2 deletions src/linting/checks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
CannotDefineFuncAlreadyHasValue,
DuplicateFuncArgName,
IncludePathContainsNULL,
IndexFromLength
IndexFromLength,
FileTooBig,
FileNotAvailable,
)

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

haserror(m::Meta) = m.error !== nothing
Expand Down

0 comments on commit 299cb3b

Please sign in to comment.