diff options
author | Sybren A. Stüvel <sybren@blender.org> | 2021-01-14 13:38:14 +0300 |
---|---|---|
committer | Sybren A. Stüvel <sybren@blender.org> | 2021-01-14 13:38:14 +0300 |
commit | 442b6e5e09510e938dba558bed5bd125ffe4a0c1 (patch) | |
tree | baaba0f3e6b90721dda94a423f9277ad655a99e4 /source/blender/modifiers | |
parent | 024ac9c089d217a1a357f133c4dbfc01b85112e9 (diff) |
MeshCache: add error handling to `fread()` calls
Handle return value of `fread()`, by showing an error message when the
file cannot be read from and stopping further processing. Not only is
error handing a good idea, it also prevents GCC from warning that the
return value of `fread()` should not be ignored.
This is similar to {D9916}.
Reviewed By: campbellbarton
Differential Revision: https://developer.blender.org/D10079
Diffstat (limited to 'source/blender/modifiers')
-rw-r--r-- | source/blender/modifiers/intern/MOD_meshcache_mdd.c | 22 | ||||
-rw-r--r-- | source/blender/modifiers/intern/MOD_meshcache_pc2.c | 11 |
2 files changed, 28 insertions, 5 deletions
diff --git a/source/blender/modifiers/intern/MOD_meshcache_mdd.c b/source/blender/modifiers/intern/MOD_meshcache_mdd.c index d216c14ab43..22e49434f7d 100644 --- a/source/blender/modifiers/intern/MOD_meshcache_mdd.c +++ b/source/blender/modifiers/intern/MOD_meshcache_mdd.c @@ -110,17 +110,26 @@ static bool meshcache_read_mdd_range_from_time(FILE *fp, return false; } + size_t num_frames_read = 0; + size_t num_frames_expect = mdd_head.frame_tot; + errno = 0; for (i = 0; i < mdd_head.frame_tot; i++) { - fread(&f_time, sizeof(float), 1, fp); + num_frames_read += fread(&f_time, sizeof(float), 1, fp); #ifdef __LITTLE_ENDIAN__ BLI_endian_switch_float(&f_time); #endif if (f_time >= time) { + num_frames_expect = i + 1; break; } f_time_prev = f_time; } + if (num_frames_read != num_frames_expect) { + *err_str = errno ? strerror(errno) : "Timestamp read failed"; + return false; + } + if (i == mdd_head.frame_tot) { frame = (float)(mdd_head.frame_tot - 1); } @@ -165,12 +174,14 @@ bool MOD_meshcache_read_mdd_index(FILE *fp, return false; } + size_t num_verts_read = 0; + errno = 0; if (factor >= 1.0f) { #if 1 float *vco = *vertexCos; uint i; for (i = mdd_head.verts_tot; i != 0; i--, vco += 3) { - fread(vco, sizeof(float[3]), 1, fp); + num_verts_read += fread(vco, sizeof(float[3]), 1, fp); # ifdef __LITTLE_ENDIAN__ BLI_endian_switch_float(vco + 0); @@ -195,7 +206,7 @@ bool MOD_meshcache_read_mdd_index(FILE *fp, uint i; for (i = mdd_head.verts_tot; i != 0; i--, vco += 3) { float tvec[3]; - fread(tvec, sizeof(float[3]), 1, fp); + num_verts_read += fread(tvec, sizeof(float[3]), 1, fp); #ifdef __LITTLE_ENDIAN__ BLI_endian_switch_float(tvec + 0); @@ -209,6 +220,11 @@ bool MOD_meshcache_read_mdd_index(FILE *fp, } } + if (num_verts_read != mdd_head.verts_tot) { + *err_str = errno ? strerror(errno) : "Vertex coordinate read failed"; + return false; + } + return true; } diff --git a/source/blender/modifiers/intern/MOD_meshcache_pc2.c b/source/blender/modifiers/intern/MOD_meshcache_pc2.c index 0ef9f26f1d7..ea8cc334dd9 100644 --- a/source/blender/modifiers/intern/MOD_meshcache_pc2.c +++ b/source/blender/modifiers/intern/MOD_meshcache_pc2.c @@ -151,11 +151,13 @@ bool MOD_meshcache_read_pc2_index(FILE *fp, return false; } + size_t num_verts_read = 0; + errno = 0; if (factor >= 1.0f) { float *vco = *vertexCos; uint i; for (i = pc2_head.verts_tot; i != 0; i--, vco += 3) { - fread(vco, sizeof(float[3]), 1, fp); + num_verts_read += fread(vco, sizeof(float[3]), 1, fp); #ifdef __BIG_ENDIAN__ BLI_endian_switch_float(vco + 0); @@ -170,7 +172,7 @@ bool MOD_meshcache_read_pc2_index(FILE *fp, uint i; for (i = pc2_head.verts_tot; i != 0; i--, vco += 3) { float tvec[3]; - fread(tvec, sizeof(float[3]), 1, fp); + num_verts_read += fread(tvec, sizeof(float[3]), 1, fp); #ifdef __BIG_ENDIAN__ BLI_endian_switch_float(tvec + 0); @@ -184,6 +186,11 @@ bool MOD_meshcache_read_pc2_index(FILE *fp, } } + if (num_verts_read != pc2_head.verts_tot) { + *err_str = errno ? strerror(errno) : "Vertex coordinate read failed"; + return false; + } + return true; } |