Skip to main content

luaur_analysis/methods/
lint_format_string_check_string_match_set.rs

1use crate::records::lint_format_string::LintFormatString;
2
3impl LintFormatString {
4    #[inline]
5    pub fn check_string_match_set(
6        &self,
7        data: *const core::ffi::c_char,
8        size: usize,
9        magic: &[u8],
10        classes: &[u8],
11    ) -> *const core::ffi::c_char {
12        let mut i = 0;
13        unsafe {
14            while i < size {
15                let ch = *data.add(i);
16                if ch == b'%' as i8 {
17                    i += 1;
18
19                    if i == size {
20                        return c"unfinished character class".as_ptr();
21                    }
22
23                    let next_ch = *data.add(i);
24                    if self.is_digit(next_ch) {
25                        return c"sets can not contain capture references".as_ptr();
26                    } else if self.is_alpha(next_ch) {
27                        // lower case lookup - upper case for every character class is defined as its inverse
28                        if !classes.contains(&((next_ch as u8) | b' ')) {
29                            return c"invalid character class, must refer to a defined class or its inverse".as_ptr();
30                        }
31                    } else {
32                        // technically % can escape any non-alphanumeric character but this is error-prone
33                        if !magic.contains(&(next_ch as u8)) {
34                            return c"expected a magic character after %".as_ptr();
35                        }
36                    }
37
38                    if i + 1 < size && *data.add(i + 1) == b'-' as i8 {
39                        return c"character range can't include character sets".as_ptr();
40                    }
41                } else if ch == b'-' as i8 {
42                    if i + 1 < size && *data.add(i + 1) == b'%' as i8 {
43                        return c"character range can't include character sets".as_ptr();
44                    }
45                }
46
47                i += 1;
48            }
49        }
50
51        core::ptr::null()
52    }
53}