Config: Fix memory leaks

Fix memory leaks that could occur along the error path.
This commit is contained in:
Magnus Vigerlöf 2007-04-10 23:55:36 +03:00 committed by Daniel Stone
parent 82962bbae2
commit 20674dcbb2

View File

@ -110,6 +110,11 @@ configAddDevice(DBusMessage *message, DBusMessageIter *iter, DBusError *error)
options->key = xstrdup("_source"); options->key = xstrdup("_source");
options->value = xstrdup("client/dbus"); options->value = xstrdup("client/dbus");
if(!options->key || !options->value) {
ErrorF("[config] couldn't allocate first key/value pair\n");
ret = BadAlloc;
goto unwind;
}
while (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_ARRAY) { while (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_ARRAY) {
tmpo = (InputOption *) xcalloc(sizeof(InputOption), 1); tmpo = (InputOption *) xcalloc(sizeof(InputOption), 1);
@ -118,6 +123,8 @@ configAddDevice(DBusMessage *message, DBusMessageIter *iter, DBusError *error)
ret = BadAlloc; ret = BadAlloc;
goto unwind; goto unwind;
} }
tmpo->next = options;
options = tmpo;
dbus_message_iter_recurse(iter, &subiter); dbus_message_iter_recurse(iter, &subiter);
@ -132,8 +139,8 @@ configAddDevice(DBusMessage *message, DBusMessageIter *iter, DBusError *error)
tmp); tmp);
MALFORMED_MESSAGE(); MALFORMED_MESSAGE();
} }
tmpo->key = xstrdup(tmp); options->key = xstrdup(tmp);
if (!tmpo->key) { if (!options->key) {
ErrorF("[config] couldn't duplicate key!\n"); ErrorF("[config] couldn't duplicate key!\n");
ret = BadAlloc; ret = BadAlloc;
goto unwind; goto unwind;
@ -148,15 +155,13 @@ configAddDevice(DBusMessage *message, DBusMessageIter *iter, DBusError *error)
dbus_message_iter_get_basic(&subiter, &tmp); dbus_message_iter_get_basic(&subiter, &tmp);
if (!tmp) if (!tmp)
MALFORMED_MESSAGE(); MALFORMED_MESSAGE();
tmpo->value = xstrdup(tmp); options->value = xstrdup(tmp);
if (!tmpo->value) { if (!options->value) {
ErrorF("[config] couldn't duplicate option!\n"); ErrorF("[config] couldn't duplicate option!\n");
ret = BadAlloc; ret = BadAlloc;
goto unwind; goto unwind;
} }
tmpo->next = options;
options = tmpo;
dbus_message_iter_next(iter); dbus_message_iter_next(iter);
} }
@ -164,16 +169,8 @@ configAddDevice(DBusMessage *message, DBusMessageIter *iter, DBusError *error)
if (ret != Success) if (ret != Success)
DebugF("[config] NewInputDeviceRequest failed\n"); DebugF("[config] NewInputDeviceRequest failed\n");
return ret; /* Fall through, must deallocate memory we've allocated */
unwind: unwind:
if (tmpo->key)
xfree(tmpo->key);
if (tmpo->value)
xfree(tmpo->value);
if (tmpo)
xfree(tmpo);
while (options) { while (options) {
tmpo = options; tmpo = options;
options = options->next; options = options->next;