Skip to content

Commit 695433c

Browse files
committed
auparse: fix single-character field trimming
The fix restores the needed vlen guards after the first trim and adds a short reviewer-facing comment explaining why. Added a regression test that feeds auparse a record containing name=: and acct=, and asserts both fields are parsed as empty strings without crashing.
1 parent 76579c8 commit 695433c

2 files changed

Lines changed: 21 additions & 3 deletions

File tree

auparse/ellist.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,15 +169,16 @@ static int parse_up_record(rnode* r)
169169
n.val[vlen-1] = 0;
170170
vlen--;
171171
}
172-
if (n.val[vlen-1] == ',') {
172+
// Single-character values can become empty after trimming.
173+
if (vlen && n.val[vlen-1] == ',') {
173174
n.val[vlen-1] = 0;
174175
vlen--;
175176
}
176-
if (n.val[vlen-1] == '\'') {
177+
if (vlen && n.val[vlen-1] == '\'') {
177178
n.val[vlen-1] = 0;
178179
vlen--;
179180
}
180-
if (n.val[vlen-1] == ')') {
181+
if (vlen && n.val[vlen-1] == ')') {
181182
if (strcmp(n.val, "(none)") &&
182183
strcmp(n.val, "(null)")) {
183184
n.val[vlen-1] = 0;

auparse/test/auparse_extra_test.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,22 @@ static void test_path_norm(void)
137137
auparse_destroy(au);
138138
}
139139

140+
141+
static void test_single_char_field_values(void)
142+
{
143+
const char buf[] =
144+
"type=LOGIN msg=audit(1143146623.787:142): name=: acct=,\n";
145+
auparse_state_t *au = auparse_init(AUSOURCE_BUFFER, buf);
146+
147+
assert(au != NULL);
148+
assert(auparse_next_event(au) > 0);
149+
assert(auparse_find_field(au, "name") != NULL);
150+
assert(strcmp(auparse_get_field_str(au), "") == 0);
151+
assert(auparse_find_field(au, "acct") != NULL);
152+
assert(strcmp(auparse_get_field_str(au), "") == 0);
153+
auparse_destroy(au);
154+
}
155+
140156
int main(void)
141157
{
142158
test_new_buffer();
@@ -145,6 +161,7 @@ int main(void)
145161
test_compare();
146162
test_timestamp_milli();
147163
test_path_norm();
164+
test_single_char_field_values();
148165
printf("extra auparse tests: all passed\n");
149166
return 0;
150167
}

0 commit comments

Comments
 (0)