From 599e979d90327c1e10031f992cb2be2cda414825 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Tue, 26 Jun 2018 17:35:04 +0200 Subject: Fix bug in DNA_struct_elem_find when checking the first field find_elem(olddata=NULL) doesn't work reliably for existence checks; it will return NULL both when the field is found at offset 0 and when it is not found at all. --- source/blender/makesdna/intern/dna_genfile.c | 41 ++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) (limited to 'source') diff --git a/source/blender/makesdna/intern/dna_genfile.c b/source/blender/makesdna/intern/dna_genfile.c index 32e41863ad1..d8d29c9c812 100644 --- a/source/blender/makesdna/intern/dna_genfile.c +++ b/source/blender/makesdna/intern/dna_genfile.c @@ -893,11 +893,48 @@ static int elem_strcmp(const char *name, const char *oname) return 0; } +/** + * Returns whether the specified field exists according to the struct format + * pointed to by old. + * + * \param sdna Old SDNA + * \param type Current field type name + * \param name Current field name + * \param old Pointer to struct information in sdna + * \return true when existsing, false otherwise. + */ +static bool elem_exists( + const SDNA *sdna, + const char *type, + const char *name, + const short *old) +{ + int a, elemcount; + const char *otype, *oname; + + /* in old is the old struct */ + elemcount = old[1]; + old += 2; + for (a = 0; a < elemcount; a++, old += 2) { + otype = sdna->types[old[0]]; + oname = sdna->names[old[1]]; + + if (elem_strcmp(name, oname) == 0) { /* name equal */ + return strcmp(type, otype) == 0; /* type equal */ + } + } + return false; +} + /** * Returns the address of the data for the specified field within olddata * according to the struct format pointed to by old, or NULL if no such * field can be found. * + * Passing olddata=NULL doesn't work reliably for existence checks; it will + * return NULL both when the field is found at offset 0 and when it is not + * found at all. For field existence checks, use elem_exists() instead. + * * \param sdna Old SDNA * \param type Current field type name * \param name Current field name @@ -1307,9 +1344,9 @@ bool DNA_struct_elem_find(const SDNA *sdna, const char *stype, const char *varty if (SDNAnr != -1) { const short * const spo = sdna->structs[SDNAnr]; - const char * const cp = find_elem(sdna, vartype, name, spo, NULL, NULL); + const bool found = elem_exists(sdna, vartype, name, spo); - if (cp) { + if (found) { return true; } } -- cgit v1.2.3