Skip to content

Commit 0931985

Browse files
authored
Add check for malloc to prevent NULL pointer dereference in docs example. (#2922)
Signed-off-by: Sunny-Anand <[email protected]>
1 parent 1946c19 commit 0931985

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

docs/doc_example/main.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <OnnxMlirRuntime.h>
22
#include <stdio.h>
3+
#include <assert.h>
34

45
OMTensorList *run_main_graph(OMTensorList *);
56

@@ -11,9 +12,16 @@ OMTensorList *create_input_list() {
1112

1213
// Construct float arrays filled with 1s or 2s.
1314
float *x1Data = (float *)malloc(sizeof(float) * num_elements);
15+
// Check if memory is allocated for generating the data.
16+
if(!x1Data) return NULL;
1417
for (int i = 0; i < num_elements; i++)
1518
x1Data[i] = 1.0;
1619
float *x2Data = (float *)malloc(sizeof(float) * num_elements);
20+
// Check if memory is allocated for generating the data.
21+
if(!x2Data){
22+
free(x1Data);
23+
return NULL;
24+
}
1725
for (int i = 0; i < num_elements; i++)
1826
x2Data[i] = 2.0;
1927

@@ -32,7 +40,10 @@ OMTensorList *create_input_list() {
3240
int main() {
3341
// Generate input TensorList
3442
OMTensorList *input_list = create_input_list();
35-
43+
if(!input_list){
44+
// Return 2 for failure to create inputs.
45+
return 2;
46+
}
3647
// Call the compiled onnx model function.
3748
OMTensorList *output_list = run_main_graph(input_list);
3849
if (!output_list) {

0 commit comments

Comments
 (0)