Skip to content

Commit c48a4e4

Browse files
committed
address sonarqube issues
1 parent 033a8bd commit c48a4e4

9 files changed

+58
-54
lines changed

src/common/memusage.c

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
#include <stdio.h>
2929
#include <stddef.h>
30+
#include <stdlib.h>
3031
#include <string.h>
3132
#include <ctype.h>
3233
#include <errno.h>
@@ -60,29 +61,24 @@
6061
#include "bfind.h"
6162

6263
#if defined(OS_LINUX) || defined(__FreeBSD__) || defined(OS_SOLARIS) || defined(OSCAP_TEST_READ_COMMON_SIZET)
63-
static int read_common_sizet(void *szp, char *strval)
64+
static int read_common_sizet(void *szp, const char *strval)
6465
{
6566
char *end;
67+
long long value;
6668

6769
if (szp == NULL || strval == NULL) {
6870
return -1;
6971
}
7072

71-
end = strchr(strval, ' ');
72-
73-
if (end == NULL)
74-
return (-1);
75-
76-
*end = '\0';
77-
7873
errno = 0;
79-
*(size_t *)szp = strtoll(strval, NULL, 10);
74+
value = strtoll(strval, &end, 10);
8075

81-
if (errno == EINVAL ||
82-
errno == ERANGE)
83-
return (-1);
76+
if (end == strval || !isspace((unsigned char)*end) ||
77+
errno == EINVAL || errno == ERANGE || value < 0)
78+
return -1;
8479

85-
return (0);
80+
*(size_t *)szp = (size_t)value;
81+
return 0;
8682
}
8783

8884
#endif
@@ -91,7 +87,7 @@ static int read_common_sizet(void *szp, char *strval)
9187

9288
struct stat_parser {
9389
char *keyword;
94-
int (*storval)(void *, char *);
90+
int (*storval)(void *, const char *);
9591
ptrdiff_t offset;
9692
};
9793

tests/API/XCCDF/unittests/test_xccdf_result_sysinfo_platform.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ int main(void)
2727
facts = xccdf_result_get_target_facts(result);
2828

2929
while (xccdf_target_fact_iterator_has_more(facts)) {
30-
struct xccdf_target_fact *fact = xccdf_target_fact_iterator_next(facts);
30+
const struct xccdf_target_fact *fact = xccdf_target_fact_iterator_next(facts);
3131
const char *name = xccdf_target_fact_get_name(fact);
3232

3333
if (name == NULL)

tests/API/XCCDF/unittests/test_xccdf_result_sysinfo_platform.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
. $builddir/tests/test_common.sh
44

5-
if [ -n "${CUSTOM_OSCAP+x}" ] ; then
5+
if [[ -n "${CUSTOM_OSCAP+x}" ]] ; then
66
exit 255
77
fi
88

tests/API/probes/test_memusage_freebsd_stress.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
. $builddir/tests/test_common.sh
44

5-
if [ "$(uname)" != "FreeBSD" ] ; then
5+
if [[ "$(uname)" != "FreeBSD" ]] ; then
66
exit 255
77
fi
88

tests/API/probes/test_memusage_platform.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
. $builddir/tests/test_common.sh
44

5-
if [ -n "${CUSTOM_OSCAP+x}" ] ; then
5+
if [[ -n "${CUSTOM_OSCAP+x}" ]] ; then
66
exit 255
77
fi
88

tests/probes/password/test_probes_password.xml.sh

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
#!/usr/bin/env bash
22

3-
passwd_file=`mktemp`
3+
passwd_file=$(mktemp)
4+
readonly AWK_PRINT_FIRST_FIELD='{print $1}'
45

56
# getpwent returns duplicate entries for root and nobody users
67
# due to a bug in systemd-userdb.service that occurs
78
# in systemd 245
89
# https://github.com/systemd/systemd/issues/15160
9-
if which systemctl &>/dev/null && \
10-
[[ `systemctl --version | grep "systemd 245"` =~ "245" ]] ; then
10+
if command -v systemctl >/dev/null 2>&1 && \
11+
systemctl --version | grep -q "systemd 245" ; then
1112
grep -Ev '^(root|nobody)' /etc/passwd > "$passwd_file"
1213
else
13-
case `uname` in
14+
case "$(uname)" in
1415
# BSD passwd files may contain comments that are ignored by getpwent(3).
1516
Darwin|FreeBSD)
1617
grep -Ev '^(#|$)' /etc/passwd > "$passwd_file"
@@ -22,51 +23,58 @@ else
2223
esac
2324
fi
2425

25-
LINES_COUNT=`cat "$passwd_file" | wc -l`
26+
LINES_COUNT=$(wc -l < "$passwd_file")
2627

