Repository: dotnet/runtime
File: src/mono/mono/metadata/object.c (C)

Code before:
		return NULL;

	GString *cmd_line = format_cmd_line (num_main_args, main_args);
	return cmd_line ? g_string_free (cmd_line, FALSE) : NULL;
}

void
mono_runtime_run_startup_hooks (void)
{
	if (mono_runtime_get_no_exec ())
		return;

	MonoClass *klass = mono_class_try_load_from_name (mono_defaults.corlib, "System", "StartupHookProvider");
	if (!klass)
		return; // Linked away
	ERROR_DECL (error);
	MonoMethod *method = mono_class_get_method_from_name_checked (klass, "ProcessStartupHooks", -1, 0, error);
	mono_error_cleanup (error);
	if (!method)
		return;

	gchar *startup_hooks_env = g_getenv ("DOTNET_STARTUP_HOOKS");
	MonoString *startup_hooks_arg;
	if (startup_hooks_env) {
		startup_hooks_arg = mono_string_new_checked (startup_hooks_env, error);
		g_free (startup_hooks_env);
	} else {
		startup_hooks_arg = mono_string_empty_internal (mono_domain_get ());
	}

	gpointer args [1];
	args[0] = startup_hooks_arg;

	mono_runtime_invoke_checked (method, NULL, args, error);
	// runtime hooks design doc says not to catch exceptions from the hooks
	mono_error_raise_exception_deprecated (error);
}

gpointer
mono_get_span_data_from_field (MonoClassField *field_handle, MonoType *field_type, MonoType *target_type, gint32 *count)
{
	int swizzle = 1;
	int align;
#if G_BYTE_ORDER != G_LITTLE_ENDIAN
	swizzle = mono_type_size (target_type, &align);
#endif

	int dummy;
	*count = mono_type_size (field_type, &dummy)/mono_type_size (target_type, &align);
	return (gpointer)mono_field_get_rva (field_handle, swizzle);
}

Review comment (bug):
Missing error check after mono_string_new_checked. If the string conversion fails (e.g., due to invalid UTF-8 in the environment variable), the error will be set but never checked. This error will then be incorrectly attributed to mono_runtime_invoke_checked at line 8149. The error should be checked and handled before reusing the error variable. Consider adding error handling similar to the pattern at line 6393 in this file, or calling mono_error_cleanup before line 8149.
```suggestion
		g_free (startup_hooks_env);

		if (!mono_error_ok (error)) {
			mono_error_raise_exception_deprecated (error);
			return;
		}
```

Code after:
		return NULL;

	GString *cmd_line = format_cmd_line (num_main_args, main_args);
	return cmd_line ? g_string_free (cmd_line, FALSE) : NULL;
}

void
mono_runtime_run_startup_hooks (void)
{
	if (mono_runtime_get_no_exec ())
		return;

	MonoClass *klass = mono_class_try_load_from_name (mono_defaults.corlib, "System", "StartupHookProvider");
	if (!klass)
		return; // Linked away
	ERROR_DECL (error);
	MonoMethod *method = mono_class_get_method_from_name_checked (klass, "ProcessStartupHooks", -1, 0, error);
	mono_error_cleanup (error);
	if (!method)
		return;

	gchar *startup_hooks_env = g_getenv ("DOTNET_STARTUP_HOOKS");
	MonoString *startup_hooks_arg;
	if (startup_hooks_env) {
		startup_hooks_arg = mono_string_new_checked (startup_hooks_env, error);
		g_free (startup_hooks_env);
		if (!is_ok (error)) {
			g_warning ("Failed to process $DOTNET_STARTUP_HOOKS environment variable");
			mono_error_cleanup (error);
			return;
		}
	} else {
		startup_hooks_arg = mono_string_empty_internal (mono_domain_get ());
	}

	gpointer args [1];
	args[0] = startup_hooks_arg;

	mono_runtime_invoke_checked (method, NULL, args, error);
	// runtime hooks design doc says not to catch exceptions from the hooks
	mono_error_raise_exception_deprecated (error);
}

gpointer
mono_get_span_data_from_field (MonoClassField *field_handle, MonoType *field_type, MonoType *target_type, gint32 *count)
{
	int swizzle = 1;
	int align;
#if G_BYTE_ORDER != G_LITTLE_ENDIAN
	swizzle = mono_type_size (target_type, &align);
#endif