D-Bus: Simplify out-of-memory reporting
There is no need to have separate wpa_printf() and different error message strings for the unlikely out-of-error messages. Use a helper function, wpas_dbus_error_no_memory(), to get consistent behavior with a one-line call. Signed-off-by: Jouni Malinen <j@w1.fi>
This commit is contained in:
parent
ff1d8104e2
commit
a0caebf397
5 changed files with 41 additions and 95 deletions
|
@ -120,6 +120,13 @@ static DBusMessage * wpas_dbus_error_scan_error(DBusMessage *message,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
DBusMessage * wpas_dbus_error_no_memory(DBusMessage *message)
|
||||||
|
{
|
||||||
|
wpa_printf(MSG_DEBUG, "dbus: Failed to allocate memory");
|
||||||
|
return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static const char * const dont_quote[] = {
|
static const char * const dont_quote[] = {
|
||||||
"key_mgmt", "proto", "pairwise", "auth_alg", "group", "eap",
|
"key_mgmt", "proto", "pairwise", "auth_alg", "group", "eap",
|
||||||
"opensc_engine_path", "pkcs11_engine_path", "pkcs11_module_path",
|
"opensc_engine_path", "pkcs11_engine_path", "pkcs11_module_path",
|
||||||
|
@ -667,13 +674,11 @@ DBusMessage * wpas_dbus_handler_get_interface(DBusMessage *message,
|
||||||
path = wpa_s->dbus_new_path;
|
path = wpa_s->dbus_new_path;
|
||||||
reply = dbus_message_new_method_return(message);
|
reply = dbus_message_new_method_return(message);
|
||||||
if (reply == NULL)
|
if (reply == NULL)
|
||||||
return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
|
return wpas_dbus_error_no_memory(message);
|
||||||
NULL);
|
|
||||||
if (!dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH, &path,
|
if (!dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH, &path,
|
||||||
DBUS_TYPE_INVALID)) {
|
DBUS_TYPE_INVALID)) {
|
||||||
dbus_message_unref(reply);
|
dbus_message_unref(reply);
|
||||||
return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
|
return wpas_dbus_error_no_memory(message);
|
||||||
NULL);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return reply;
|
return reply;
|
||||||
|
@ -1025,11 +1030,7 @@ static int wpas_dbus_get_scan_ssids(DBusMessage *message, DBusMessageIter *var,
|
||||||
if (len != 0) {
|
if (len != 0) {
|
||||||
ssid = os_malloc(len);
|
ssid = os_malloc(len);
|
||||||
if (ssid == NULL) {
|
if (ssid == NULL) {
|
||||||
wpa_printf(MSG_DEBUG,
|
*reply = wpas_dbus_error_no_memory(message);
|
||||||
"%s[dbus]: out of memory. Cannot allocate memory for SSID",
|
|
||||||
__func__);
|
|
||||||
*reply = dbus_message_new_error(
|
|
||||||
message, DBUS_ERROR_NO_MEMORY, NULL);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
os_memcpy(ssid, val, len);
|
os_memcpy(ssid, val, len);
|
||||||
|
@ -1093,12 +1094,8 @@ static int wpas_dbus_get_scan_ies(DBusMessage *message, DBusMessageIter *var,
|
||||||
|
|
||||||
nies = os_realloc(ies, ies_len + len);
|
nies = os_realloc(ies, ies_len + len);
|
||||||
if (nies == NULL) {
|
if (nies == NULL) {
|
||||||
wpa_printf(MSG_DEBUG,
|
|
||||||
"%s[dbus]: out of memory. Cannot allocate memory for IE",
|
|
||||||
__func__);
|
|
||||||
os_free(ies);
|
os_free(ies);
|
||||||
*reply = dbus_message_new_error(
|
*reply = wpas_dbus_error_no_memory(message);
|
||||||
message, DBUS_ERROR_NO_MEMORY, NULL);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
ies = nies;
|
ies = nies;
|
||||||
|
@ -1191,11 +1188,7 @@ static int wpas_dbus_get_scan_channels(DBusMessage *message,
|
||||||
freqs = nfreqs;
|
freqs = nfreqs;
|
||||||
}
|
}
|
||||||
if (freqs == NULL) {
|
if (freqs == NULL) {
|
||||||
wpa_printf(MSG_DEBUG,
|
*reply = wpas_dbus_error_no_memory(message);
|
||||||
"%s[dbus]: out of memory. can't allocate memory for freqs",
|
|
||||||
__func__);
|
|
||||||
*reply = dbus_message_new_error(
|
|
||||||
message, DBUS_ERROR_NO_MEMORY, NULL);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1210,11 +1203,7 @@ static int wpas_dbus_get_scan_channels(DBusMessage *message,
|
||||||
os_free(freqs);
|
os_free(freqs);
|
||||||
freqs = nfreqs;
|
freqs = nfreqs;
|
||||||
if (freqs == NULL) {
|
if (freqs == NULL) {
|
||||||
wpa_printf(MSG_DEBUG,
|
*reply = wpas_dbus_error_no_memory(message);
|
||||||
"%s[dbus]: out of memory. Can't allocate memory for freqs",
|
|
||||||
__func__);
|
|
||||||
*reply = dbus_message_new_error(
|
|
||||||
message, DBUS_ERROR_NO_MEMORY, NULL);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
freqs[freqs_num] = 0;
|
freqs[freqs_num] = 0;
|
||||||
|
@ -1425,8 +1414,7 @@ DBusMessage * wpas_dbus_handler_signal_poll(DBusMessage *message,
|
||||||
nomem:
|
nomem:
|
||||||
if (reply)
|
if (reply)
|
||||||
dbus_message_unref(reply);
|
dbus_message_unref(reply);
|
||||||
reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY, NULL);
|
return wpas_dbus_error_no_memory(message);
|
||||||
return reply;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1506,15 +1494,13 @@ DBusMessage * wpas_dbus_handler_add_network(DBusMessage *message,
|
||||||
|
|
||||||
reply = dbus_message_new_method_return(message);
|
reply = dbus_message_new_method_return(message);
|
||||||
if (reply == NULL) {
|
if (reply == NULL) {
|
||||||
reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
|
reply = wpas_dbus_error_no_memory(message);
|
||||||
NULL);
|
|
||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
if (!dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH, &path,
|
if (!dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH, &path,
|
||||||
DBUS_TYPE_INVALID)) {
|
DBUS_TYPE_INVALID)) {
|
||||||
dbus_message_unref(reply);
|
dbus_message_unref(reply);
|
||||||
reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
|
reply = wpas_dbus_error_no_memory(message);
|
||||||
NULL);
|
|
||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1845,26 +1831,18 @@ DBusMessage * wpas_dbus_handler_add_blob(DBusMessage *message,
|
||||||
|
|
||||||
blob = os_zalloc(sizeof(*blob));
|
blob = os_zalloc(sizeof(*blob));
|
||||||
if (!blob) {
|
if (!blob) {
|
||||||
reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
|
reply = wpas_dbus_error_no_memory(message);
|
||||||
NULL);
|
|
||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
|
|
||||||
blob->data = os_malloc(blob_len);
|
blob->data = os_malloc(blob_len);
|
||||||
if (!blob->data) {
|
blob->name = os_strdup(blob_name);
|
||||||
reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
|
if (!blob->data || !blob->name) {
|
||||||
NULL);
|
reply = wpas_dbus_error_no_memory(message);
|
||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
os_memcpy(blob->data, blob_data, blob_len);
|
os_memcpy(blob->data, blob_data, blob_len);
|
||||||
|
|
||||||
blob->len = blob_len;
|
blob->len = blob_len;
|
||||||
blob->name = os_strdup(blob_name);
|
|
||||||
if (!blob->name) {
|
|
||||||
reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
|
|
||||||
NULL);
|
|
||||||
goto err;
|
|
||||||
}
|
|
||||||
|
|
||||||
wpa_config_set_blob(wpa_s->conf, blob);
|
wpa_config_set_blob(wpa_s->conf, blob);
|
||||||
wpas_notify_blob_added(wpa_s, blob->name);
|
wpas_notify_blob_added(wpa_s, blob->name);
|
||||||
|
@ -1909,39 +1887,21 @@ DBusMessage * wpas_dbus_handler_get_blob(DBusMessage *message,
|
||||||
}
|
}
|
||||||
|
|
||||||
reply = dbus_message_new_method_return(message);
|
reply = dbus_message_new_method_return(message);
|
||||||
if (!reply) {
|
if (!reply)
|
||||||
reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
|
return wpas_dbus_error_no_memory(message);
|
||||||
NULL);
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
|
|
||||||
dbus_message_iter_init_append(reply, &iter);
|
dbus_message_iter_init_append(reply, &iter);
|
||||||
|
|
||||||
if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
|
if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
|
||||||
DBUS_TYPE_BYTE_AS_STRING,
|
DBUS_TYPE_BYTE_AS_STRING,
|
||||||
&array_iter)) {
|
&array_iter) ||
|
||||||
|
!dbus_message_iter_append_fixed_array(&array_iter, DBUS_TYPE_BYTE,
|
||||||
|
&(blob->data), blob->len) ||
|
||||||
|
!dbus_message_iter_close_container(&iter, &array_iter)) {
|
||||||
dbus_message_unref(reply);
|
dbus_message_unref(reply);
|
||||||
reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
|
reply = wpas_dbus_error_no_memory(message);
|
||||||
NULL);
|
|
||||||
goto out;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!dbus_message_iter_append_fixed_array(&array_iter, DBUS_TYPE_BYTE,
|
|
||||||
&(blob->data), blob->len)) {
|
|
||||||
dbus_message_unref(reply);
|
|
||||||
reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
|
|
||||||
NULL);
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!dbus_message_iter_close_container(&iter, &array_iter)) {
|
|
||||||
dbus_message_unref(reply);
|
|
||||||
reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
|
|
||||||
NULL);
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
|
|
||||||
out:
|
|
||||||
return reply;
|
return reply;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2026,9 +1986,7 @@ DBusMessage * wpas_dbus_handler_autoscan(DBusMessage *message,
|
||||||
|
|
||||||
tmp = os_strdup(arg);
|
tmp = os_strdup(arg);
|
||||||
if (tmp == NULL) {
|
if (tmp == NULL) {
|
||||||
reply = dbus_message_new_error(message,
|
reply = wpas_dbus_error_no_memory(message);
|
||||||
DBUS_ERROR_NO_MEMORY,
|
|
||||||
NULL);
|
|
||||||
} else {
|
} else {
|
||||||
os_free(wpa_s->conf->autoscan);
|
os_free(wpa_s->conf->autoscan);
|
||||||
wpa_s->conf->autoscan = tmp;
|
wpa_s->conf->autoscan = tmp;
|
||||||
|
@ -4058,8 +4016,7 @@ DBusMessage * wpas_dbus_handler_subscribe_preq(
|
||||||
|
|
||||||
name = os_strdup(dbus_message_get_sender(message));
|
name = os_strdup(dbus_message_get_sender(message));
|
||||||
if (!name)
|
if (!name)
|
||||||
return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
|
return wpas_dbus_error_no_memory(message);
|
||||||
"out of memory");
|
|
||||||
|
|
||||||
wpa_s->preq_notify_peer = name;
|
wpa_s->preq_notify_peer = name;
|
||||||
|
|
||||||
|
|
|
@ -319,6 +319,7 @@ DBusMessage * wpas_dbus_error_invalid_args(DBusMessage *message,
|
||||||
const char *arg);
|
const char *arg);
|
||||||
DBusMessage * wpas_dbus_error_unknown_error(DBusMessage *message,
|
DBusMessage * wpas_dbus_error_unknown_error(DBusMessage *message,
|
||||||
const char *arg);
|
const char *arg);
|
||||||
|
DBusMessage * wpas_dbus_error_no_memory(DBusMessage *message);
|
||||||
|
|
||||||
DBusMessage * wpas_dbus_handler_subscribe_preq(
|
DBusMessage * wpas_dbus_handler_subscribe_preq(
|
||||||
DBusMessage *message, struct wpa_supplicant *wpa_s);
|
DBusMessage *message, struct wpa_supplicant *wpa_s);
|
||||||
|
|
|
@ -186,8 +186,7 @@ DBusMessage * wpas_dbus_handler_p2p_listen(DBusMessage *message,
|
||||||
|
|
||||||
if (!dbus_message_get_args(message, NULL, DBUS_TYPE_INT32, &timeout,
|
if (!dbus_message_get_args(message, NULL, DBUS_TYPE_INT32, &timeout,
|
||||||
DBUS_TYPE_INVALID))
|
DBUS_TYPE_INVALID))
|
||||||
return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
|
return wpas_dbus_error_no_memory(message);
|
||||||
NULL);
|
|
||||||
|
|
||||||
if (wpa_s->p2p_dev)
|
if (wpa_s->p2p_dev)
|
||||||
wpa_s = wpa_s->p2p_dev;
|
wpa_s = wpa_s->p2p_dev;
|
||||||
|
@ -1784,15 +1783,13 @@ DBusMessage * wpas_dbus_handler_add_persistent_group(
|
||||||
|
|
||||||
reply = dbus_message_new_method_return(message);
|
reply = dbus_message_new_method_return(message);
|
||||||
if (reply == NULL) {
|
if (reply == NULL) {
|
||||||
reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
|
reply = wpas_dbus_error_no_memory(message);
|
||||||
NULL);
|
|
||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
if (!dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH, &path,
|
if (!dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH, &path,
|
||||||
DBUS_TYPE_INVALID)) {
|
DBUS_TYPE_INVALID)) {
|
||||||
dbus_message_unref(reply);
|
dbus_message_unref(reply);
|
||||||
reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
|
reply = wpas_dbus_error_no_memory(message);
|
||||||
NULL);
|
|
||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -303,10 +303,8 @@ DBusMessage * wpas_dbus_handler_wps_start(DBusMessage *message,
|
||||||
}
|
}
|
||||||
|
|
||||||
reply = dbus_message_new_method_return(message);
|
reply = dbus_message_new_method_return(message);
|
||||||
if (!reply) {
|
if (!reply)
|
||||||
return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
|
return wpas_dbus_error_no_memory(message);
|
||||||
NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
dbus_message_iter_init_append(reply, &iter);
|
dbus_message_iter_init_append(reply, &iter);
|
||||||
if (!wpa_dbus_dict_open_write(&iter, &dict_iter) ||
|
if (!wpa_dbus_dict_open_write(&iter, &dict_iter) ||
|
||||||
|
@ -314,8 +312,7 @@ DBusMessage * wpas_dbus_handler_wps_start(DBusMessage *message,
|
||||||
!wpa_dbus_dict_append_string(&dict_iter, "Pin", npin)) ||
|
!wpa_dbus_dict_append_string(&dict_iter, "Pin", npin)) ||
|
||||||
!wpa_dbus_dict_close_write(&iter, &dict_iter)) {
|
!wpa_dbus_dict_close_write(&iter, &dict_iter)) {
|
||||||
dbus_message_unref(reply);
|
dbus_message_unref(reply);
|
||||||
return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
|
return wpas_dbus_error_no_memory(message);
|
||||||
NULL);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return reply;
|
return reply;
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
#include "dbus_common_i.h"
|
#include "dbus_common_i.h"
|
||||||
#include "dbus_new.h"
|
#include "dbus_new.h"
|
||||||
#include "dbus_new_helpers.h"
|
#include "dbus_new_helpers.h"
|
||||||
|
#include "dbus_new_handlers.h"
|
||||||
#include "dbus_dict_helpers.h"
|
#include "dbus_dict_helpers.h"
|
||||||
|
|
||||||
|
|
||||||
|
@ -80,19 +81,13 @@ static DBusMessage * get_all_properties(DBusMessage *message, char *interface,
|
||||||
DBusError error;
|
DBusError error;
|
||||||
|
|
||||||
reply = dbus_message_new_method_return(message);
|
reply = dbus_message_new_method_return(message);
|
||||||
if (reply == NULL) {
|
if (reply == NULL)
|
||||||
wpa_printf(MSG_ERROR, "%s: out of memory creating dbus reply",
|
return wpas_dbus_error_no_memory(message);
|
||||||
__func__);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
dbus_message_iter_init_append(reply, &iter);
|
dbus_message_iter_init_append(reply, &iter);
|
||||||
if (!wpa_dbus_dict_open_write(&iter, &dict_iter)) {
|
if (!wpa_dbus_dict_open_write(&iter, &dict_iter)) {
|
||||||
wpa_printf(MSG_ERROR, "%s: out of memory creating reply",
|
|
||||||
__func__);
|
|
||||||
dbus_message_unref(reply);
|
dbus_message_unref(reply);
|
||||||
return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
|
return wpas_dbus_error_no_memory(message);
|
||||||
"out of memory");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
dbus_error_init(&error);
|
dbus_error_init(&error);
|
||||||
|
@ -108,8 +103,7 @@ static DBusMessage * get_all_properties(DBusMessage *message, char *interface,
|
||||||
|
|
||||||
if (!wpa_dbus_dict_close_write(&iter, &dict_iter)) {
|
if (!wpa_dbus_dict_close_write(&iter, &dict_iter)) {
|
||||||
dbus_message_unref(reply);
|
dbus_message_unref(reply);
|
||||||
return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
|
return wpas_dbus_error_no_memory(message);
|
||||||
"out of memory");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return reply;
|
return reply;
|
||||||
|
|
Loading…
Reference in a new issue