aurora-lint 0.5.2

aurora-lint - a fast CERT C static analyzer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
//! Pre-parse pass: blank a `#if`/`#ifdef`/`#ifndef` + matching `#endif` pair
//! that opens *inside* an unclosed parenthesized expression (task 1044).
//!
//! Real shape (pure-ftpd `ls.c`, `listfile`):
//! ```c
//! if (
//! # ifndef ALWAYS_SHOW_RESOLVED_SYMLINKS
//!     broken_client_compat != 0 &&
//! # endif
//!     S_ISLNK(st.st_mode)) {
//! ```
//!
//! and, equivalently, a build-time-optional operand of a condition
//! (sqlite `alter.c`, `os_unix.c`, curl `url.c`) or a build-time-optional
//! entry in a parameter or argument list.
//!
//! `tree-sitter-c` has no production for a preprocessor conditional inside
//! an expression -- `preproc_if` is a *block item*, so it can sit between
//! statements but never between two operands -- and there is no error
//! recovery that isolates it cleanly. Every occurrence of this shape is a
//! parse error, confirmed across the condition, parameter-list and
//! argument-list spellings.
//!
//! What makes it worth a pass of its own is how *unstably* the parse fails.
//! GLR recovery picks between competing repairs by cost, and the cost
//! depends on the tokens that follow, so an edit far away flips the outcome:
//! on pure-ftpd's `ls.c`, truncated after `listfile` and padded with a
//! filler function, sixteen trivial statements of padding leave `listfile`
//! parsed as a normal `function_definition` and seventeen collapse
//! everything from its opening line to end-of-file into a single `ERROR`
//! node -- a one-statement, 16-byte difference. Inside that node the
//! function's *definition* is invisible to every rule that walks the tree
//! (DCL31-C then reports each later call to `listfile` as undeclared) while
//! its calls are still found, which is the worst of both. The threshold is a
//! parser-internal artifact, so the same file is fine or broken depending on
//! edits with nothing to do with it.
//!
//! Fix, mirroring `preproc_dangling_else` and `label_preproc_guard`: blank
//! the opening directive line and its matching `#endif` (same-length
//! whitespace, newlines preserved), so the guarded fragment rejoins the
//! expression it belongs to. The cost is the same one those passes accept --
//! aurora-lint's "maybe compiled" `#ifdef` branch modeling
//! (`process_preproc_conditional` in `analyze::cfg`) is lost for this
//! fragment. Here that cost is close to zero: the fragment currently sits
//! inside an `ERROR` node, so no rule was reading a `preproc_ifdef` ancestor
//! off it in the first place.
//!
//! Deliberately conservative, and all three conditions matter because the
//! "inside an unclosed paren" test is textual:
//!
//! * The block must have no `#else`/`#elif` at its own depth -- keeping one
//!   branch's text would silently pick a side of a two-sided expression
//!   (the same call the passes above make).
//! * The guarded lines must contain no `;`, `{` or `}`. An expression
//!   fragment has none; a statement does. This is what keeps a
//!   *statement*-level guard from being blanked if the paren scan ever
//!   misjudges.
//! * The guarded lines' own parentheses must balance, so blanking the
//!   directives cannot leave the expression unbalanced.

/// How far back the paren scan will look for the start of the statement
/// containing a directive. Bounded so a pathological file cannot make this
/// quadratic, and because a parenthesized expression spanning more lines
/// than this is not a shape worth guessing about.
const MAX_STATEMENT_LOOKBACK: usize = 64;

/// The directive keyword on `line`, if it is one -- `"ifndef"` for both
/// `#ifndef X` and `# ifndef X`. The space after `#` is not cosmetic here:
/// pure-ftpd indents nested conditionals exactly that way (`# ifndef ...`
/// inside an `#if`), which is the very file this pass exists for.
fn directive_keyword(line: &str) -> Option<&str> {
    let rest = line.trim_start().strip_prefix('#')?;
    Some(
        rest.trim_start()
            .split(|c: char| !c.is_alphanumeric())
            .next()
            .unwrap_or(""),
    )
}

fn is_directive_start(line: &str) -> bool {
    matches!(directive_keyword(line), Some("if" | "ifdef" | "ifndef"))
}

fn is_endif(line: &str) -> bool {
    directive_keyword(line) == Some("endif")
}

fn is_branch_directive(line: &str) -> bool {
    matches!(directive_keyword(line), Some("else" | "elif"))
}

fn is_directive(line: &str) -> bool {
    line.trim_start().starts_with('#')
}

/// Per-line view of a file with comments and literals removed, so a `(`,
/// `;` or `}` counted below is always real code.
struct CodeLines<'a> {
    raw: Vec<&'a str>,
    /// `raw[i]` with comments, string and character literals replaced by
    /// spaces. Preprocessor directive lines (including `\`-continuations)
    /// are blank here: their parentheses are the directive's own.
    code: Vec<String>,
}

