lib: assert more invariants
authorLuca BRUNO <luca.bruno@coreos.com>
Thu, 24 Nov 2022 08:13:13 +0000 (08:13 +0000)
committerLuca BRUNO <luca.bruno@coreos.com>
Thu, 24 Nov 2022 09:51:10 +0000 (09:51 +0000)
This converts a few more safety checks into either plain GErrors
or hard assertions.

src/libostree/ostree-async-progress.c
src/libostree/ostree-checksum-input-stream.c
src/libostree/ostree-date-utils.c
src/libostree/ostree-gpg-verifier.c
src/libostree/ostree-repo-static-delta-compilation.c
src/libostree/ostree-sysroot-cleanup.c
src/libostree/ostree-sysroot-deploy.c
src/libostree/ostree-sysroot-upgrader.c

index edbde11c5430f1c3d361f67d051b15c8d9fdc65b..8a780372a1498ce1e6f0af07e8101f26b4e8d33b 100644 (file)
@@ -128,10 +128,12 @@ GVariant *
 ostree_async_progress_get_variant (OstreeAsyncProgress *self,
                                    const char          *key)
 {
+  g_assert (OSTREE_IS_ASYNC_PROGRESS (self));
+
   GVariant *rval;
 
-  g_return_val_if_fail (OSTREE_IS_ASYNC_PROGRESS (self), NULL);
-  g_return_val_if_fail (key != NULL, NULL);
+  if (key == NULL)
+    return NULL; /* Early return */
 
   g_mutex_lock (&self->lock);
   rval = g_hash_table_lookup (self->values, GUINT_TO_POINTER (g_quark_from_string (key)));
@@ -437,8 +439,8 @@ void
 ostree_async_progress_copy_state (OstreeAsyncProgress *self,
                                   OstreeAsyncProgress *dest)
 {
-  g_return_if_fail (OSTREE_IS_ASYNC_PROGRESS (self));
-  g_return_if_fail (OSTREE_IS_ASYNC_PROGRESS (dest));
+  g_assert (OSTREE_IS_ASYNC_PROGRESS (self));
+  g_assert (OSTREE_IS_ASYNC_PROGRESS (dest));
 
   g_mutex_lock (&self->lock);
 
index 7cdf2048f4a99a2917a51e748abfa3878301b3e7..9747515e89ef1dca2ac1ca498b7a2337cd3adb22 100644 (file)
@@ -124,16 +124,15 @@ OstreeChecksumInputStream *
 ostree_checksum_input_stream_new (GInputStream    *base,
                                   GChecksum       *checksum)
 {
-  OstreeChecksumInputStream *stream;
+  g_assert (G_IS_INPUT_STREAM (base));
 
-  g_return_val_if_fail (G_IS_INPUT_STREAM (base), NULL);
+  OstreeChecksumInputStream *stream = g_object_new (
+      OSTREE_TYPE_CHECKSUM_INPUT_STREAM,
+      "base-stream", base,
+      "checksum", checksum,
+      NULL);
 
-  stream = g_object_new (OSTREE_TYPE_CHECKSUM_INPUT_STREAM,
-                        "base-stream", base,
-                         "checksum", checksum,
-                        NULL);
-
-  return (OstreeChecksumInputStream*) (stream);
+  return stream;
 }
 
 static gssize
index a71569119f27dab1f85e8d70f164e2ed7115ad20..7c078a96e1435719b1efeabcf1511cd1d0186c28 100644 (file)
@@ -40,8 +40,10 @@ parse_uint (const char *buf,
   const char *end_ptr = NULL;
   gint saved_errno = 0;
 
-  g_return_val_if_fail (n_digits == 2 || n_digits == 4, FALSE);
-  g_return_val_if_fail (out != NULL, FALSE);
+  g_assert (out != NULL);
+
+  if(!(n_digits == 2 || n_digits == 4))
+    return FALSE;
 
   errno = 0;
   number = g_ascii_strtoull (buf, (gchar **)&end_ptr, 10);
index 1c8d94646abbeca3f90c74b39c057cdd482d8e3d..8301907ccb08f27b74eb48d9156d66b5469e9147 100644 (file)
@@ -549,7 +549,7 @@ _ostree_gpg_verifier_add_global_keyring_dir (OstreeGpgVerifier  *self,
                                              GCancellable       *cancellable,
                                              GError            **error)
 {
-  g_return_val_if_fail (OSTREE_IS_GPG_VERIFIER (self), FALSE);
+  g_assert (OSTREE_IS_GPG_VERIFIER (self));
 
   const char *global_keyring_path = g_getenv ("OSTREE_GPG_HOME");
   if (global_keyring_path == NULL)
index dece876ee23e04659c73a9a71f0e49ab1230119a..1e3af0d8618d05d4a9cbc21632c20c4e5b102afd 100644 (file)
@@ -1384,7 +1384,8 @@ ostree_repo_static_delta_generate (OstreeRepo                   *self,
   builder.max_chunk_size_bytes = ((guint64)max_chunk_size) * 1000 * 1000;
 
   (void) g_variant_lookup (params, "endianness", "u", &endianness);
-  g_return_val_if_fail (endianness == G_BIG_ENDIAN || endianness == G_LITTLE_ENDIAN, FALSE);
+  if (!(endianness == G_BIG_ENDIAN || endianness == G_LITTLE_ENDIAN))
+    return glnx_throw (error, "Invalid endianness parameter");
 
   builder.swap_endian = endianness != G_BYTE_ORDER;
   builder.parts = builder_parts;
index 3471cac72af7381c82725b6592a6d147a234dbf1..51f87b03e0201fbe7ef9beb4890dcc15180d14cf 100644 (file)
@@ -578,8 +578,8 @@ _ostree_sysroot_cleanup_internal (OstreeSysroot              *self,
                                   GCancellable               *cancellable,
                                   GError                    **error)
 {
-  g_return_val_if_fail (OSTREE_IS_SYSROOT (self), FALSE);
-  g_return_val_if_fail (self->loadstate == OSTREE_SYSROOT_LOAD_STATE_LOADED, FALSE);
+  g_assert (OSTREE_IS_SYSROOT (self));
+  g_assert (self->loadstate == OSTREE_SYSROOT_LOAD_STATE_LOADED);
 
   if (!_ostree_sysroot_ensure_writable (self, error))
     return FALSE;
index 26b07080b10bb8a54e502fb93ed23035e79d64c1..482124e2b64166a1a2a13b9ea7e32def23f8fd40 100644 (file)
@@ -2755,7 +2755,7 @@ sysroot_initialize_deployment (OstreeSysroot     *self,
                                GCancellable      *cancellable,
                                GError           **error)
 {
-  g_return_val_if_fail (osname != NULL || self->booted_deployment != NULL, FALSE);
+  g_assert (osname != NULL || self->booted_deployment != NULL);
 
   if (osname == NULL)
     osname = ostree_deployment_get_osname (self->booted_deployment);
@@ -3198,8 +3198,8 @@ ostree_sysroot_stage_overlay_initrd (OstreeSysroot  *self,
                                      GCancellable   *cancellable,
                                      GError        **error)
 {
-  g_return_val_if_fail (fd != -1, FALSE);
-  g_return_val_if_fail (out_checksum != NULL, FALSE);
+  g_assert (fd != -1);
+  g_assert (out_checksum != NULL);
 
   if (!glnx_shutil_mkdir_p_at (AT_FDCWD, _OSTREE_SYSROOT_RUNSTATE_STAGED_INITRDS_DIR,
                                0755, cancellable, error))
index 58f8ecd05aae8318c223d605d2fcc3e939fd0b4e..b8d2d0c9c76e6931232fdd65e4c5c6d5cef0bbf8 100644 (file)
@@ -340,9 +340,9 @@ ostree_sysroot_upgrader_get_origin (OstreeSysrootUpgrader *self)
 GKeyFile *
 ostree_sysroot_upgrader_dup_origin (OstreeSysrootUpgrader *self)
 {
-  GKeyFile *copy = NULL;
+  g_assert (OSTREE_IS_SYSROOT_UPGRADER (self));
 
-  g_return_val_if_fail (OSTREE_IS_SYSROOT_UPGRADER (self), NULL);
+  GKeyFile *copy = NULL;
 
   if (self->origin != NULL)
     {