Thread: compiler warning read_objtype_from_string()
I'm getting the following compiler warning (using nondefault optimization options): objectaddress.c: In function 'read_objtype_from_string': objectaddress.c:2309:9: error: 'type' may be used uninitialized in this function [-Werror=maybe-uninitialized] return type; The comment for the function says * Return ObjectType for the given object type as given by* getObjectTypeDescription; if no valid ObjectType code exists,but it's a* possible output type from getObjectTypeDescription, return -1. But the claim that it can return -1 does not seem supported by the code. -- Peter Eisentraut http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
Peter Eisentraut wrote: > I'm getting the following compiler warning (using nondefault > optimization options): > > objectaddress.c: In function 'read_objtype_from_string': > objectaddress.c:2309:9: error: 'type' may be used uninitialized in this > function [-Werror=maybe-uninitialized] > return type; Umm. I think it can only be uninitialized if we fall out of the end of the array, in which case we're supposed to throw the ERROR and never return. Is that not working? > The comment for the function says > > * Return ObjectType for the given object type as given by > * getObjectTypeDescription; if no valid ObjectType code exists, but it's a > * possible output type from getObjectTypeDescription, return -1. > > But the claim that it can return -1 does not seem supported by the code. Actually, it is -- but the -1 value comes from the ObjectType array. Perhaps the comment should state that explicitely. -- Álvaro Herrera https://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
Alvaro Herrera <alvherre@2ndquadrant.com> writes: > Peter Eisentraut wrote: >> I'm getting the following compiler warning (using nondefault >> optimization options): >> objectaddress.c: In function 'read_objtype_from_string': >> objectaddress.c:2309:9: error: 'type' may be used uninitialized in this >> function [-Werror=maybe-uninitialized] >> return type; > Umm. I think it can only be uninitialized if we fall out of the end of > the array, in which case we're supposed to throw the ERROR and never > return. Is that not working? I do not think you should assume that the compiler is smart enough to deduce that, nor that all compilers even know ereport(ERROR) doesn't return. Personally I don't see the point of the "type" variable at all, anyway. I would have written this as int i; for (i = 0; i < lengthof(ObjectTypeMap); i++) { if (strcmp(ObjectTypeMap[i].tm_name, objtype) == 0) return ObjectTypeMap[i].tm_type; } ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("unrecognizedobject type \"%s\"", objtype))); return -1; /* keep compiler quiet */ regards, tom lane
Tom Lane wrote: > I do not think you should assume that the compiler is smart enough to > deduce that, nor that all compilers even know ereport(ERROR) doesn't > return. Personally I don't see the point of the "type" variable at > all, anyway. I would have written this as > > [code] Makes sense. I will patch it that way. -- Álvaro Herrera https://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services