Fix PGLC_localeconv() to handle errors better.
Fix PGLC_localeconv() to handle errors better.

The code was intentionally not very careful about leaking strdup'd
strings in case of an error.  That was forgivable probably, but it
also failed to notice strdup() failures, which could lead to subsequent
null-pointer-dereference crashes, since many callers unsurprisingly
didn't check for null pointers in the struct lconv fields.  An even
worse problem is that it could throw error while we were setlocale'd
to a non-C locale, causing unwanted behavior in subsequent libc calls.

Rewrite to ensure that we cannot throw elog(ERROR) until after we've
restored the previous locale settings, or at least attempted to.
(I'm sorely tempted to make restore failure be a FATAL error, but
will refrain for the moment.)  Having done that, it's not much more
work to ensure that we clean up strdup'd storage on the way out, too.

This code is substantially the same in all supported branches, so
back-patch all the way.

Michael Paquier and Tom Lane

Discussion: <066c0499ef965564fc7dd7bdd0743a3ca30ab7ee@mail.gmail.com>

greenplum-db/gpdb,xinzweb/gpdb,xinzweb/gpdb,adam8157/gpdb,adam8157/gpdb,50wu/gpdb,xinzweb/gpdb,xinzweb/gpdb,xinzweb/gpdb,lisakowen/gpdb,greenplum-db/gpdb,xinzweb/gpdb,lisakowen/gpdb,50wu/gpdb,lisakowen/gpdb,50wu/gpdb,adam8157/gpdb,lisakowen/gpdb,lisakowen/gpdb,greenplum-db/gpdb,adam8157/gpdb,greenplum-db/gpdb,50wu/gpdb,50wu/gpdb,xinzweb/gpdb,50wu/gpdb,lisakowen/gpdb,greenplum-db/gpdb,xinzweb/gpdb,lisakowen/gpdb,greenplum-db/gpdb,adam8157/gpdb,50wu/gpdb,50wu/gpdb,greenplum-db/gpdb,lisakowen/gpdb,adam8157/gpdb,greenplum-db/gpdb,adam8157/gpdb,adam8157/gpdb