Skip to content

Commit 0cd0189

Browse files
[fix] Added error handling for malloc errors (#1242)
* For the cases when memory is allocated only for usage in the QPL API (pointers are checked internally in that case), do not add the `NULL` pointer check; otherwise, if the check is placed, a customer may think that the pointers are not checked by QPL functions, which is not correct. * For the cases when memory is allocated not for internal usage by QPL API (mostly, in `tools/` directory), add an explicit check right after the malloc call.
1 parent 4e7349e commit 0cd0189

File tree

6 files changed

+79
-2
lines changed

6 files changed

+79
-2
lines changed

doc/source/documentation/dev_guide_docs/c_use_cases/c_use_huffman_tables.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ Example code:
183183

184184
.. code-block:: c
185185
186+
/* Pointer to the allocated buffer, is checked in qpl_huffman_table_serialize() API */
186187
uint8_t* buffer = (uint8_t*) std::malloc(serialized_size * sizeof(uint8_t*));
187188
188189
status = qpl_huffman_table_serialize(huffman_table,

tools/ref/ref_expand.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,21 @@ REF_INLINE qpl_status own_expand_le_be(qpl_job* const qpl_job_ptr) {
119119

120120
// Extracted elements from source_ptr vector
121121
uint32_t* extracted_ptr = (uint32_t*)malloc((uint64_t)number_of_elements * sizeof(uint32_t));
122+
if (extracted_ptr == NULL) { return QPL_STS_NO_MEM_ERR; }
122123

123124
// Extracted mask elements from source_mask_ptr vector
124125
uint32_t* extracted_mask_ptr = (uint32_t*)malloc((uint64_t)number_of_elements * sizeof(uint32_t));
126+
if (extracted_mask_ptr == NULL) {
127+
REF_FREE_PTR(extracted_ptr);
128+
return QPL_STS_NO_MEM_ERR;
129+
}
125130

126131
// Results of the operation
127132
uint32_t* results_ptr = (uint32_t*)malloc((uint64_t)number_of_elements * sizeof(uint32_t));
133+
if (results_ptr == NULL) {
134+
REF_FREE_PTR2(extracted_ptr, extracted_mask_ptr);
135+
return QPL_STS_NO_MEM_ERR;
136+
}
128137

129138
// Convert source vector's elements to uint32_t format
130139
qpl_status status = ref_convert_to_32u_le_be(source_ptr, 0, source_bit_width, number_of_elements, extracted_ptr,
@@ -200,12 +209,21 @@ REF_INLINE qpl_status own_expand_prle(qpl_job* const qpl_job_ptr) {
200209

201210
// Extracted elements from source_ptr vector
202211
uint32_t* extracted_ptr = (uint32_t*)malloc((uint64_t)number_of_elements * sizeof(uint32_t));
212+
if (extracted_ptr == NULL) { return QPL_STS_NO_MEM_ERR; }
203213

204214
// Extracted mask elements from source_mask_ptr vector
205215
uint32_t* extracted_mask_ptr = (uint32_t*)malloc((uint64_t)source_length * sizeof(uint32_t));
216+
if (extracted_mask_ptr == NULL) {
217+
REF_FREE_PTR(extracted_ptr);
218+
return QPL_STS_NO_MEM_ERR;
219+
}
206220

207221
// Results of the operation
208222
uint32_t* results_ptr = (uint32_t*)malloc((uint64_t)source_length * sizeof(uint32_t));
223+
if (results_ptr == NULL) {
224+
REF_FREE_PTR2(extracted_ptr, extracted_mask_ptr);
225+
return QPL_STS_NO_MEM_ERR;
226+
}
209227

210228
// Convert source vector's elements to uint32_t format
211229
status = ref_convert_to_32u_prle(source_ptr, source_end_ptr, extracted_ptr, &available_bytes);

tools/ref/ref_expand_rle.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ REF_INLINE qpl_status own_expand_rle_prle(qpl_job* const qpl_job_ptr) {
161161

162162
// Extracted elements from source_ptr vector
163163
uint32_t* extracted_ptr = (uint32_t*)malloc((uint64_t)number_of_elements * sizeof(uint32_t));
164+
if (extracted_ptr == NULL) { return QPL_STS_NO_MEM_ERR; }
164165

165166
// Convert source vector's elements to uint32_t format
166167
status = ref_convert_to_32u_prle(source_ptr, source_end_ptr, extracted_ptr, &available_bytes);
@@ -176,6 +177,10 @@ REF_INLINE qpl_status own_expand_rle_prle(qpl_job* const qpl_job_ptr) {
176177

177178
// Extracted mask elements from source_mask_ptr vector
178179
uint32_t* extracted_mask_ptr = (uint32_t*)malloc((uint64_t)number_of_elements_mask * sizeof(uint32_t));
180+
if (extracted_mask_ptr == NULL) {
181+
REF_FREE_PTR(extracted_ptr);
182+
return QPL_STS_NO_MEM_ERR;
183+
}
179184

180185
// Extract mask bits
181186
status = ref_convert_to_32u_le_be(source_mask_ptr, 0, mask_bit_width, number_of_elements_mask, extracted_mask_ptr,
@@ -191,6 +196,10 @@ REF_INLINE qpl_status own_expand_rle_prle(qpl_job* const qpl_job_ptr) {
191196

192197
// Results of the operations
193198
uint32_t* results_ptr = (uint32_t*)malloc((uint64_t)number_of_output_elements * sizeof(uint32_t));
199+
if (results_ptr == NULL) {
200+
REF_FREE_PTR2(extracted_ptr, extracted_mask_ptr);
201+
return QPL_STS_NO_MEM_ERR;
202+
}
194203

195204
// Main action
196205
status = own_expand_rle(extracted_ptr, extracted_mask_ptr, number_of_elements_mask, results_ptr,
@@ -359,9 +368,14 @@ REF_INLINE qpl_status own_expand_rle_le_be(qpl_job* const qpl_job_ptr) {
359368

360369
// Extracted elements from source_ptr vector
361370
uint32_t* extracted_ptr = (uint32_t*)malloc((uint64_t)number_of_elements * sizeof(uint32_t));
371+
if (extracted_ptr == NULL) { return QPL_STS_NO_MEM_ERR; }
362372

363373
// Extracted mask elements from source_mask_ptr vector
364374
uint32_t* extracted_mask_ptr = (uint32_t*)malloc((uint64_t)number_of_elements_mask * sizeof(uint32_t));
375+
if (extracted_mask_ptr == NULL) {
376+
REF_FREE_PTR(extracted_ptr);
377+
return QPL_STS_NO_MEM_ERR;
378+
}
365379

366380
// Convert source vector's elements to uint32_t format
367381
status = ref_convert_to_32u_le_be(source_ptr, 0, source_bit_width, number_of_elements, extracted_ptr,
@@ -377,6 +391,10 @@ REF_INLINE qpl_status own_expand_rle_le_be(qpl_job* const qpl_job_ptr) {
377391

378392
// Results of the operations
379393
uint32_t* results_ptr = (uint32_t*)malloc((uint64_t)number_of_output_elements * sizeof(uint32_t));
394+
if (results_ptr == NULL) {
395+
REF_FREE_PTR2(extracted_ptr, extracted_mask_ptr);
396+
return QPL_STS_NO_MEM_ERR;
397+
}
380398

381399
// Extract mask bits
382400
status = ref_convert_to_32u_le_be(source_mask_ptr, 0, mask_bit_width, number_of_elements_mask, extracted_mask_ptr,

tools/ref/ref_extract.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,14 @@ REF_INLINE qpl_status own_extract_le_be(qpl_job* const qpl_job_ptr) {
122122

123123
// Extracted elements from source_ptr vector
124124
uint32_t* extracted_ptr = (uint32_t*)malloc((uint64_t)number_of_elements * sizeof(uint32_t));
125+
if (extracted_ptr == NULL) { return QPL_STS_NO_MEM_ERR; }
125126

126127
// Results of the operation
127128
uint32_t* results_ptr = (uint32_t*)malloc((uint64_t)number_of_elements * sizeof(uint32_t));
129+
if (results_ptr == NULL) {
130+
REF_FREE_PTR(extracted_ptr);
131+
return QPL_STS_NO_MEM_ERR;
132+
}
128133

129134
// Convert source vector's elements to uint32_t format
130135
qpl_status status = ref_convert_to_32u_le_be(source_ptr, 0, source_bit_width, number_of_elements, extracted_ptr,
@@ -203,9 +208,14 @@ REF_INLINE qpl_status own_extract_prle(qpl_job* const qpl_job_ptr) {
203208

204209
// Extracted elements from source_ptr vector
205210
uint32_t* extracted_ptr = (uint32_t*)malloc((uint64_t)number_of_elements * sizeof(uint32_t));
211+
if (extracted_ptr == NULL) { return QPL_STS_NO_MEM_ERR; }
206212

207213
// Results of the operation
208214
uint32_t* results_ptr = (uint32_t*)malloc((uint64_t)number_of_elements * sizeof(uint32_t));
215+
if (results_ptr == NULL) {
216+
REF_FREE_PTR(extracted_ptr);
217+
return QPL_STS_NO_MEM_ERR;
218+
}
209219

210220
// Convert source vector's elements to uint32_t format
211221
status = ref_convert_to_32u_prle(source_ptr, source_end_ptr, extracted_ptr, &available_bytes);

tools/ref/ref_scan.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,13 @@ REF_INLINE qpl_status own_compare_le_be(qpl_job* const qpl_job_ptr) {
117117
REF_BAD_ARG_RET((available_bytes < REF_BIT_2_BYTE(bit_length)), QPL_STS_SRC_IS_SHORT_ERR);
118118

119119
uint32_t* extracted_ptr = (uint32_t*)malloc((uint64_t)number_of_elements * sizeof(uint32_t));
120-
uint32_t* results_ptr = (uint32_t*)malloc((uint64_t)number_of_elements * sizeof(uint32_t));
120+
if (extracted_ptr == NULL) { return QPL_STS_NO_MEM_ERR; }
121+
122+
uint32_t* results_ptr = (uint32_t*)malloc((uint64_t)number_of_elements * sizeof(uint32_t));
123+
if (results_ptr == NULL) {
124+
REF_FREE_PTR(extracted_ptr);
125+
return QPL_STS_NO_MEM_ERR;
126+
}
121127

122128
// convert source vector's elements to uint32_t format
123129
qpl_status status = ref_convert_to_32u_le_be(source_ptr, 0, source_bit_width, number_of_elements, extracted_ptr,
@@ -171,7 +177,13 @@ REF_INLINE qpl_status own_compare_prle(qpl_job* const qpl_job_ptr) {
171177
if (number_of_elements < qpl_job_ptr->num_input_elements) { return QPL_STS_SRC_IS_SHORT_ERR; }
172178

173179
uint32_t* extracted_ptr = (uint32_t*)malloc((uint64_t)number_of_elements * sizeof(uint32_t));
174-
uint32_t* results_ptr = (uint32_t*)malloc((uint64_t)number_of_elements * sizeof(uint32_t));
180+
if (extracted_ptr == NULL) { return QPL_STS_NO_MEM_ERR; }
181+
182+
uint32_t* results_ptr = (uint32_t*)malloc((uint64_t)number_of_elements * sizeof(uint32_t));
183+
if (results_ptr == NULL) {
184+
REF_FREE_PTR(extracted_ptr);
185+
return QPL_STS_NO_MEM_ERR;
186+
}
175187

176188
// convert source vector's elements to uint32_t format
177189
status = ref_convert_to_32u_prle(source_ptr, source_end_ptr, extracted_ptr, &available_bytes);

tools/ref/ref_select.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,21 @@ REF_INLINE qpl_status own_select_le_be(qpl_job* const qpl_job_ptr) {
126126

127127
// Extracted elements from source_ptr vector
128128
uint32_t* extracted_ptr = (uint32_t*)malloc((uint64_t)number_of_elements * sizeof(uint32_t));
129+
if (extracted_ptr == NULL) { return QPL_STS_NO_MEM_ERR; }
129130

130131
// Extracted mask elements from source_mask_ptr vector
131132
uint32_t* extracted_mask_ptr = (uint32_t*)malloc((uint64_t)number_of_elements * sizeof(uint32_t));
133+
if (extracted_mask_ptr == NULL) {
134+
REF_FREE_PTR(extracted_ptr);
135+
return QPL_STS_NO_MEM_ERR;
136+
}
132137

133138
// Results of the operation
134139
uint32_t* results_ptr = (uint32_t*)malloc((uint64_t)number_of_elements * sizeof(uint32_t));
140+
if (results_ptr == NULL) {
141+
REF_FREE_PTR2(extracted_ptr, extracted_mask_ptr);
142+
return QPL_STS_NO_MEM_ERR;
143+
}
135144

136145
// Convert source vector's elements to uint32_t format
137146
qpl_status status = ref_convert_to_32u_le_be(source_ptr, 0, source_bit_width, number_of_elements, extracted_ptr,
@@ -214,12 +223,21 @@ REF_INLINE qpl_status own_select_prle(qpl_job* const qpl_job_ptr) {
214223

215224
// Extracted elements from source_ptr vector
216225
uint32_t* extracted_ptr = (uint32_t*)malloc((uint64_t)number_of_elements * sizeof(uint32_t));
226+
if (extracted_ptr == NULL) { return QPL_STS_NO_MEM_ERR; }
217227

218228
// Extracted mask elements from source_mask_ptr vector
219229
uint32_t* extracted_mask_ptr = (uint32_t*)malloc((uint64_t)source_length * sizeof(uint32_t));
230+
if (extracted_mask_ptr == NULL) {
231+
REF_FREE_PTR(extracted_ptr);
232+
return QPL_STS_NO_MEM_ERR;
233+
}
220234

221235
// Results of the operation
222236
uint32_t* results_ptr = (uint32_t*)malloc((uint64_t)source_length * sizeof(uint32_t));
237+
if (results_ptr == NULL) {
238+
REF_FREE_PTR2(extracted_ptr, extracted_mask_ptr);
239+
return QPL_STS_NO_MEM_ERR;
240+
}
223241

224242
// Convert source vector's elements to uint32_t format
225243
status = ref_convert_to_32u_prle(source_ptr, source_end_ptr, extracted_ptr, &available_bytes);

0 commit comments

Comments
 (0)