impl<'a> CodeLines<'a> {
    fn new(source: &'a str) -> Self {
        let raw: Vec<&str> = source.lines().collect();
        let mut code = Vec::with_capacity(raw.len());
        let mut in_block_comment = false;
        let mut in_directive = false;
        for line in &raw {
            let (stripped, still_open) = strip_comments_and_literals(line, in_block_comment);
            in_block_comment = still_open;
            let is_directive_line = in_directive || is_directive(line);
            // A directive continues onto the next physical line via a
            // trailing `\`, and that continuation is still the directive's
            // text, not code.
            in_directive = is_directive_line && line.trim_end().ends_with('\\');
            code.push(if is_directive_line {
                " ".repeat(stripped.len())
            } else {
                stripped
            });
        }
        Self { raw, code }
    }

    fn len(&self) -> usize {
        self.raw.len()
    }

    fn paren_balance(&self, i: usize) -> i32 {
        let line = &self.code[i];
        line.matches('(').count() as i32 - line.matches(')').count() as i32
    }

    /// True if line `i` closes a statement, i.e. its code ends with `;`,
    /// `{` or `}`. Used only as a backward-scan stopping point.
    fn ends_statement(&self, i: usize) -> bool {
        matches!(
            self.code[i].trim_end().chars().next_back(),
            Some(';' | '{' | '}')
        )
    }
}

/// Replace every comment, string literal and character literal in `line`
/// with spaces, preserving length. Returns the stripped line and whether a
/// block comment is still open at end of line.
fn strip_comments_and_literals(line: &str, mut in_block_comment: bool) -> (String, bool) {
    let bytes = line.as_bytes();
    let mut out = vec![b' '; bytes.len()];
    let mut i = 0usize;
    while i < bytes.len() {
        if in_block_comment {
            if bytes[i] == b'*' && bytes.get(i + 1) == Some(&b'/') {
                in_block_comment = false;
                i += 2;
            } else {
                i += 1;
            }
            continue;
        }
        match bytes[i] {
            b'/' if bytes.get(i + 1) == Some(&b'*') => {
                in_block_comment = true;
                i += 2;
            }
            b'/' if bytes.get(i + 1) == Some(&b'/') => break,
            quote @ (b'"' | b'\'') => {
                i += 1;
                while i < bytes.len() {
                    if bytes[i] == b'\\' {
                        i += 2;
                        continue;
                    }
                    if bytes[i] == quote {
                        i += 1;
                        break;
                    }
                    i += 1;
                }
            }
            c => {
                out[i] = c;
                i += 1;
            }
        }
    }
    // Safe: every retained byte came from `line` at the same index and is
    // ASCII (a multi-byte sequence only ever appears inside a comment or a
    // string literal, both of which are blanked wholesale here), and every
    // other byte is an ASCII space.
    (
        String::from_utf8(out).unwrap_or_else(|_| " ".repeat(bytes.len())),
        in_block_comment,
    )
}

/// True if line `i` sits inside a parenthesized expression that opened
/// earlier in the same statement.
///
/// Counted backwards from the statement's own start rather than tracked
/// forward across the file on purpose: a `#if`/`#else` pair whose two
/// branches contain different numbers of parentheses (sqlite has several)
/// makes a whole-file running count drift, and a drifted count would report
/// ordinary statement-level guards hundreds of lines later as being inside
/// an expression.
fn inside_unclosed_paren(lines: &CodeLines, i: usize) -> bool {
    let mut balance = 0i32;
    let mut scanned = 0usize;
    let mut k = i;
    while k > 0 && scanned < MAX_STATEMENT_LOOKBACK {
        k -= 1;
        scanned += 1;
        // A blank line is treated as a statement boundary: it bounds the
        // scan cheaply and no real parenthesized expression is split by
        // one. A comment-only line contributes nothing and is not a
        // boundary.
        if lines.raw[k].trim().is_empty() {
            break;
        }
        balance += lines.paren_balance(k);
        if lines.ends_statement(k) {
            break;
        }
    }
    balance > 0
}

/// Blank the `#if`/`#ifdef`/`#ifndef` + matching `#endif` directive lines of
/// every conditional that opens inside an unclosed parenthesized
/// expression, per the module docs above. Length-preserving.
pub fn blank_paren_guarded_preproc(source: &str) -> String {
    let lines = CodeLines::new(source);
    let mut line_starts = Vec::with_capacity(lines.len());
    let mut offset = 0usize;
    for line in &lines.raw {
        line_starts.push(offset);
        offset += line.len() + 1; // '\n' (a CRLF's '\r' is left as-is below)
    }

    let mut out = source.as_bytes().to_vec();

    for i in 0..lines.len() {
        if !is_directive_start(lines.raw[i]) || !inside_unclosed_paren(&lines, i) {
            continue;
        }

        let mut depth = 1i32;
        let mut end_idx = None;
        let mut has_branch = false;
        for (j, raw) in lines.raw.iter().enumerate().skip(i + 1) {
            if is_directive_start(raw) {
                depth += 1;
            } else if is_endif(raw) {
                depth -= 1;
                if depth == 0 {
                    end_idx = Some(j);
                    break;
                }
            } else if depth == 1 && is_branch_directive(raw) {
                has_branch = true;
            }
        }

        let Some(end_idx) = end_idx else {
            continue; // No matching #endif -- move on rather than halt.
        };
        if has_branch {
            continue;
        }

        let body = i + 1..end_idx;
        let is_expression_fragment = body
            .clone()
            .all(|k| !lines.code[k].contains([';', '{', '}']));
        let balanced = body.clone().map(|k| lines.paren_balance(k)).sum::<i32>() == 0;
        if !is_expression_fragment || !balanced {
            continue;
        }

        blank_line(&mut out, line_starts[i], lines.raw[i].len());
        blank_line(&mut out, line_starts[end_idx], lines.raw[end_idx].len());
    }

    String::from_utf8(out).unwrap_or_else(|_| source.to_string())
}

