[Bf-blender-cvs] [5c726dd4ef0] master: Fix C++ STL importer unused function result warning

Iyad Ahmed noreply at git.blender.org
Thu Jun 30 10:14:41 CEST 2022


Commit: 5c726dd4ef09a8ffbd4f609d45106065c5bd04f3
Author: Iyad Ahmed
Date:   Thu Jun 30 10:13:05 2022 +0200
Branches: master
https://developer.blender.org/rB5c726dd4ef09a8ffbd4f609d45106065c5bd04f3

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

===================================================================

M	source/blender/io/stl/importer/stl_import.cc
M	source/blender/io/stl/importer/stl_import.hh
M	source/blender/io/stl/importer/stl_import_binary_reader.cc

===================================================================

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);
   }



More information about the Bf-blender-cvs mailing list