Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change: move assignments out of if conditions (base) #893

Merged
merged 2 commits into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions base/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ if (BUILD_TESTS)

set (NETWORKING_TEST_LINKER_WRAP_OPTIONS "-Wl,-wrap,g_io_channel_new_file,-wrap,g_io_channel_shutdown")
add_unit_test (networking-test networking_tests.c gvm_base_shared ${CGREEN_LIBRARIES} ${GLIB_LDFLAGS} ${LINKER_HARDENING_FLAGS} ${NETWORKING_TEST_LINKER_WRAP_OPTIONS})
add_unit_test (pwpolicy-test pwpolicy_tests.c ${GLIB_LDFLAGS} ${LINKER_HARDENING_FLAGS})
add_unit_test (version-test version_tests.c ${GLIB_LDFLAGS} ${LINKER_HARDENING_FLAGS})
add_unit_test (nvti-test nvti_tests.c ${GLIB_LDFLAGS} ${LINKER_HARDENING_FLAGS})
add_unit_test (hosts-test hosts_tests.c gvm_base_shared gvm_util_shared ${GLIB_LDFLAGS} ${LINKER_HARDENING_FLAGS})
Expand Down
5 changes: 3 additions & 2 deletions base/drop_privileges.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,10 @@

if (geteuid () == 0)
{
struct passwd *user_pw = NULL;
struct passwd *user_pw;

if ((user_pw = getpwnam (username)))
user_pw = getpwnam (username);

Check warning on line 69 in base/drop_privileges.c

View check run for this annotation

Codecov / codecov/patch

base/drop_privileges.c#L69

Added line #L69 was not covered by tests
if (user_pw)
{
if (initgroups (username, user_pw->pw_gid) != 0)
return drop_privileges_error (
Expand Down
21 changes: 14 additions & 7 deletions base/hosts.c
Original file line number Diff line number Diff line change
Expand Up @@ -1053,7 +1053,8 @@
{
gchar *name;

if ((name = gvm_host_value_str (hosts->hosts[i])))
name = gvm_host_value_str (hosts->hosts[i]);
if (name)
{
gvm_host_t *host, *removed = hosts->hosts[i];

Expand Down Expand Up @@ -1589,7 +1590,8 @@
{
gchar *name;

if ((name = gvm_host_value_str (excluded_hosts->hosts[i])))
name = gvm_host_value_str (excluded_hosts->hosts[i]);

Check warning on line 1593 in base/hosts.c

View check run for this annotation

Codecov / codecov/patch

base/hosts.c#L1593

Added line #L1593 was not covered by tests
if (name)
g_hash_table_insert (name_table, name, hosts);
}

Expand All @@ -1598,7 +1600,8 @@
{
gchar *name;

if ((name = gvm_host_value_str (hosts->hosts[i])))
name = gvm_host_value_str (hosts->hosts[i]);

Check warning on line 1603 in base/hosts.c

View check run for this annotation

Codecov / codecov/patch

base/hosts.c#L1603

Added line #L1603 was not covered by tests
if (name)
{
if (g_hash_table_lookup (name_table, name))
{
Expand Down Expand Up @@ -1667,7 +1670,8 @@
{
gchar *name;

if ((name = gvm_host_value_str (denied_hosts->hosts[i])))
name = gvm_host_value_str (denied_hosts->hosts[i]);
if (name)
g_hash_table_insert (name_deny_table, name, hosts);
}
}
Expand All @@ -1682,7 +1686,8 @@
{
gchar *name;

if ((name = gvm_host_value_str (allowed_hosts->hosts[i])))
name = gvm_host_value_str (allowed_hosts->hosts[i]);
if (name)
g_hash_table_insert (name_allow_table, name, hosts);
}
}
Expand All @@ -1693,7 +1698,8 @@
{
gchar *name;

if ((name = gvm_host_value_str (hosts->hosts[i])))
name = gvm_host_value_str (hosts->hosts[i]);
if (name)
{
if (denied_hosts != NULL
&& g_hash_table_lookup (name_deny_table, name))
Expand Down Expand Up @@ -2022,7 +2028,8 @@
{
gchar *name;

if ((name = gvm_host_reverse_lookup (hosts->hosts[i])))
name = gvm_host_reverse_lookup (hosts->hosts[i]);

Check warning on line 2031 in base/hosts.c

View check run for this annotation

Codecov / codecov/patch

base/hosts.c#L2031

Added line #L2031 was not covered by tests
if (name)
{
if (g_hash_table_lookup (name_table, name))
{
Expand Down
6 changes: 4 additions & 2 deletions base/networking.c
Original file line number Diff line number Diff line change
Expand Up @@ -949,7 +949,8 @@

interface = g_strndup (items_in_line[0], 64);
/* Cut interface str after ":" if IP aliasing is used. */
if ((char_p = strchr (interface, ':')))
char_p = strchr (interface, ':');
if (char_p)
{
*char_p = '\0';
}
Expand Down Expand Up @@ -1292,7 +1293,8 @@
return NULL;

// get a connected udp socket
if ((sockfd = get_connected_udp_sock (target_addr)) < 0)
sockfd = get_connected_udp_sock (target_addr);

Check warning on line 1296 in base/networking.c

View check run for this annotation

Codecov / codecov/patch

base/networking.c#L1296

Added line #L1296 was not covered by tests
if (sockfd < 0)
return NULL;
// get socked address which is the addr of the interface we want to get
out_iface_addr.ss_family = family;
Expand Down
3 changes: 2 additions & 1 deletion base/pwpolicy.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,8 @@
else if (*line == '#' && line[1] == '+') /* Processing instruction. */
{
line += 2;
if ((p = is_keyword (line, "desc")))
p = is_keyword (line, "desc");

Check warning on line 244 in base/pwpolicy.c

View check run for this annotation

Codecov / codecov/patch

base/pwpolicy.c#L244

Added line #L244 was not covered by tests
if (p)
{
g_free (*descp);
if (*p)
Expand Down
79 changes: 79 additions & 0 deletions base/pwpolicy_tests.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/* SPDX-FileCopyrightText: 2019-2023 Greenbone AG
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "pwpolicy.c"

#include <cgreen/assertions.h>
#include <cgreen/cgreen.h>
#include <cgreen/constraint_syntax_helpers.h>
#include <cgreen/internal/c_assertions.h>
#include <cgreen/mocks.h>

Describe (pwpolicy);
BeforeEach (pwpolicy)
{
}

AfterEach (pwpolicy)
{
}

/* parse_pattern_line */

Ensure (pwpolicy, parse_pattern_line_allows)
{
char *desc, *error, *line;

desc = NULL;
line = g_strdup ("password");
error = parse_pattern_line (line, "test", 111, &desc, "passw0rd", "name");
assert_that (error, is_null);
g_free (desc);
g_free (line);
}

Ensure (pwpolicy, parse_pattern_line_refuses)
{
char *desc, *error, *line;

desc = NULL;
line = g_strdup ("password");
error = parse_pattern_line (line, "test", 111, &desc, "password", "name");
assert_that (error, is_not_null);
g_free (desc);
g_free (error);
g_free (line);
}

Ensure (pwpolicy, parse_pattern_line_comment)
{
char *desc, *error, *line;

desc = NULL;
line = g_strdup ("# password");
error = parse_pattern_line (line, "test", 111, &desc, "password", "name");
assert_that (error, is_null);
g_free (desc);
g_free (error);
g_free (line);
}

/* Test suite. */
int
main (int argc, char **argv)
{
TestSuite *suite;

suite = create_test_suite ();

add_test_with_context (suite, pwpolicy, parse_pattern_line_allows);
add_test_with_context (suite, pwpolicy, parse_pattern_line_refuses);
add_test_with_context (suite, pwpolicy, parse_pattern_line_comment);

if (argc > 1)
return run_single_test (suite, argv[1], create_text_reporter ());

Check warning on line 76 in base/pwpolicy_tests.c

View check run for this annotation

Codecov / codecov/patch

base/pwpolicy_tests.c#L76

Added line #L76 was not covered by tests

return run_test_suite (suite, create_text_reporter ());
}