diff options
author | Iyad Ahmed <iyadahmed2001> | 2022-06-30 11:13:05 +0300 |
---|---|---|
committer | Sybren A. Stüvel <sybren@blender.org> | 2022-06-30 11:14:08 +0300 |
commit | 5c726dd4ef09a8ffbd4f609d45106065c5bd04f3 (patch) | |
tree | 9e893b4a69a8d49670d16642ddef7a7c301e78b3 /source | |
parent | c39e9326317442f6751fb0b2929c49b3512309d5 (diff) |
Fix C++ STL importer unused function result warning
Fix unused function result warnings for usages of `fread` in the C++
.stl importer, by actually using the returned error code.
Reviewed By: sybren
Differential Revision: https://developer.blender.org/D15189
Diffstat (limited to 'source')
-rw-r--r-- | source/blender/io/stl/importer/stl_import.cc | 23 | ||||
-rw-r--r-- | source/blender/io/stl/importer/stl_import.hh | 2 | ||||
-rw-r--r-- | source/blender/io/stl/importer/stl_import_binary_reader.cc | 7 |
3 files changed, 29 insertions, 3 deletions
diff --git a/source/blender/io/stl/importer/stl_import.cc b/source/blender/io/stl/importer/stl_import.cc index f358598a216..097d14b038c 100644 --- a/source/blender/io/stl/importer/stl_import.cc +++ b/source/blender/io/stl/importer/stl_import.cc @@ -29,6 +29,17 @@ namespace blender::io::stl { +void stl_import_report_error(FILE *file) +{ + fprintf(stderr, "STL Importer: failed to read file"); + if (feof(file)) { + fprintf(stderr, ", end of file reached.\n"); + } + else if (ferror(file)) { + perror("Error"); + } +} + void importer_main(bContext *C, const STLImportParams &import_params) { Main *bmain = CTX_data_main(C); @@ -56,7 +67,10 @@ void importer_main(Main *bmain, uint32_t num_tri = 0; size_t file_size = BLI_file_size(import_params.filepath); fseek(file, BINARY_HEADER_SIZE, SEEK_SET); - fread(&num_tri, sizeof(uint32_t), 1, file); + if (fread(&num_tri, sizeof(uint32_t), 1, file) != 1) { + stl_import_report_error(file); + return; + } bool is_ascii_stl = (file_size != (BINARY_HEADER_SIZE + 4 + BINARY_STRIDE * num_tri)); /* Name used for both mesh and object. */ @@ -64,7 +78,7 @@ void importer_main(Main *bmain, BLI_strncpy(ob_name, BLI_path_basename(import_params.filepath), FILE_MAX); BLI_path_extension_replace(ob_name, FILE_MAX, ""); - Mesh *mesh; + Mesh *mesh = nullptr; if (is_ascii_stl) { mesh = read_stl_ascii(import_params.filepath, bmain, ob_name, import_params.use_facet_normal); } @@ -72,6 +86,11 @@ void importer_main(Main *bmain, mesh = read_stl_binary(file, bmain, ob_name, import_params.use_facet_normal); } + if (mesh == nullptr) { + fprintf(stderr, "STL Importer: Failed to import mesh '%s'\n", import_params.filepath); + return; + } + if (import_params.use_mesh_validate) { bool verbose_validate = false; #ifdef DEBUG diff --git a/source/blender/io/stl/importer/stl_import.hh b/source/blender/io/stl/importer/stl_import.hh index 377544c26af..a5d252248a8 100644 --- a/source/blender/io/stl/importer/stl_import.hh +++ b/source/blender/io/stl/importer/stl_import.hh @@ -10,6 +10,8 @@ namespace blender::io::stl { +void stl_import_report_error(FILE *file); + /* Main import function used from within Blender. */ void importer_main(bContext *C, const STLImportParams &import_params); diff --git a/source/blender/io/stl/importer/stl_import_binary_reader.cc b/source/blender/io/stl/importer/stl_import_binary_reader.cc index 6eaed16160e..fb9dcea0a1d 100644 --- a/source/blender/io/stl/importer/stl_import_binary_reader.cc +++ b/source/blender/io/stl/importer/stl_import_binary_reader.cc @@ -15,6 +15,7 @@ #include "DNA_mesh_types.h" +#include "stl_import.hh" #include "stl_import_binary_reader.hh" #include "stl_import_mesh.hh" @@ -33,7 +34,11 @@ Mesh *read_stl_binary(FILE *file, Main *bmain, char *mesh_name, bool use_custom_ const int chunk_size = 1024; uint32_t num_tris = 0; fseek(file, BINARY_HEADER_SIZE, SEEK_SET); - fread(&num_tris, sizeof(uint32_t), 1, file); + if (fread(&num_tris, sizeof(uint32_t), 1, file) != 1) { + stl_import_report_error(file); + return nullptr; + } + if (num_tris == 0) { return BKE_mesh_add(bmain, mesh_name); } |