Views
No views yet
microsoft/codebert-base designed to detect vulnerabilities in C source code functions.LABEL_1) or Safe (LABEL_0).transformers library pipeline.1from transformers import pipeline
2
3# Load the classifier pipeline
4classifier = pipeline("text-classification", model="jacpacd/vuln-detector-codebert-c-sard")
5
6# Example of a vulnerable C function (Memory Leak)
7vulnerable_code = """
8void CWE401_Memory_Leak__strdup_char_01_bad()
9{
10 char * data;
11 data = NULL;
12 {
13 char myString[] = "myString";
14 /* POTENTIAL FLAW: Allocate memory from the heap */
15 data = strdup(myString);
16 printLine(data);
17 }
18 /* POTENTIAL FLAW: No deallocation of memory */
19 ;
20}
21"""
22
23# Example of a safe C function
24safe_code = """
25void CWE401_Memory_Leak__strdup_char_01_goodB2G()
26{
27 char * data;
28 data = NULL;
29 {
30 char myString[] = "myString";
31 data = strdup(myString);
32 printLine(data);
33 }
34 /* FIX: Deallocate memory */
35 free(data);
36}
37"""
38
39results_vuln = classifier(vulnerable_code)
40results_safe = classifier(safe_code)
41
42print(f"Vulnerable Code Prediction: {results_vuln[0]}")
43# Expected output: {'label': 'LABEL_1', 'score': 0.99...}
44
45print(f"Safe Code Prediction: {results_safe[0]}")
46# Expected output: {'label': 'LABEL_0', 'score': 0.99...}