2728
function getField {
28-
LINE=`sed -n "${I}p" "$passwd_file"`
29-
case $1 in
29+
local field_name="$1"
30+
local line_number="$2"
31+
local line
32+
local username
33+
34+
line=$(sed -n "${line_number}p" "$passwd_file")
35+
username=$(echo "$line" | awk -F':' "$AWK_PRINT_FIRST_FIELD")
36+
37+
case "$field_name" in
3038
'username' )
31-
echo $LINE | awk -F':' '{print $1}'
39+
echo "$username"
3240
;;
3341
'password' )
34-
echo $LINE | awk -F':' '{print $2}'
42+
echo "$line" | awk -F':' '{print $2}'
3543
;;
3644
'user_id' )
37-
case `uname` in
45+
case "$(uname)" in
3846
FreeBSD)
39-
id -u "`echo $LINE | awk -F':' '{print $1}'`"
47+
id -u "$username"
4048
;;
4149
Darwin)
42-
id -u "`echo $LINE | awk -F':' '{print $1}'`"
50+
id -u "$username"
4351
;;
4452
*)
45-
echo $LINE | awk -F':' '{print $3}'
53+
echo "$line" | awk -F':' '{print $3}'
4654
;;
4755
esac
4856
;;
4957
'group_id' )
50-
case `uname` in
58+
case "$(uname)" in
5159
FreeBSD)
52-
id -g "`echo $LINE | awk -F':' '{print $1}'`"
60+
id -g "$username"
5361
;;
5462
Darwin)
55-
id -g "`echo $LINE | awk -F':' '{print $1}'`"
63+
id -g "$username"
5664
;;
5765
*)
58-
echo $LINE | awk -F':' '{print $4}'
66+
echo "$line" | awk -F':' '{print $4}'
5967
;;
6068
esac
6169
;;
6270
'gcos' )
63-
echo $LINE | awk -F':' '{gsub(/&/,"&amp;",$5); print $5}'
71+
echo "$line" | awk -F':' '{gsub(/&/,"&amp;",$5); print $5}'
6472
;;
6573
'home_dir' )
66-
echo $LINE | awk -F':' '{print $6}'
74+
echo "$line" | awk -F':' '{print $6}'
6775
;;
6876
'login_shell' )
69-
echo $LINE | awk -F':' '{print $7}'
77+
echo "$line" | awk -F':' '{print $7}'
7078
;;
7179
esac
7280
}
@@ -129,7 +137,7 @@ I=1
129137
while [ $I -le $LINES_COUNT ]; do
130138
cat <<EOF
131139
<password_object version="1" id="oval:1:obj:${I}" xmlns="http://oval.mitre.org/XMLSchema/oval-definitions-5#unix">
132-
<username>`getField "username" ${I}`</username>
140+
<username>$(getField "username" "${I}")</username>
133141
</password_object>
134142
EOF
135143
I=$[$I+1]
@@ -145,13 +153,13 @@ I=1
145153
while [ $I -le $LINES_COUNT ]; do
146154
cat <<EOF
147155
<password_state version="1" id="oval:1:ste:${I}" xmlns="http://oval.mitre.org/XMLSchema/oval-definitions-5#unix">
148-
<username>`getField 'username' $I`</username>
149-
<password>`getField 'password' $I`</password>
150-
<user_id datatype="int">`getField 'user_id' $I`</user_id>
151-
<group_id datatype="int">`getField 'group_id' $I`</group_id>
152-
<gcos>`getField 'gcos' $I`</gcos>
153-
<home_dir>`getField 'home_dir' $I`</home_dir>
154-
<login_shell>`getField 'login_shell' $I`</login_shell>
156+
<username>$(getField 'username' "$I")</username>
157+
<password>$(getField 'password' "$I")</password>
158+
<user_id datatype="int">$(getField 'user_id' "$I")</user_id>
159+
<group_id datatype="int">$(getField 'group_id' "$I")</group_id>
160+
<gcos>$(getField 'gcos' "$I")</gcos>
161+
<home_dir>$(getField 'home_dir' "$I")</home_dir>
162+
<login_shell>$(getField 'login_shell' "$I")</login_shell>
155163
</password_state>
156164
EOF
157165
I=$[$I+1]

tests/probes/password/test_probes_password_offline_fallback.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ function test_probes_password_offline_fallback {
1313
*) return 255 ;;
1414
esac
1515

16-
local DF="${srcdir}/test_probes_password_offline.xml"
17-
local RF="results.xml"
18-
[ -f "$RF" ] && rm -f "$RF"
16+
local definition_file="${srcdir}/test_probes_password_offline.xml"
17+
local results_file="results.xml"
18+
[[ -f "$results_file" ]] && rm -f "$results_file"
1919

2020
tmpdir=$(mktemp -t -d "test_password_fallback.XXXXXX")
2121
mkdir -p "$tmpdir/etc"
@@ -27,12 +27,12 @@ root:x:0:0:root:/root:/bin/bash
2727
EOF
2828

2929
set_offline_chroot_dir "$tmpdir"
30-
$OSCAP oval eval --results "$RF" "$DF"
30+
$OSCAP oval eval --results "$results_file" "$definition_file"
3131
set_offline_chroot_dir ""
3232
rm -rf "$tmpdir"
3333

34-
if [ -f "$RF" ]; then
35-
result="$RF"
34+
if [[ -f "$results_file" ]]; then
35+
result="$results_file"
3636
assert_exists 1 'oval_results/results/system/tests/test[@test_id="oval:1:tst:1"][@result="true"]'
3737
else
3838
return 1

tests/probes/runlevel/test_probes_runlevel_unsupported.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function test_probes_runlevel_unsupported {
1414

1515
local definition="${top_srcdir}/tests/oval_details/runlevel.oval.xml"
1616
local results="results_unsupported.xml"
17-
[ -f "$results" ] && rm -f "$results"
17+
[[ -f "$results" ]] && rm -f "$results"
1818

1919
$OSCAP oval eval --results "$results" "$definition"
2020

tests/probes/shadow/test_probes_shadow_offline_unsupported.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function test_probes_shadow_offline_unsupported {
1414

1515
local definition="${srcdir}/test_probes_shadow_offline.xml"
1616
local results="results_unsupported.xml"
17-
[ -f "$results" ] && rm -f "$results"
17+
[[ -f "$results" ]] && rm -f "$results"
1818

1919
tmpdir=$(make_temp_dir /tmp "test_offline_mode_shadow_unsupported")
2020
mkdir -p "${tmpdir}/etc"

0 commit comments

Comments
 (0)