@@ -246,9 +246,18 @@ void CTfLiteClass::GetInputTensorSize()
246246
247247long CTfLiteClass::GetFileSize (std::string filename)
248248{
249- struct stat stat_buf;
250- long rc = stat (filename.c_str (), &stat_buf);
251- return rc == 0 ? stat_buf.st_size : -1 ;
249+ struct stat stat_buf;
250+ long rc = -1 ;
251+
252+ FILE *pFile = fopen (filename.c_str (), " rb" ); // previously only "rb
253+
254+ if (pFile != NULL )
255+ {
256+ rc = stat (filename.c_str (), &stat_buf);
257+ fclose (pFile);
258+ }
259+
260+ return rc == 0 ? stat_buf.st_size : -1 ;
252261}
253262
254263
@@ -270,25 +279,33 @@ bool CTfLiteClass::ReadFileToModel(std::string _fn)
270279
271280 LogFile.WriteToFile (ESP_LOG_DEBUG, TAG, " Loading Model " + _fn + " /size: " + std::to_string (size) + " bytes..." );
272281
273-
274282#ifdef DEBUG_DETAIL_ON
275283 LogFile.WriteHeapInfo (" CTLiteClass::Alloc modelfile start" );
276284#endif
277285
278286 modelfile = (unsigned char *)psram_get_shared_model_memory ();
279287
280- if (modelfile != NULL )
288+ if (modelfile != NULL )
281289 {
282- FILE* f = fopen (_fn.c_str (), " rb" ); // previously only "r
283- fread (modelfile, 1 , size, f);
284- fclose (f);
290+ FILE *pFile = fopen (_fn.c_str (), " rb" ); // previously only "rb
291+
292+ if (pFile != NULL )
293+ {
294+ fread (modelfile, 1 , size, pFile);
295+ fclose (pFile);
285296
286- #ifdef DEBUG_DETAIL_ON
287- LogFile.WriteHeapInfo (" CTLiteClass::Alloc modelfile successful" );
288- #endif
297+ #ifdef DEBUG_DETAIL_ON
298+ LogFile.WriteHeapInfo (" CTLiteClass::Alloc modelfile successful" );
299+ #endif
289300
290- return true ;
291- }
301+ return true ;
302+ }
303+ else
304+ {
305+ LogFile.WriteToFile (ESP_LOG_ERROR, TAG, " CTfLiteClass::ReadFileToModel: Model does not exist" );
306+ return false ;
307+ }
308+ }
292309 else
293310 {
294311 LogFile.WriteToFile (ESP_LOG_ERROR, TAG, " CTfLiteClass::ReadFileToModel: Can't allocate enough memory: " + std::to_string (size));
0 commit comments