On 05.11.18 22:38, Liam Merwick wrote: > The calls to find_mapping_for_cluster() may return NULL but it > isn't always checked for before dereferencing the value returned. > Additionally, add some asserts to cover cases where NULL can't > be returned but which might not be obvious at first glance. > > Signed-off-by: Liam Merwick <liam.merw...@oracle.com> > --- > block/vvfat.c | 50 ++++++++++++++++++++++++++++++++++---------------- > 1 file changed, 34 insertions(+), 16 deletions(-) > > diff --git a/block/vvfat.c b/block/vvfat.c > index fc41841a5c3c..263274d9739a 100644 > --- a/block/vvfat.c > +++ b/block/vvfat.c
[...] > @@ -2428,16 +2424,13 @@ static int commit_direntries(BDRVVVFATState* s, > direntry_t* direntry = array_get(&(s->directory), dir_index); > uint32_t first_cluster = dir_index == 0 ? 0 : > begin_of_direntry(direntry); > mapping_t* mapping = find_mapping_for_cluster(s, first_cluster); > - > int factor = 0x10 * s->sectors_per_cluster; > int old_cluster_count, new_cluster_count; > - int current_dir_index = mapping->info.dir.first_dir_index; > - int first_dir_index = current_dir_index; > + int current_dir_index; > + int first_dir_index; > int ret, i; > uint32_t c; > > -DLOG(fprintf(stderr, "commit_direntries for %s, parent_mapping_index %d\n", > mapping->path, parent_mapping_index)); > - > assert(direntry); > assert(mapping); Oh, having moved the condition below the declarations brings an interesting point to light, which is that there is an assertion for it here already. So... > assert(mapping->begin == first_cluster); > @@ -2445,6 +2438,15 @@ DLOG(fprintf(stderr, "commit_direntries for %s, > parent_mapping_index %d\n", mapp > assert(mapping->mode & MODE_DIRECTORY); > assert(dir_index == 0 || is_directory(direntry)); > > + if (mapping == NULL) { > + return -1; > + } > + ...this should just not be added altogether. > +DLOG(fprintf(stderr, "commit_direntries for %s, parent_mapping_index %d\n", > + mapping->path, parent_mapping_index)); Moving this and the following dereferencing statements below that assertion is reasonable, though. I think you should indent the DLOG() while you're at it, though, because there is no reason not to, and the way it is here just violates the coding style. (Disregarding that vvfat.c effectively is a complete violation of the qemu coding style. *cough*) > + > + current_dir_index = mapping->info.dir.first_dir_index; > + first_dir_index = current_dir_index; > mapping->info.dir.parent_mapping_index = parent_mapping_index; > > if (first_cluster == 0) { So with the "if (mapping == NULL) {}" block above (hunk @@2445) dropped and the DLOG() indented: Reviewed-by: Max Reitz <mre...@redhat.com>
signature.asc
Description: OpenPGP digital signature