Skip to content

Commit 480df52

Browse files
authored
Merge pull request #71 from fosslight/develop
Fix memory bug when checking file type
2 parents ea0c147 + bb81b06 commit 480df52

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/fosslight_binary/binary_analysis.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
_root_path = ""
3838
_start_time = ""
3939
windows = False
40+
BYTES = 2048
4041

4142
JAR_VUL_HEADER = {'BIN_FL_Binary': ['ID', 'Source Name or Path', 'OSS Name',
4243
'OSS Version', 'License', 'Download Location',
@@ -222,10 +223,17 @@ def check_binary(file_with_path):
222223
if stat.S_ISFIFO(os.stat(file_with_path).st_mode):
223224
return False
224225
file_command_result = ""
226+
file_command_failed = False
225227
try:
226228
file_command_result = magic.from_file(file_with_path)
227-
except Exception as ex:
228-
logger.debug(f"Failed to check specific file type:{file_with_path}, {ex}")
229+
except Exception:
230+
file_command_failed = True
231+
if file_command_failed:
232+
try:
233+
file_command_result = magic.from_buffer(open(file_with_path).read(BYTES))
234+
except Exception as ex:
235+
logger.debug(f"Failed to check file type:{file_with_path}, {ex}")
236+
229237
if file_command_result:
230238
file_command_result = file_command_result.lower()
231239
if any(file_command_result.startswith(x) for x in _REMOVE_FILE_COMMAND_RESULT):

0 commit comments

Comments
 (0)