From 0a94bfcfdf1dea093ab7ef1a1ed081a36b43e09b Mon Sep 17 00:00:00 2001 From: Eric Hanson <5846501+ericphanson@users.noreply.github.com> Date: Wed, 17 Feb 2021 01:21:47 +0100 Subject: [PATCH] Fix NUL string error (#7) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Prevent NUL characters in strings read by `licensecheck` * add test * add error test * bump version Co-authored-by: Mosè Giordano --- Project.toml | 2 +- src/find_licenses.jl | 4 +++- test/nul_string_dir/LICENSE | 21 ++++++++++++++++++ .../file_with_nul_in_the_middle.txt | Bin 0 -> 100 bytes test/runtests.jl | 8 +++++++ 5 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 test/nul_string_dir/LICENSE create mode 100644 test/nul_string_dir/file_with_nul_in_the_middle.txt diff --git a/Project.toml b/Project.toml index d89080a..fe1b714 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "LicenseCheck" uuid = "726dbf0d-6eb6-41af-b36c-cd770e0f00cc" authors = ["Eric P. Hanson"] -version = "0.2.0" +version = "0.2.1" [deps] licensecheck_jll = "4ecb348a-8b88-51ea-b912-4c460483ee91" diff --git a/src/find_licenses.jl b/src/find_licenses.jl index 4054d09..3172e30 100644 --- a/src/find_licenses.jl +++ b/src/find_licenses.jl @@ -30,7 +30,9 @@ function license_table(dir, names; validate_strings = true, validate_paths = tru path = joinpath(dir, lic) validate_paths && (isfile(path) || continue) text = read(path, String) - validate_strings && (isvalid(String, text) || continue) + # In licensecheck we're going to convert the string to a `Cstring` with + # `unsafe_convert`, so the string can't have any NUL character + validate_strings && ((isvalid(String, text) && !Base.containsnul(text)) || continue) lc = licensecheck(text) if lc.license_file_percent_covered > 0 push!(table, (; license_filename=lic, lc...)) diff --git a/test/nul_string_dir/LICENSE b/test/nul_string_dir/LICENSE new file mode 100644 index 0000000..9e2aae2 --- /dev/null +++ b/test/nul_string_dir/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2021 Eric P. Hanson + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/test/nul_string_dir/file_with_nul_in_the_middle.txt b/test/nul_string_dir/file_with_nul_in_the_middle.txt new file mode 100644 index 0000000000000000000000000000000000000000..b5b1922db12cf4bbe66b75f0bc6b56087120285d GIT binary patch literal 100 zcmcC8DyVV_aq}o*i1Z6|4K$3%Da*(<^!2H#vJCT2^3E!BDk{l0a5qZzF3K@-_6^I( z^fvJD4Ds`>2u?C^@h=ams0hmS%5%zf%S}tFa&&C3b$bFKma DgufxP literal 0 HcmV?d00001 diff --git a/test/runtests.jl b/test/runtests.jl index 031e171..5f69e1a 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -107,4 +107,12 @@ dorian_gray = """ results = find_licenses_by_list(joinpath(@__DIR__, "..")) @test fl ∈ results end + + @testset "AnalyzeRegistry#14" begin + fl = find_license("nul_string_dir") + @test fl.license_filename == "LICENSE" + @test fl.licenses_found == ["MIT"] + @test fl.license_file_percent_covered > 90 + @test_throws ArgumentError LicenseCheck.license_table("nul_string_dir", ["file_with_nul_in_the_middle.txt"]; validate_strings = false) + end end