all: remove pointless NULL checks

g_malloc(), etc, never return NULL, by API contract. Likewise, by
extension, no other glib function ever returns NULL due to lack of
memory. So remove lots of unnecessary checks (the vast majority of
which would have immediately crashed had they ever run anyway, since
g_set_error(), g_warning(), and nm_log_*() all need to allocate
memory).

https://bugzilla.gnome.org/show_bug.cgi?id=693678
This commit is contained in:
Dan Winship 2013-01-31 15:36:12 -05:00
parent d0bfd47dfb
commit d04f286327
22 changed files with 15 additions and 244 deletions

View file

@ -148,13 +148,6 @@ parse_old_openssl_key_file (const GByteArray *contents,
}
str = g_string_new_len (NULL, end - start);
if (!str) {
g_set_error (error, NM_CRYPTO_ERROR,
NM_CRYPTO_ERR_OUT_OF_MEMORY,
_("Not enough memory to store PEM file data."));
goto parse_error;
}
for (ln = lines; *ln; ln++) {
char *p = *ln;
@ -302,15 +295,9 @@ parse_pkcs8_key_file (const GByteArray *contents,
if (der && length) {
key = g_byte_array_sized_new (length);
if (key) {
g_byte_array_append (key, der, length);
g_assert (key->len == length);
*out_encrypted = encrypted;
} else {
g_set_error_literal (error, NM_CRYPTO_ERROR,
NM_CRYPTO_ERR_OUT_OF_MEMORY,
_("Not enough memory to store private key data."));
}
g_byte_array_append (key, der, length);
g_assert (key->len == length);
*out_encrypted = encrypted;
} else {
g_set_error_literal (error, NM_CRYPTO_ERROR,
NM_CRYPTO_ERR_DECODE_FAILED,
@ -330,14 +317,8 @@ file_to_g_byte_array (const char *filename, GError **error)
if (g_file_get_contents (filename, &contents, &length, error)) {
array = g_byte_array_sized_new (length);
if (array) {
g_byte_array_append (array, (guint8 *) contents, length);
g_assert (array->len == length);
} else {
g_set_error (error, NM_CRYPTO_ERROR,
NM_CRYPTO_ERR_OUT_OF_MEMORY,
_("Not enough memory to store certificate data."));
}
g_byte_array_append (array, (guint8 *) contents, length);
g_assert (array->len == length);
g_free (contents);
}
return array;
@ -368,12 +349,6 @@ convert_iv (const char *src,
num /= 2;
c = g_malloc0 (num + 1);
if (c == NULL) {
g_set_error (error, NM_CRYPTO_ERROR,
NM_CRYPTO_ERR_OUT_OF_MEMORY,
_("Not enough memory to store the IV."));
return NULL;
}
conv[2] = '\0';
for (i = 0; i < num; i++) {
@ -426,12 +401,6 @@ make_des_key (const char *cipher,
}
key = g_malloc0 (digest_len + 1);
if (!key) {
g_set_error (error, NM_CRYPTO_ERROR,
NM_CRYPTO_ERR_OUT_OF_MEMORY,
_("Not enough memory to decrypt private key."));
return NULL;
}
if (!crypto_md5_hash (salt,
salt_len,
@ -489,13 +458,7 @@ decrypt_key (const char *cipher,
error);
if (output && decrypted_len) {
decrypted = g_byte_array_sized_new (decrypted_len);
if (decrypted)
g_byte_array_append (decrypted, (guint8 *) output, decrypted_len);
else {
g_set_error (error, NM_CRYPTO_ERROR,
NM_CRYPTO_ERR_OUT_OF_MEMORY,
_("Not enough memory to store decrypted private key."));
}
g_byte_array_append (decrypted, (guint8 *) output, decrypted_len);
}
out:
@ -616,14 +579,8 @@ extract_pem_cert_data (GByteArray *contents, GError **error)
if (der && length) {
cert = g_byte_array_sized_new (length);
if (cert) {
g_byte_array_append (cert, der, length);
g_assert (cert->len == length);
} else {
g_set_error (error, NM_CRYPTO_ERROR,
NM_CRYPTO_ERR_OUT_OF_MEMORY,
_("Not enough memory to store certificate data."));
}
g_byte_array_append (cert, der, length);
g_assert (cert->len == length);
} else {
g_set_error (error, NM_CRYPTO_ERROR,
NM_CRYPTO_ERR_DECODE_FAILED,

View file

@ -158,12 +158,6 @@ crypto_decrypt (const char *cipher,
}
output = g_malloc0 (data->len);
if (!output) {
g_set_error (error, NM_CRYPTO_ERROR,
NM_CRYPTO_ERR_OUT_OF_MEMORY,
_("Not enough memory for decrypted key buffer."));
return NULL;
}
err = gcry_cipher_open (&ctx, cipher_mech, GCRY_CIPHER_MODE_CBC, 0);
if (err) {
@ -279,12 +273,6 @@ crypto_encrypt (const char *cipher,
padded_buf[data->len + i] = (guint8) (pad_len & 0xFF);
output = g_malloc0 (output_len);
if (!output) {
g_set_error (error, NM_CRYPTO_ERROR,
NM_CRYPTO_ERR_OUT_OF_MEMORY,
_("Could not allocate memory for encrypting."));
return NULL;
}
err = gcry_cipher_open (&ctx, cipher_mech, GCRY_CIPHER_MODE_CBC, 0);
if (err) {

View file

@ -178,12 +178,6 @@ crypto_decrypt (const char *cipher,
}
output = g_malloc0 (data->len);
if (!output) {
g_set_error (error, NM_CRYPTO_ERROR,
NM_CRYPTO_ERR_OUT_OF_MEMORY,
_("Not enough memory for decrypted key buffer."));
return NULL;
}
slot = PK11_GetBestSlot (cipher_mech, NULL);
if (!slot) {
@ -346,12 +340,6 @@ crypto_encrypt (const char *cipher,
padded_buf[data->len + i] = (guint8) (pad_len & 0xFF);
output = g_malloc0 (output_len);
if (!output) {
g_set_error (error, NM_CRYPTO_ERROR,
NM_CRYPTO_ERR_OUT_OF_MEMORY,
_("Could not allocate memory for encrypting."));
return NULL;
}
slot = PK11_GetBestSlot (cipher_mech, NULL);
if (!slot) {

View file

@ -1564,10 +1564,8 @@ file_to_byte_array (const char *filename)
if (g_file_get_contents (filename, &contents, &length, NULL)) {
array = g_byte_array_sized_new (length);
if (array) {
g_byte_array_append (array, (guint8 *) contents, length);
g_assert (array->len == length);
}
g_byte_array_append (array, (guint8 *) contents, length);
g_assert (array->len == length);
g_free (contents);
}
return array;

View file

@ -703,10 +703,6 @@ need_secrets (NMSetting *setting)
GPtrArray *secrets;
secrets = g_ptr_array_sized_new (4);
if (!secrets) {
g_warning ("Not enough memory to create required secrets array.");
return NULL;
}
g_assert (priv->key_mgmt);

View file

@ -2144,13 +2144,6 @@ make_key (const char *salt,
g_return_val_if_fail (out_len != NULL, NULL);
key = g_malloc0 (digest_len + 1);
if (!key) {
g_set_error (error,
NM_CRYPTO_ERROR,
NM_CRYPTO_ERR_OUT_OF_MEMORY,
_("Not enough memory to make encryption key."));
return NULL;
}
if (!crypto_md5_hash (salt, salt_len, password, strlen (password), key, digest_len, error)) {
*out_len = 0;
@ -2255,37 +2248,16 @@ nm_utils_rsa_key_encrypt (const GByteArray *data,
goto out;
pem = g_string_sized_new (enc_len * 2 + 100);
if (!pem) {
g_set_error_literal (error, NM_CRYPTO_ERROR,
NM_CRYPTO_ERR_OUT_OF_MEMORY,
_("Could not allocate memory for PEM file creation."));
goto out;
}
g_string_append (pem, "-----BEGIN RSA PRIVATE KEY-----\n");
g_string_append (pem, "Proc-Type: 4,ENCRYPTED\n");
/* Convert the salt to a hex string */
tmp = utils_bin2hexstr ((const char *) salt, sizeof (salt), 16);
if (!tmp) {
g_set_error (error, NM_CRYPTO_ERROR,
NM_CRYPTO_ERR_OUT_OF_MEMORY,
_("Could not allocate memory for writing IV to PEM file."));
goto out;
}
g_string_append_printf (pem, "DEK-Info: DES-EDE3-CBC,%s\n\n", tmp);
g_free (tmp);
/* Convert the encrypted key to a base64 string */
p = tmp = g_base64_encode ((const guchar *) enc, enc_len);
if (!tmp) {
g_set_error (error, NM_CRYPTO_ERROR,
NM_CRYPTO_ERR_OUT_OF_MEMORY,
_("Could not allocate memory for writing encrypted key to PEM file."));
goto out;
}
left = strlen (tmp);
while (left > 0) {
g_string_append_len (pem, p, (left < 64) ? left : 64);
@ -2298,12 +2270,6 @@ nm_utils_rsa_key_encrypt (const GByteArray *data,
g_string_append (pem, "-----END RSA PRIVATE KEY-----\n");
ret = g_byte_array_sized_new (pem->len);
if (!ret) {
g_set_error (error, NM_CRYPTO_ERROR,
NM_CRYPTO_ERR_OUT_OF_MEMORY,
_("Could not allocate memory for PEM file data."));
goto out;
}
g_byte_array_append (ret, (const unsigned char *) pem->str, pem->len);
if (tmp_password && out_password)
*out_password = g_strdup (tmp_password);

View file

@ -69,10 +69,6 @@ dump_key_to_pem (const char *key, gsize key_len, int key_type)
}
str = g_string_new (NULL);
if (!str) {
g_warning ("Couldn't allocate buffer to write out key.");
goto out;
}
g_string_append (str, start_tag);
g_string_append_c (str, '\n');
@ -122,10 +118,8 @@ file_to_byte_array (const char *filename)
if (g_file_get_contents (filename, &contents, &length, NULL)) {
array = g_byte_array_sized_new (length);
if (array) {
g_byte_array_append (array, (guint8 *) contents, length);
g_assert (array->len == length);
}
g_byte_array_append (array, (guint8 *) contents, length);
g_assert (array->len == length);
g_free (contents);
}
return array;

View file

@ -1118,16 +1118,7 @@ ip4_options_to_config (NMDHCPClient *self)
g_return_val_if_fail (priv->options != NULL, NULL);
ip4_config = nm_ip4_config_new ();
if (!ip4_config) {
nm_log_warn (LOGD_DHCP4, "(%s): couldn't allocate memory for an IP4Config!", priv->iface);
return NULL;
}
addr = nm_ip4_address_new ();
if (!addr) {
nm_log_warn (LOGD_DHCP4, "(%s): couldn't allocate memory for an IP4 Address!", priv->iface);
goto error;
}
str = g_hash_table_lookup (priv->options, "new_ip_address");
if (str && (inet_pton (AF_INET, str, &tmp_addr) > 0)) {
@ -1333,10 +1324,6 @@ ip6_options_to_config (NMDHCPClient *self)
}
ip6_config = nm_ip6_config_new ();
if (!ip6_config) {
nm_log_warn (LOGD_DHCP6, "(%s): couldn't allocate memory for an IP6Config!", priv->iface);
return NULL;
}
str = g_hash_table_lookup (priv->options, "new_ip6_address");
if (str) {

View file

@ -474,10 +474,6 @@ dhclient_start (NMDHCPClient *client,
pid_file = g_strdup_printf (LOCALSTATEDIR "/run/dhclient%s-%s.pid",
ipv6 ? "6" : "",
iface);
if (!pid_file) {
nm_log_warn (log_domain, "(%s): not enough memory for dhcpcd options.", iface);
return -1;
}
/* Kill any existing dhclient from the pidfile */
binary_name = g_path_get_basename (priv->path);
@ -492,10 +488,6 @@ dhclient_start (NMDHCPClient *client,
g_free (priv->lease_file);
priv->lease_file = get_dhclient_leasefile (iface, uuid, ipv6);
if (!priv->lease_file) {
nm_log_warn (log_domain, "(%s): not enough memory for dhclient options.", iface);
return -1;
}
argv = g_ptr_array_new ();
g_ptr_array_add (argv, (gpointer) priv->path);

View file

@ -111,10 +111,6 @@ ip4_start (NMDHCPClient *client,
uuid = nm_dhcp_client_get_uuid (client);
priv->pid_file = g_strdup_printf (NMSTATEDIR "/dhcpcd-%s.pid", iface);
if (!priv->pid_file) {
nm_log_warn (LOGD_DHCP4, "(%s): not enough memory for dhcpcd options.", iface);
return -1;
}
if (!g_file_test (priv->path, G_FILE_TEST_EXISTS)) {
nm_log_warn (LOGD_DHCP4, "%s does not exist.", priv->path);

View file

@ -183,12 +183,6 @@ nm_ip6_device_new (NMIP6Manager *manager,
g_return_val_if_fail (hwaddr_len <= NM_UTILS_HWADDR_LEN_MAX, NULL);
device = g_slice_new0 (NMIP6Device);
if (!device) {
nm_log_err (LOGD_IP6, "(%d): out of memory creating IP6 addrconf object.",
ifindex);
return NULL;
}
device->ifindex = ifindex;
device->iface = nm_netlink_index_to_iface (ifindex);
if (!device->iface) {
@ -1606,19 +1600,7 @@ nm_ip6_manager_get_ip6_config (NMIP6Manager *manager, int ifindex)
static NMIP6Manager *
nm_ip6_manager_new (void)
{
NMIP6Manager *manager;
NMIP6ManagerPrivate *priv;
manager = g_object_new (NM_TYPE_IP6_MANAGER, NULL);
priv = NM_IP6_MANAGER_GET_PRIVATE (manager);
if (!priv->devices) {
nm_log_err (LOGD_IP6, "not enough memory to initialize IP6 manager tables");
g_object_unref (manager);
manager = NULL;
}
return manager;
return g_object_new (NM_TYPE_IP6_MANAGER, NULL);
}
static NMIP6Manager *singleton = NULL;

View file

@ -234,12 +234,6 @@ parse_state_file (const char *filename,
g_return_val_if_fail (wimax_enabled != NULL, FALSE);
state_file = g_key_file_new ();
if (!state_file) {
g_set_error (error, NM_CONFIG_ERROR, NM_CONFIG_ERROR_NO_MEMORY,
"Not enough memory to load state file %s.", filename);
return FALSE;
}
g_key_file_set_list_separator (state_file, ',');
if (!g_key_file_load_from_file (state_file, filename, G_KEY_FILE_KEEP_COMMENTS, &tmp_error)) {
gboolean ret = FALSE;

View file

@ -147,12 +147,6 @@ fill_from_file (NMConfig *config,
}
kf = g_key_file_new ();
if (!kf) {
g_set_error (error, NM_CONFIG_ERROR, NM_CONFIG_ERROR_NO_MEMORY,
"Not enough memory to load config file %s", path);
return FALSE;
}
g_key_file_set_list_separator (kf, ',');
if (g_key_file_load_from_file (kf, path, G_KEY_FILE_NONE, error)) {
config->path = g_strdup (path);

View file

@ -2535,11 +2535,6 @@ supplicant_iface_connection_error_cb (NMSupplicantInterface * iface,
priv = NM_DEVICE_WIFI_GET_PRIVATE (self);
cb_data = g_slice_new0 (struct iface_con_error_cb_data);
if (cb_data == NULL) {
nm_log_err (LOGD_WIFI, "Not enough memory to process supplicant connection error.");
return;
}
cb_data->self = self;
cb_data->name = g_strdup (name);
cb_data->message = g_strdup (message);

View file

@ -2732,9 +2732,6 @@ ip6_use_tempaddr (void)
sysctl_data = g_strdup_printf ("%s%s", group_name, contents);
keyfile = g_key_file_new ();
if (keyfile == NULL)
goto done;
if (!g_key_file_load_from_data (keyfile, sysctl_data, len + strlen (group_name), G_KEY_FILE_NONE, NULL))
goto done;

View file

@ -1437,8 +1437,6 @@ write_value_to_state_file (const char *filename,
FALSE);
key_file = g_key_file_new ();
if (!key_file)
return FALSE;
g_key_file_set_list_separator (key_file, ',');
g_key_file_load_from_file (key_file, filename, G_KEY_FILE_KEEP_COMMENTS, NULL);

View file

@ -1393,11 +1393,6 @@ is_mac_auto_wired_blacklisted (NMSettings *self, const GByteArray *mac)
return FALSE;
config = g_key_file_new ();
if (!config) {
nm_log_warn (LOGD_SETTINGS, "not enough memory to load config file.");
return FALSE;
}
g_key_file_set_list_separator (config, ',');
if (!g_key_file_load_from_file (config, priv->config_file, G_KEY_FILE_NONE, NULL))
goto out;
@ -1465,8 +1460,6 @@ default_wired_deleted (NMDefaultWiredConnection *wired,
goto cleanup;
config = g_key_file_new ();
if (!config)
goto cleanup;
if (nm_connection_get_setting_wired (NM_CONNECTION (wired)))
hwaddr_type = ARPHRD_ETHER;

View file

@ -163,7 +163,6 @@ svEscape(const char *s) {
newlen = slen + mangle - newline + 3; /* 3 is extra ""\0 */
new = g_malloc0(newlen);
if (!new) return NULL;
j = 0;
new[j++] = '"';

View file

@ -926,12 +926,7 @@ test_read_wired_static_no_prefix (guint32 expected_prefix)
const char *tmp;
file = g_strdup_printf (TEST_IFCFG_STATIC_NO_PREFIX "-%u", expected_prefix);
ASSERT (file != NULL,
"wired-static-no-prefix-read", "failed to create path to file");
expected_id = g_strdup_printf ("System test-wired-static-no-prefix-%u", expected_prefix);
ASSERT (expected_id != NULL,
"wired-static-no-prefix-read", "failed to expected connection ID");
connection = connection_from_file (file,
NULL,
@ -7713,10 +7708,8 @@ file_to_byte_array (const char *filename)
if (g_file_get_contents (filename, &contents, &length, NULL)) {
array = g_byte_array_sized_new (length);
if (array) {
g_byte_array_append (array, (guint8 *) contents, length);
g_assert (array->len == length);
}
g_byte_array_append (array, (guint8 *) contents, length);
g_assert (array->len == length);
g_free (contents);
}
return array;

View file

@ -143,7 +143,6 @@ utils_single_quote_string (const char *str)
drop++;
}
new_str = g_malloc0 (slen + extra - drop + 4); /* 4 is for $''\0*/
if (!new_str) return NULL;
if (extra > 0)
new_str[j++] = '$';
@ -179,7 +178,6 @@ utils_single_unquote_string (const char *str)
slen = strlen (str);
new_str = g_malloc0 (slen + 1);
if (!new_str) return NULL;
if ( (slen >= 2 && (str[0] == dq_char || str[0] == q_char) && str[0] == str[slen-1])
|| (slen >= 3 && str[0] == '$' && str[1] == q_char && str[1] == str[slen-1])) {

View file

@ -140,13 +140,6 @@ write_secret_file (const char *path,
gboolean success = FALSE;
tmppath = g_malloc0 (strlen (path) + 10);
if (!tmppath) {
g_set_error (error, IFCFG_PLUGIN_ERROR, 0,
"Could not allocate memory for temporary file for '%s'",
path);
return FALSE;
}
memcpy (tmppath, path, strlen (path));
strcat (tmppath, ".XXXXXX");

View file

@ -136,17 +136,7 @@ nm_supplicant_config_add_option_with_type (NMSupplicantConfig *self,
}
opt = g_slice_new0 (ConfigOption);
if (opt == NULL) {
nm_log_warn (LOGD_SUPPLICANT, "Couldn't allocate memory for new config option.");
return FALSE;
}
opt->value = g_malloc0 ((sizeof (char) * len) + 1);
if (opt->value == NULL) {
nm_log_warn (LOGD_SUPPLICANT, "Couldn't allocate memory for new config option value.");
g_slice_free (ConfigOption, opt);
return FALSE;
}
memcpy (opt->value, value, len);
opt->len = len;
@ -207,27 +197,10 @@ nm_supplicant_config_add_blob (NMSupplicantConfig *self,
}
blob = g_byte_array_sized_new (value->len);
if (!blob) {
nm_log_warn (LOGD_SUPPLICANT, "Couldn't allocate memory for new config blob.");
return FALSE;
}
g_byte_array_append (blob, value->data, value->len);
opt = g_slice_new0 (ConfigOption);
if (opt == NULL) {
nm_log_warn (LOGD_SUPPLICANT, "Couldn't allocate memory for new config option.");
g_byte_array_free (blob, TRUE);
return FALSE;
}
opt->value = g_strdup_printf ("blob://%s", blobid);
if (opt->value == NULL) {
nm_log_warn (LOGD_SUPPLICANT, "Couldn't allocate memory for new config option value.");
g_byte_array_free (blob, TRUE);
g_slice_free (ConfigOption, opt);
return FALSE;
}
opt->len = strlen (opt->value);
opt->type = type;