Skip to main content

gobject_linter/rules/
use_g_strlcpy.rs

1use gobject_ast::model::{FileModel, FunctionDefItem};
2
3use crate::{
4    ast_context::AstContext,
5    config::Config,
6    rules::{FunctionRename, Rule, Violation},
7};
8
9const RENAMES: &[FunctionRename] = &[
10    FunctionRename {
11        from: "strcpy",
12        to: None,
13        message: "Use g_strlcpy(dst, src, sizeof(dst)) instead of strcpy — no bounds checking",
14    },
15    FunctionRename {
16        from: "strcat",
17        to: None,
18        message: "Use g_strlcat(dst, src, sizeof(dst)) instead of strcat — no bounds checking",
19    },
20    FunctionRename {
21        from: "strncat",
22        to: None,
23        message: "Use g_strlcat(dst, src, sizeof(dst)) instead of strncat — strncat's n parameter is the max to append, not the buffer size, which is error-prone",
24    },
25];
26
27pub struct UseGStrlcpy;
28
29impl Rule for UseGStrlcpy {
30    fn name(&self) -> &'static str {
31        "use_g_strlcpy"
32    }
33
34    fn description(&self) -> &'static str {
35        "Use g_strlcpy/g_strlcat instead of unsafe strcpy/strcat/strncat"
36    }
37
38    fn category(&self) -> crate::rules::Category {
39        crate::rules::Category::Correctness
40    }
41
42    fn check_func_impl(
43        &self,
44        _ast_context: &AstContext,
45        _config: &Config,
46        func: &FunctionDefItem,
47        file: &FileModel,
48        violations: &mut Vec<Violation>,
49    ) {
50        self.check_function_renames(func, file, _config, violations, RENAMES);
51    }
52}