Skip to content

Commit

Permalink
Add check for malloc to prevent NULL pointer dereference in docs exam…
Browse files Browse the repository at this point in the history
…ple. (#2922)


Signed-off-by: Sunny-Anand <[email protected]>
  • Loading branch information
Sunny-Anand authored Sep 5, 2024
1 parent 1946c19 commit 0931985
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion docs/doc_example/main.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <OnnxMlirRuntime.h>
#include <stdio.h>
#include <assert.h>

OMTensorList *run_main_graph(OMTensorList *);

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

// Construct float arrays filled with 1s or 2s.
float *x1Data = (float *)malloc(sizeof(float) * num_elements);
// Check if memory is allocated for generating the data.
if(!x1Data) return NULL;
for (int i = 0; i < num_elements; i++)
x1Data[i] = 1.0;
float *x2Data = (float *)malloc(sizeof(float) * num_elements);
// Check if memory is allocated for generating the data.
if(!x2Data){
free(x1Data);
return NULL;
}
for (int i = 0; i < num_elements; i++)
x2Data[i] = 2.0;

Expand All @@ -32,7 +40,10 @@ OMTensorList *create_input_list() {
int main() {
// Generate input TensorList
OMTensorList *input_list = create_input_list();

if(!input_list){
// Return 2 for failure to create inputs.
return 2;
}
// Call the compiled onnx model function.
OMTensorList *output_list = run_main_graph(input_list);
if (!output_list) {
Expand Down

0 comments on commit 0931985

Please sign in to comment.