Skip to content

Commit 04a67f8

Browse files
committed
fix(cypher): build the DISTINCT row key without snprintf-derived sizes
CodeQL cpp/overflowing-snprintf (alert 85) flagged rb_row_key: it advanced `used` by snprintf's return value and passed `cap - used` as the next size, which would underflow if a call ever truncated. The buffer is sized for every column first, so it never did; the key is now copied with explicit bounds instead. The key bytes are unchanged, so DISTINCT results are identical. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent c9cacdf commit 04a67f8

1 file changed

Lines changed: 13 additions & 3 deletions

File tree

‎src/cypher/cypher.c‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4016,10 +4016,20 @@ static const char *rb_row_key(const result_builder_t *rb, const char **row, char
40164016
*cap = need;
40174017
}
40184018
size_t used = 0;
4019-
(*buf)[0] = '\0';
40204019
for (int c = 0; c < rb->col_count; c++) {
4021-
used += (size_t)snprintf(*buf + used, *cap - used, "%zu:%s", strlen(row[c]), row[c]);
4022-
}
4020+
size_t len = strlen(row[c]);
4021+
char prefix[CBM_SZ_32];
4022+
int prefix_len = snprintf(prefix, sizeof(prefix), "%zu:", len);
4023+
if (prefix_len <= 0 || (size_t)prefix_len >= sizeof(prefix) ||
4024+
(size_t)prefix_len + len >= *cap - used) {
4025+
return NULL; /* cannot happen: `need` reserved this */
4026+
}
4027+
memcpy(*buf + used, prefix, (size_t)prefix_len);
4028+
used += (size_t)prefix_len;
4029+
memcpy(*buf + used, row[c], len);
4030+
used += len;
4031+
}
4032+
(*buf)[used] = '\0';
40234033
return *buf;
40244034
}
40254035

0 commit comments

Comments
 (0)