Merge pull request #3188 from XhmikosR/xmr/codeql

A couple of CodeQL fixes
This commit is contained in:
Steven Black
2026-07-07 13:37:02 -07:00
committed by GitHub
2 changed files with 31 additions and 38 deletions
+27 -36
View File
@@ -288,32 +288,29 @@ def main():
nounifiedhosts=nounifiedhosts,
)
remove_old_hosts_file(settings["outputpath"], "hosts", settings["backup"])
if settings["compress"]:
finalfile = open(path_join_robust(settings["outputpath"], "hosts"), "w+b")
compressedfile = tempfile.NamedTemporaryFile()
remove_dups_and_excl(mergefile, exclusionregexes, compressedfile)
compress_file(compressedfile, settings["targetip"], finalfile)
elif settings["minimise"]:
finalfile = open(path_join_robust(settings["outputpath"], "hosts"), "w+b")
minimisedfile = tempfile.NamedTemporaryFile()
remove_dups_and_excl(mergefile, exclusionregexes, minimisedfile)
minimise_file(minimisedfile, settings["targetip"], finalfile)
else:
finalfile = remove_dups_and_excl(mergefile, exclusionregexes)
with open(path_join_robust(settings["outputpath"], "hosts"), "w+b") as finalfile:
if settings["compress"] or settings["minimise"]:
with tempfile.NamedTemporaryFile() as tmpfile:
remove_dups_and_excl(mergefile, exclusionregexes, tmpfile)
if settings["compress"]:
compress_file(tmpfile, settings["targetip"], finalfile)
else:
minimise_file(tmpfile, settings["targetip"], finalfile)
else:
remove_dups_and_excl(mergefile, exclusionregexes, finalfile)
numberofrules = settings["numberofrules"]
outputsubfolder = settings["outputsubfolder"]
skipstatichosts = settings["skipstatichosts"]
numberofrules = settings["numberofrules"]
outputsubfolder = settings["outputsubfolder"]
skipstatichosts = settings["skipstatichosts"]
write_opening_header(
finalfile,
extensions=extensions,
numberofrules=numberofrules,
outputsubfolder=outputsubfolder,
skipstatichosts=skipstatichosts,
nounifiedhosts=nounifiedhosts,
)
finalfile.close()
write_opening_header(
finalfile,
extensions=extensions,
numberofrules=numberofrules,
outputsubfolder=outputsubfolder,
skipstatichosts=skipstatichosts,
nounifiedhosts=nounifiedhosts,
)
if not settings["nogendata"]:
update_readme_data(
@@ -916,7 +913,7 @@ def minimise_file(inputfile, targetip, outputfile):
inputfile.close()
def remove_dups_and_excl(mergefile, exclusionregexes, outputfile=None):
def remove_dups_and_excl(mergefile, exclusionregexes, finalfile):
"""
Remove duplicates and remove hosts that we are excluding.
@@ -929,9 +926,8 @@ def remove_dups_and_excl(mergefile, exclusionregexes, outputfile=None):
The file object that contains the hostnames that we are pruning.
exclusionregexes : list
The list of regex patterns used to exclude domains.
outputfile : file
The file object in which the result is written. If None, the file
'settings["outputpath"]' will be created.
finalfile : file
The file object in which the result is written.
"""
numberofrules = settings["numberofrules"]
@@ -947,11 +943,6 @@ def remove_dups_and_excl(mergefile, exclusionregexes, outputfile=None):
if not os.path.exists(settings["outputpath"]):
os.makedirs(settings["outputpath"])
if outputfile is None:
finalfile = open(path_join_robust(settings["outputpath"], "hosts"), "w+b")
else:
finalfile = outputfile
# analyze any post.json here
post_json_path = os.path.join(os.path.dirname(finalfile.name), "post.json")
filters = []
@@ -1018,9 +1009,6 @@ def remove_dups_and_excl(mergefile, exclusionregexes, outputfile=None):
settings["numberofrules"] = numberofrules
mergefile.close()
if outputfile is None:
return finalfile
# Dot-separated labels of [a-z0-9_-], hyphens not at label ends, at least two
# labels. Expects a lowercased hostname.
@@ -1477,6 +1465,8 @@ def move_hosts_file_into_place(finalfile):
print_failure(f"Replacing {target_file} failed.")
return False
return False
def flush_dns_cache():
"""
@@ -1679,6 +1669,7 @@ def domain_to_idna(line):
splited_line[index] = splited_line[index].encode("IDNA").decode("UTF-8")
except IndexError:
# No hostname field to encode, leave the line as-is.
pass
return separator.join(splited_line)
return line.encode("IDNA").decode("UTF-8")
+4 -2
View File
@@ -90,8 +90,10 @@ def main():
"wt",
encoding="utf-8",
newline="\n",
) as out:
for line in open(README_TEMPLATE, encoding="utf-8", newline="\n"):
) as out, open(
README_TEMPLATE, encoding="utf-8", newline="\n"
) as template:
for line in template:
line = line.replace(
"@GEN_DATE@", time.strftime("%B %d %Y", time.gmtime())
)