Skip to content

Commit e1eb1f6

Browse files
author
Paolo Tranquilli
committed
Rust: address review
Also fix some minor things in `bulk_generate_mad.py`.
1 parent 6162cf5 commit e1eb1f6

File tree

4 files changed

+31
-29
lines changed

4 files changed

+31
-29
lines changed

misc/scripts/models-as-data/bulk_generate_mad.py

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,27 @@
88
import os.path
99
import subprocess
1010
import sys
11-
from typing import NotRequired, TypedDict, List, Callable, Optional
11+
from typing import Required, TypedDict, List, Callable, Optional
1212
from concurrent.futures import ThreadPoolExecutor, as_completed
1313
import time
1414
import argparse
15-
import json
16-
import requests
1715
import zipfile
1816
import tarfile
1917
import shutil
2018

19+
def missing_module(module_name: str) -> None:
20+
print(f"ERROR: {module_name} is not installed. Please install it with 'pip install {module_name}'.")
21+
sys.exit(1)
22+
2123
try:
2224
import yaml
2325
except ImportError:
24-
print(
25-
"ERROR: PyYAML is not installed. Please install it with 'pip install pyyaml'."
26-
)
27-
sys.exit(1)
26+
missing_module("pyyaml")
27+
28+
try:
29+
import requests
30+
except ImportError:
31+
missing_module("requests")
2832

2933
import generate_mad as mad
3034

@@ -37,23 +41,14 @@
3741

3842

3943
# A project to generate models for
40-
class Project(TypedDict):
41-
"""
42-
Type definition for projects (acquired via a GitHub repo) to model.
43-
44-
Attributes:
45-
name: The name of the project
46-
git_repo: URL to the git repository
47-
git_tag: Optional Git tag to check out
48-
"""
49-
50-
name: str
51-
git_repo: NotRequired[str]
52-
git_tag: NotRequired[str]
53-
with_sinks: NotRequired[bool]
54-
with_sinks: NotRequired[bool]
55-
with_summaries: NotRequired[bool]
56-
44+
Project = TypedDict("Project", {
45+
"name": Required[str],
46+
"git-repo": str,
47+
"git-tag": str,
48+
"with-sinks": bool,
49+
"with-sources": bool,
50+
"with-summaries": bool,
51+
}, total=False)
5752

5853
def should_generate_sinks(project: Project) -> bool:
5954
return project.get("with-sinks", True)
@@ -72,14 +67,14 @@ def clone_project(project: Project) -> str:
7267
Shallow clone a project into the build directory.
7368
7469
Args:
75-
project: A dictionary containing project information with 'name', 'git_repo', and optional 'git_tag' keys.
70+
project: A dictionary containing project information with 'name', 'git-repo', and optional 'git-tag' keys.
7671
7772
Returns:
7873
The path to the cloned project directory.
7974
"""
8075
name = project["name"]
81-
repo_url = project["git_repo"]
82-
git_tag = project.get("git_tag")
76+
repo_url = project["git-repo"]
77+
git_tag = project.get("git-tag")
8378

8479
# Determine target directory
8580
target_dir = os.path.join(build_dir, name)
@@ -178,7 +173,7 @@ def build_database(
178173
Args:
179174
language: The language for which to build the database (e.g., "rust").
180175
extractor_options: Additional options for the extractor.
181-
project: A dictionary containing project information with 'name' and 'git_repo' keys.
176+
project: A dictionary containing project information with 'name' and 'git-repo' keys.
182177
project_dir: Path to the CodeQL database.
183178
184179
Returns:

rust/bulk_generation_targets.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
strategy: dca
22
language: rust
33
destination: rust/ql/lib/ext/generated
4+
# targets must have name specified and corresponding to the name in the DCA suite
5+
# they can optionally specify any of
6+
# with-sinks: false
7+
# with-sources: false
8+
# with-summaries: false
9+
# if a target has a dependency in this same list, it should be listed after that dependency
410
targets:
511
- name: rust
612
- name: libc

rust/ql/test/query-tests/security/CWE-770/UncontrolledAllocationSize.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,3 +526,4 @@ nodes
526526
subpaths
527527
testFailures
528528
| main.rs:202:32:202:38 | realloc | Unexpected result: Alert=arg1 |
529+
| main.rs:202:52:202:96 | //... | Missing result: Alert[rust/uncontrolled-allocation-size] |

rust/ql/test/query-tests/security/CWE-770/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ unsafe fn test_system_alloc(v: usize) {
199199

200200
let l3 = std::alloc::Layout::array::<u8>(10).unwrap();
201201
let m3 = std::alloc::System.alloc(l3);
202-
let _ = std::alloc::System.realloc(m3, l3, v); // $ MISSING: Alert[rust/uncontrolled-allocation-size]
202+
let _ = std::alloc::System.realloc(m3, l3, v); // $ Alert[rust/uncontrolled-allocation-size]
203203

204204
let l4 = std::alloc::Layout::array::<u8>(10).unwrap();
205205
let m4 = std::ptr::NonNull::<u8>::new(std::alloc::alloc(l4)).unwrap();

0 commit comments

Comments
 (0)