/// Replace every non-newline byte of the line at `line_start` with a space.
fn blank_line(out: &mut [u8], line_start: usize, line_len: usize) {
    for b in out.iter_mut().skip(line_start).take(line_len) {
        if *b != b'\n' && *b != b'\r' {
            *b = b' ';
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::parser::c_language;

    fn fixed(src: &str) -> String {
        let out = blank_paren_guarded_preproc(src);
        assert_eq!(src.len(), out.len(), "byte length changed");
        assert_eq!(
            src.matches('\n').count(),
            out.matches('\n').count(),
            "line count changed"
        );
        out
    }

    fn parses_clean(src: &str) -> bool {
        let mut parser = tree_sitter::Parser::new();
        parser.set_language(&c_language()).unwrap();
        let tree = parser.parse(fixed(src), None).unwrap();
        !tree.root_node().has_error()
    }

    #[test]
    fn fixes_ifdef_splitting_an_if_condition() {
        let src = "\
int f(int a, int b) {
    if (a
#ifdef X
        && b
#endif
        ) {
        return 1;
    }
    return 0;
}
";
        assert!(parses_clean(src));
        assert!(!fixed(src).contains("#ifdef X"));
    }

    /// pure-ftpd indents a nested conditional as `# ifndef X`, which is the
    /// spelling this pass was written for.
    #[test]
    fn fixes_a_space_between_hash_and_keyword() {
        let src = "\
int f(int a) {
    if (
# ifndef ALWAYS_SHOW_RESOLVED_SYMLINKS
        compat != 0 &&
# endif
        g(a)) {
        return 1;
    }
    return 0;
}
";
        assert!(parses_clean(src));
    }

    #[test]
    fn fixes_ifdef_in_a_parameter_list() {
        let src = "\
static void g(
#ifdef X
    int a,
#endif
    int b)
{
}
";
        assert!(parses_clean(src));
    }

    #[test]
    fn fixes_ifdef_in_an_argument_list() {
        let src = "\
void h(void) {
    foo(1,
#ifdef X
        2,
#endif
        3);
}
";
        assert!(parses_clean(src));
    }

    /// The common, already-parseable case: a guard around whole statements.
    /// tree-sitter handles it, and blanking it would throw away the
    /// `preproc_ifdef` node rules correlate against.
    #[test]
    fn leaves_a_statement_level_guard_alone() {
        let src = "\
void f(void) {
    int x = 0;
#ifdef X
    x = 1;
#endif
    (void) x;
}
";
        assert_eq!(blank_paren_guarded_preproc(src), src);
    }

    /// Keeping one branch of a two-sided expression would silently pick a
    /// side; leave it for the parser to fail on as before.
    #[test]
    fn leaves_a_two_branch_block_alone() {
        let src = "\
void f(void) {
    foo(a &&
#ifdef DEBUGBUILD
        getenv(\"X\")
#else
        0
#endif
        );
}
";
        assert_eq!(blank_paren_guarded_preproc(src), src);
    }

    /// Blanking the directives around a guarded fragment whose own
    /// parentheses do not balance would leave the expression unbalanced.
    #[test]
    fn leaves_an_unbalanced_body_alone() {
        let src = "\
void f(void) {
    foo(a,
#ifdef X
        bar(b,
#endif
        c));
}
";
        assert_eq!(blank_paren_guarded_preproc(src), src);
    }

    /// The reason the paren scan runs backwards from the statement rather
    /// than forwards across the file: the `#if`/`#else` pair below leaves a
    /// whole-file running count one `(` ahead forever, which would report
    /// the ordinary statement-level guard after it as being inside an
    /// expression.
    #[test]
    fn a_branch_with_unbalanced_parens_does_not_poison_later_guards() {
        let src = "\
void f(int a) {
#ifdef X
    g((a);
#else
    g(a));
#endif
    int x = 0;
#ifdef Y
    x = 1;
#endif
    (void) x;
}
";
        assert_eq!(blank_paren_guarded_preproc(src), src);
    }

    #[test]
    fn a_guard_inside_a_comment_or_string_is_not_a_directive() {
        let src = "\
void f(void) {
    const char *s = \"#ifdef X\";
    /* #ifdef Y */
    foo(s);
}
";
        assert_eq!(blank_paren_guarded_preproc(src), src);
    }
}