


Q: Given the following code slice:
```
1 static s32 gf_hevc_read_sps_bs_internal(GF_BitStream *bs, HEVCState *hevc, u8 layer_id, u32 *vui_flag_pos)
2 {
3 	s32 vps_id, sps_id = -1;
4 	u32 i, nb_CTUs, depth;
5 	HEVC_SPS *sps;
6 	HEVC_VPS *vps;
7 	HEVC_ProfileTierLevel ptl;
8 	Bool multiLayerExtSpsFlag;
9 	u8 sps_ext_or_max_sub_layers_minus1, max_sub_layers_minus1;
10 
11 	if (vui_flag_pos) *vui_flag_pos = 0;
12 
13 	//nalu header already parsed
14 	vps_id = gf_bs_read_int_log(bs, 4, "vps_id");
15 	if (vps_id >= 16) {
16 		return -1;
17 	}
18 	memset(&ptl, 0, sizeof(ptl));
19 	max_sub_layers_minus1 = 0;
20 	sps_ext_or_max_sub_layers_minus1 = 0;
21 	if (layer_id == 0)
22 		max_sub_layers_minus1 = gf_bs_read_int_log(bs, 3, "max_sub_layers_minus1");
23 	else
24 		sps_ext_or_max_sub_layers_minus1 = gf_bs_read_int_log(bs, 3, "sps_ext_or_max_sub_layers_minus1");
25 	multiLayerExtSpsFlag = (layer_id != 0) && (sps_ext_or_max_sub_layers_minus1 == 7);
26 	if (!multiLayerExtSpsFlag) {
27 		gf_bs_read_int_log(bs, 1, "temporal_id_nesting_flag");
28 		hevc_profile_tier_level(bs, 1, max_sub_layers_minus1, &ptl, 0);
29 	}
30 
31 	sps_id = gf_bs_read_ue_log(bs, "sps_id");
32 	if ((sps_id < 0) || (sps_id >= 16)) {
33 		return -1;
34 	}
35 
36 	sps = &hevc->sps[sps_id];
37 	if (!sps->state) {
38 		sps->state = 1;
39 		sps->id = sps_id;
40 		sps->vps_id = vps_id;
41 	}
42 	sps->ptl = ptl;
43 	vps = &hevc->vps[vps_id];
44 	sps->max_sub_layers_minus1 = 0;
45 	sps->sps_ext_or_max_sub_layers_minus1 = 0;
46 
47 	/* default values */
48 	sps->colour_primaries = 2;
49 	sps->transfer_characteristic = 2;
50 	sps->matrix_coeffs = 2;
51 
52 	//sps_rep_format_idx = 0;
53 	if (multiLayerExtSpsFlag) {
54 		sps->update_rep_format_flag = gf_bs_read_int_log(bs, 1, "update_rep_format_flag");
55 		if (sps->update_rep_format_flag) {
56 			sps->rep_format_idx = gf_bs_read_int_log(bs, 8, "rep_format_idx");
57 		}
58 		else {
59 			sps->rep_format_idx = vps->rep_format_idx[layer_id];
60 		}
61 		sps->width = vps->rep_formats[sps->rep_format_idx].pic_width_luma_samples;
62 		sps->height = vps->rep_formats[sps->rep_format_idx].pic_height_luma_samples;
63 		sps->chroma_format_idc = vps->rep_formats[sps->rep_format_idx].chroma_format_idc;
64 		sps->bit_depth_luma = vps->rep_formats[sps->rep_format_idx].bit_depth_luma;
65 		sps->bit_depth_chroma = vps->rep_formats[sps->rep_format_idx].bit_depth_chroma;
66 		sps->separate_colour_plane_flag = vps->rep_formats[sps->rep_format_idx].separate_colour_plane_flag;
67 
68 		//TODO this is crude ...
69 		sps->ptl = vps->ext_ptl[0];
70 	}
71 	else {
72 		sps->chroma_format_idc = gf_bs_read_ue_log(bs, "chroma_format_idc");
73 		if (sps->chroma_format_idc == 3)
74 			sps->separate_colour_plane_flag = gf_bs_read_int_log(bs, 1, "separate_colour_plane_flag");
75 		sps->width = gf_bs_read_ue_log(bs, "width");
76 		sps->height = gf_bs_read_ue_log(bs, "height");
77 		if ((sps->cw_flag = gf_bs_read_int_log(bs, 1, "conformance_window_flag"))) {
78 			u32 SubWidthC, SubHeightC;
79 
80 			if (sps->chroma_format_idc == 1) {
81 				SubWidthC = SubHeightC = 2;
82 			}
83 			else if (sps->chroma_format_idc == 2) {
84 				SubWidthC = 2;
85 				SubHeightC = 1;
86 			}
87 			else {
88 				SubWidthC = SubHeightC = 1;
89 			}
90 
91 			sps->cw_left = gf_bs_read_ue_log(bs, "conformance_window_left");
92 			sps->cw_right = gf_bs_read_ue_log(bs, "conformance_window_right");
93 			sps->cw_top = gf_bs_read_ue_log(bs, "conformance_window_top");
94 			sps->cw_bottom = gf_bs_read_ue_log(bs, "conformance_window_bottom");
95 
96 			sps->width -= SubWidthC * (sps->cw_left + sps->cw_right);
97 			sps->height -= SubHeightC * (sps->cw_top + sps->cw_bottom);
98 		}
99 		sps->bit_depth_luma = 8 + gf_bs_read_ue_log(bs, "bit_depth_luma_minus8");
100 		sps->bit_depth_chroma = 8 + gf_bs_read_ue_log(bs, "bit_depth_chroma_minus8");
101 	}
102 
103 	sps->log2_max_pic_order_cnt_lsb = 4 + gf_bs_read_ue_log(bs, "log2_max_pic_order_cnt_lsb_minus4");
104 
105 	if (!multiLayerExtSpsFlag) {
106 		sps->sub_layer_ordering_info_present_flag = gf_bs_read_int_log(bs, 1, "sub_layer_ordering_info_present_flag");
107 		for (i = sps->sub_layer_ordering_info_present_flag ? 0 : sps->max_sub_layers_minus1; i <= sps->max_sub_layers_minus1; i++) {
108 			gf_bs_read_ue_log_idx(bs, "max_dec_pic_buffering", i);
109 			gf_bs_read_ue_log_idx(bs, "num_reorder_pics", i);
110 			gf_bs_read_ue_log_idx(bs, "max_latency_increase", i);
111 		}
112 	}
113 
114 	sps->log2_min_luma_coding_block_size = 3 + gf_bs_read_ue_log(bs, "log2_min_luma_coding_block_size_minus3");
115 	sps->log2_diff_max_min_luma_coding_block_size = gf_bs_read_ue_log(bs, "log2_diff_max_min_luma_coding_block_size");
116 	sps->max_CU_width = (1 << (sps->log2_min_luma_coding_block_size + sps->log2_diff_max_min_luma_coding_block_size));
117 	sps->max_CU_height = (1 << (sps->log2_min_luma_coding_block_size + sps->log2_diff_max_min_luma_coding_block_size));
118 
119 	sps->log2_min_transform_block_size = 2 + gf_bs_read_ue_log(bs, "log2_min_transform_block_size_minus2");
120 	sps->log2_max_transform_block_size = sps->log2_min_transform_block_size  + gf_bs_read_ue_log(bs, "log2_max_transform_block_size");
121 
122 	depth = 0;
123 	sps->max_transform_hierarchy_depth_inter = gf_bs_read_ue_log(bs, "max_transform_hierarchy_depth_inter");
124 	sps->max_transform_hierarchy_depth_intra = gf_bs_read_ue_log(bs, "max_transform_hierarchy_depth_intra");
125 	while ((u32)(sps->max_CU_width >> sps->log2_diff_max_min_luma_coding_block_size) > (u32)(1 << (sps->log2_min_transform_block_size + depth)))
126 	{
127 		depth++;
128 	}
129 	sps->max_CU_depth = sps->log2_diff_max_min_luma_coding_block_size + depth;
130 
131 	nb_CTUs = ((sps->width + sps->max_CU_width - 1) / sps->max_CU_width) * ((sps->height + sps->max_CU_height - 1) / sps->max_CU_height);
132 	sps->bitsSliceSegmentAddress = 0;
133 	while (nb_CTUs > (u32)(1 << sps->bitsSliceSegmentAddress)) {
134 		sps->bitsSliceSegmentAddress++;
135 	}
136 
137 	sps->scaling_list_enable_flag = gf_bs_read_int_log(bs, 1, "scaling_list_enable_flag");
138 	if (sps->scaling_list_enable_flag) {
139 		sps->infer_scaling_list_flag = 0;
140 		sps->scaling_list_ref_layer_id = 0;
141 		if (multiLayerExtSpsFlag) {
142 			sps->infer_scaling_list_flag = gf_bs_read_int_log(bs, 1, "infer_scaling_list_flag");
143 		}
144 		if (sps->infer_scaling_list_flag) {
145 			sps->scaling_list_ref_layer_id = gf_bs_read_int_log(bs, 6, "scaling_list_ref_layer_id");
146 		}
147 		else {
148 			sps->scaling_list_data_present_flag = gf_bs_read_int_log(bs, 1, "scaling_list_data_present_flag");
149 			if (sps->scaling_list_data_present_flag) {
150 				hevc_scaling_list_data(bs);
151 			}
152 		}
153 	}
154 	sps->asymmetric_motion_partitions_enabled_flag = gf_bs_read_int_log(bs, 1, "asymmetric_motion_partitions_enabled_flag");
155 	sps->sample_adaptive_offset_enabled_flag = gf_bs_read_int_log(bs, 1, "sample_adaptive_offset_enabled_flag");
156 	if ( (sps->pcm_enabled_flag = gf_bs_read_int_log(bs, 1, "pcm_enabled_flag")) ) {
157 		sps->pcm_sample_bit_depth_luma_minus1 = gf_bs_read_int_log(bs, 4, "pcm_sample_bit_depth_luma_minus1");
158 		sps->pcm_sample_bit_depth_chroma_minus1 = gf_bs_read_int_log(bs, 4, "pcm_sample_bit_depth_chroma_minus1");
159 		sps->log2_min_pcm_luma_coding_block_size_minus3 = gf_bs_read_ue_log(bs, "log2_min_pcm_luma_coding_block_size_minus3");
160 		sps->log2_diff_max_min_pcm_luma_coding_block_size = gf_bs_read_ue_log(bs, "log2_diff_max_min_pcm_luma_coding_block_size");
161 		sps->pcm_loop_filter_disable_flag = gf_bs_read_int_log(bs, 1, "pcm_loop_filter_disable_flag");
162 	}
163 	sps->num_short_term_ref_pic_sets = gf_bs_read_ue_log(bs, "num_short_term_ref_pic_sets");
164 	if (sps->num_short_term_ref_pic_sets > 64) {
165 		GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[HEVC] Invalid number of short term reference picture sets %d\n", sps->num_short_term_ref_pic_sets));
166 		return -1;
167 	}
168 
169 	for (i = 0; i < sps->num_short_term_ref_pic_sets; i++) {
170 		Bool ret = hevc_parse_short_term_ref_pic_set(bs, sps, i);
171 		/*cannot parse short_term_ref_pic_set, skip VUI parsing*/
172 		if (!ret) {
173 			GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[HEVC] Invalid short_term_ref_pic_set\n"));
174 			return -1;
175 		}
176 	}
177 	sps->long_term_ref_pics_present_flag = gf_bs_read_int_log(bs, 1, "long_term_ref_pics_present_flag");
178 	if (sps->long_term_ref_pics_present_flag) {
179 		sps->num_long_term_ref_pic_sps = gf_bs_read_ue_log(bs, "num_long_term_ref_pic_sps");
180 		for (i = 0; i < sps->num_long_term_ref_pic_sps; i++) {
181 			gf_bs_read_int_log_idx(bs, sps->log2_max_pic_order_cnt_lsb, "lt_ref_pic_poc_lsb_sps", i);
182 			gf_bs_read_int_log_idx(bs, 1, "used_by_curr_pic_lt_sps_flag", i);
183 		}
184 	}
185 	sps->temporal_mvp_enable_flag = gf_bs_read_int_log(bs, 1, "temporal_mvp_enable_flag");
186 	sps->strong_intra_smoothing_enable_flag = gf_bs_read_int_log(bs, 1, "strong_intra_smoothing_enable_flag");
187 
188 	if (vui_flag_pos)
189 		*vui_flag_pos = (u32)gf_bs_get_bit_offset(bs);
190 
191 	if ((sps->vui_parameters_present_flag = gf_bs_read_int_log(bs, 1, "vui_parameters_present_flag")) ) {
192 		sps->aspect_ratio_info_present_flag = gf_bs_read_int_log(bs, 1, "aspect_ratio_info_present_flag");
193 		if (sps->aspect_ratio_info_present_flag) {
194 			sps->sar_idc = gf_bs_read_int_log(bs, 8, "aspect_ratio_idc");
195 			if (sps->sar_idc == 255) {
196 				sps->sar_width = gf_bs_read_int_log(bs, 16, "aspect_ratio_width");
197 				sps->sar_height = gf_bs_read_int_log(bs, 16, "aspect_ratio_height");
198 			}
199 			else if (sps->sar_idc < 17) {
200 				sps->sar_width = hevc_sar[sps->sar_idc].w;
201 				sps->sar_height = hevc_sar[sps->sar_idc].h;
202 			}
203 		}
204 
205 		if ((sps->overscan_info_present = gf_bs_read_int_log(bs, 1, "overscan_info_present")))
206 			sps->overscan_appropriate = gf_bs_read_int_log(bs, 1, "overscan_appropriate");
207 
208 		sps->video_signal_type_present_flag = gf_bs_read_int_log(bs, 1, "video_signal_type_present_flag");
209 		if (sps->video_signal_type_present_flag) {
210 			sps->video_format = gf_bs_read_int_log(bs, 3, "video_format");
211 			sps->video_full_range_flag = gf_bs_read_int_log(bs, 1, "video_full_range_flag");
212 			if ((sps->colour_description_present_flag = gf_bs_read_int_log(bs, 1, "colour_description_present_flag"))) {
213 				sps->colour_primaries = gf_bs_read_int_log(bs, 8, "colour_primaries");
214 				sps->transfer_characteristic = gf_bs_read_int_log(bs, 8, "transfer_characteristic");
215 				sps->matrix_coeffs = gf_bs_read_int_log(bs, 8, "matrix_coefficients");
216 			}
217 		}
218 
219 		if ((sps->chroma_loc_info_present_flag = gf_bs_read_int_log(bs, 1, "chroma_loc_info_present_flag"))) {
220 			sps->chroma_sample_loc_type_top_field = gf_bs_read_ue_log(bs, "chroma_sample_loc_type_top_field");
221 			sps->chroma_sample_loc_type_bottom_field = gf_bs_read_ue_log(bs, "chroma_sample_loc_type_bottom_field");
222 		}
223 
224 		sps->neutra_chroma_indication_flag = gf_bs_read_int_log(bs, 1, "neutra_chroma_indication_flag");
225 		sps->field_seq_flag = gf_bs_read_int_log(bs, 1, "field_seq_flag");
226 		sps->frame_field_info_present_flag = gf_bs_read_int_log(bs, 1, "frame_field_info_present_flag");
227 
228 		if ((sps->default_display_window_flag = gf_bs_read_int_log(bs, 1, "default_display_window_flag"))) {
229 			sps->left_offset = gf_bs_read_ue_log(bs, "display_window_left_offset");
230 			sps->right_offset = gf_bs_read_ue_log(bs, "display_window_right_offset");
231 			sps->top_offset = gf_bs_read_ue_log(bs, "display_window_top_offset");
232 			sps->bottom_offset = gf_bs_read_ue_log(bs, "display_window_bottom_offset");
233 		}
234 
235 		sps->has_timing_info = gf_bs_read_int_log(bs, 1, "has_timing_info");
236 		if (sps->has_timing_info) {
237 			sps->num_units_in_tick = gf_bs_read_int_log(bs, 32, "num_units_in_tick");
238 			sps->time_scale = gf_bs_read_int_log(bs, 32, "time_scale");
239 			sps->poc_proportional_to_timing_flag = gf_bs_read_int_log(bs, 1, "poc_proportional_to_timing_flag");
240 			if (sps->poc_proportional_to_timing_flag)
241 				sps->num_ticks_poc_diff_one_minus1 = gf_bs_read_ue_log(bs, "num_ticks_poc_diff_one_minus1");
242 			if ((sps->hrd_parameters_present_flag = gf_bs_read_int_log(bs, 1, "hrd_parameters_present_flag"))) {
243 				//				GF_LOG(GF_LOG_INFO, GF_LOG_CODING, ("[HEVC] HRD param parsing not implemented\n"));
244 				return sps_id;
245 			}
246 		}
247 
248 		if (gf_bs_read_int_log(bs, 1, "bitstream_restriction_flag")) {
249 			gf_bs_read_int_log(bs, 1, "tiles_fixed_structure_flag");
250 			gf_bs_read_int_log(bs, 1, "motion_vectors_over_pic_boundaries_flag");
251 			gf_bs_read_int_log(bs, 1, "restricted_ref_pic_lists_flag");
252 			gf_bs_read_ue_log(bs, "min_spatial_segmentation_idc");
253 			gf_bs_read_ue_log(bs, "max_bytes_per_pic_denom");
254 			gf_bs_read_ue_log(bs, "max_bits_per_min_cu_denom");
255 			gf_bs_read_ue_log(bs, "log2_max_mv_length_horizontal");
256 			gf_bs_read_ue_log(bs, "log2_max_mv_length_vertical");
257 		}
258 	}
259 
260 	if (gf_bs_read_int_log(bs, 1, "sps_extension_flag")) {
261 #if 0
262 		while (gf_bs_available(bs)) {
263 			/*sps_extension_data_flag */ gf_bs_read_int(bs, 1);
264 		}
265 #endif
266 
267 	}
268 
269 	return sps_id;
270 }```
which has a CWE-125 vulnerability at line:
```
43 	vps = &hevc->vps[vps_id];
```
Please generate the patch for the vulnerability. Please follow the patch format in the exemplars.
## Reasoning for CWE-125 Patch

The original code has a potential out-of-bounds read vulnerability (CWE-125) because it only checks if `vps_id` is greater than or equal to 16, but not if it's less than 0.

Here's a breakdown of the reasoning:

**Step 1: Identify the Vulnerable Assumption**

The code assumes that `vps_id` will always be a non-negative value. It relies on the check `if (vps_id >= 16)` to catch potential out-of-bounds accesses. However, it fails to consider the possibility of `vps_id` being negative.

**Step 2: Understand the Potential Consequence**

If `vps_id` is negative, accessing `hevc->vps[vps_id]` will lead to an out-of-bounds read. This is because negative indices in C/C++ don't automatically wrap around to the end of the array; they typically access memory locations before the start of the array, leading to undefined behavior.

**Step 3: Implement the Fix**

The patch addresses the issue by adding a check for negative values of `vps_id`. The modified condition:

```c
if ((vps_id < 0) || (vps_id >= 16))
```

ensures that `vps_id` falls within the valid range of indices for the `hevc->vps` array (assuming it has a size of 16 or less, which should be verified separately). 

By checking for both negative and excessively large values of `vps_id`, the patched code prevents out-of-bounds reads and improves the robustness of the program.



Q: Given the following code slice:
```
1 s32 hevc_parse_slice_segment(GF_BitStream *bs, HEVCState *hevc, HEVCSliceInfo *si)
2 {
3 	u32 i, j;
4 	u32 num_ref_idx_l0_active = 0, num_ref_idx_l1_active = 0;
5 	HEVC_PPS *pps;
6 	HEVC_SPS *sps;
7 	s32 pps_id;
8 	Bool RapPicFlag = GF_FALSE;
9 	Bool IDRPicFlag = GF_FALSE;
10 
11 	si->first_slice_segment_in_pic_flag = gf_bs_read_int_log(bs, 1, "first_slice_segment_in_pic_flag");
12 
13 	switch (si->nal_unit_type) {
14 	case GF_HEVC_NALU_SLICE_IDR_W_DLP:
15 	case GF_HEVC_NALU_SLICE_IDR_N_LP:
16 		IDRPicFlag = GF_TRUE;
17 		RapPicFlag = GF_TRUE;
18 		break;
19 	case GF_HEVC_NALU_SLICE_BLA_W_LP:
20 	case GF_HEVC_NALU_SLICE_BLA_W_DLP:
21 	case GF_HEVC_NALU_SLICE_BLA_N_LP:
22 	case GF_HEVC_NALU_SLICE_CRA:
23 		RapPicFlag = GF_TRUE;
24 		break;
25 	}
26 
27 	if (RapPicFlag) {
28 		gf_bs_read_int_log(bs, 1, "no_output_of_prior_pics_flag");
29 	}
30 
31 	pps_id = gf_bs_read_ue_log(bs, "pps_id");
32 	if (pps_id >= 64)
33 		return -1;
34 
35 	pps = &hevc->pps[pps_id];
36 	sps = &hevc->sps[pps->sps_id];
37 	si->sps = sps;
38 	si->pps = pps;
39 
40 	if (!si->first_slice_segment_in_pic_flag && pps->dependent_slice_segments_enabled_flag) {
41 		si->dependent_slice_segment_flag = gf_bs_read_int_log(bs, 1, "dependent_slice_segment_flag");
42 	}
43 	else {
44 		si->dependent_slice_segment_flag = GF_FALSE;
45 	}
46 
47 	if (!si->first_slice_segment_in_pic_flag) {
48 		si->slice_segment_address = gf_bs_read_int_log(bs, sps->bitsSliceSegmentAddress, "slice_segment_address");
49 	}
50 	else {
51 		si->slice_segment_address = 0;
52 	}
53 
54 	if (!si->dependent_slice_segment_flag) {
55 		Bool deblocking_filter_override_flag = 0;
56 		Bool slice_temporal_mvp_enabled_flag = 0;
57 		Bool slice_sao_luma_flag = 0;
58 		Bool slice_sao_chroma_flag = 0;
59 		Bool slice_deblocking_filter_disabled_flag = 0;
60 
61 		//"slice_reserved_undetermined_flag[]"
62 		gf_bs_read_int_log(bs, pps->num_extra_slice_header_bits, "slice_reserved_undetermined_flag");
63 
64 		si->slice_type = gf_bs_read_ue_log(bs, "slice_type");
65 
66 		if (pps->output_flag_present_flag)
67 			gf_bs_read_int_log(bs, 1, "pic_output_flag");
68 
69 		if (sps->separate_colour_plane_flag == 1)
70 			gf_bs_read_int_log(bs, 2, "colour_plane_id");
71 
72 		if (IDRPicFlag) {
73 			si->poc_lsb = 0;
74 
75 			//if not asked to parse full header, abort since we know the poc
76 			if (!hevc->full_slice_header_parse) return 0;
77 
78 		}
79 		else {
80 			si->poc_lsb = gf_bs_read_int_log(bs, sps->log2_max_pic_order_cnt_lsb, "poc_lsb");
81 
82 			//if not asked to parse full header, abort once we have the poc
83 			if (!hevc->full_slice_header_parse) return 0;
84 
85 			if (gf_bs_read_int_log(bs, 1, "short_term_ref_pic_set_sps_flag") == 0) {
86 				Bool ret = hevc_parse_short_term_ref_pic_set(bs, sps, sps->num_short_term_ref_pic_sets);
87 				if (!ret)
88 					return -1;
89 			}
90 			else if (sps->num_short_term_ref_pic_sets > 1) {
91 				u32 numbits = 0;
92 
93 				while ((u32)(1 << numbits) < sps->num_short_term_ref_pic_sets)
94 					numbits++;
95 				if (numbits > 0)
96 					gf_bs_read_int_log(bs, numbits, "short_term_ref_pic_set_idx");
97 				/*else
98 					short_term_ref_pic_set_idx = 0;*/
99 			}
100 			if (sps->long_term_ref_pics_present_flag) {
101 				u8 DeltaPocMsbCycleLt[32];
102 				u32 num_long_term_sps = 0;
103 				u32 num_long_term_pics = 0;
104 
105 				memset(DeltaPocMsbCycleLt, 0, sizeof(u8) * 32);
106 				
107 				if (sps->num_long_term_ref_pic_sps > 0) {
108 					num_long_term_sps = gf_bs_read_ue_log(bs, "num_long_term_sps");
109 				}
110 				num_long_term_pics = gf_bs_read_ue_log(bs, "num_long_term_pics");
111 
112 				for (i = 0; i < num_long_term_sps + num_long_term_pics; i++) {
113 					if (i < num_long_term_sps) {
114 						if (sps->num_long_term_ref_pic_sps > 1)
115 							gf_bs_read_int_log_idx(bs, gf_get_bit_size(sps->num_long_term_ref_pic_sps), "lt_idx_sps", i);
116 					}
117 					else {
118 						gf_bs_read_int_log_idx(bs, sps->log2_max_pic_order_cnt_lsb, "PocLsbLt", i);
119 						gf_bs_read_int_log_idx(bs, 1, "UsedByCurrPicLt", i);
120 					}
121 					if (gf_bs_read_int_log_idx(bs, 1, "delta_poc_msb_present_flag", i)) {
122 						if (i == 0 || i == num_long_term_sps)
123 							DeltaPocMsbCycleLt[i] = gf_bs_read_ue_log_idx(bs, "DeltaPocMsbCycleLt", i);
124 						else
125 							DeltaPocMsbCycleLt[i] = gf_bs_read_ue_log_idx(bs, "DeltaPocMsbCycleLt", i) + DeltaPocMsbCycleLt[i - 1];
126 					}
127 				}
128 			}
129 			if (sps->temporal_mvp_enable_flag)
130 				slice_temporal_mvp_enabled_flag = gf_bs_read_int_log(bs, 1, "slice_temporal_mvp_enabled_flag");
131 		}
132 		if (sps->sample_adaptive_offset_enabled_flag) {
133 			u32 ChromaArrayType = sps->separate_colour_plane_flag ? 0 : sps->chroma_format_idc;
134 			slice_sao_luma_flag = gf_bs_read_int_log(bs, 1, "slice_sao_luma_flag");
135 			if (ChromaArrayType != 0)
136 				slice_sao_chroma_flag = gf_bs_read_int_log(bs, 1, "slice_sao_chroma_flag");
137 		}
138 
139 		if (si->slice_type == GF_HEVC_SLICE_TYPE_P || si->slice_type == GF_HEVC_SLICE_TYPE_B) {
140 			//u32 NumPocTotalCurr;
141 			num_ref_idx_l0_active = pps->num_ref_idx_l0_default_active;
142 			num_ref_idx_l1_active = 0;
143 			if (si->slice_type == GF_HEVC_SLICE_TYPE_B)
144 				num_ref_idx_l1_active = pps->num_ref_idx_l1_default_active;
145 
146 			if (gf_bs_read_int_log(bs, 1, "num_ref_idx_active_override_flag")) {
147 				num_ref_idx_l0_active = 1 + gf_bs_read_ue_log(bs, "num_ref_idx_l0_active");
148 				if (si->slice_type == GF_HEVC_SLICE_TYPE_B)
149 					num_ref_idx_l1_active = 1 + gf_bs_read_ue_log(bs, "num_ref_idx_l1_active");
150 			}
151 
152 			if (pps->lists_modification_present_flag /*TODO: && NumPicTotalCurr > 1*/) {
153 				if (!ref_pic_lists_modification(bs, si->slice_type, num_ref_idx_l0_active, num_ref_idx_l1_active)) {
154 					GF_LOG(GF_LOG_WARNING, GF_LOG_CODING, ("[hevc] ref_pic_lists_modification( ) not implemented\n"));
155 					return -1;
156 				}
157 			}
158 
159 			if (si->slice_type == GF_HEVC_SLICE_TYPE_B)
160 				gf_bs_read_int_log(bs, 1, "mvd_l1_zero_flag");
161 			if (pps->cabac_init_present_flag)
162 				gf_bs_read_int_log(bs, 1, "cabac_init_flag");
163 
164 			if (slice_temporal_mvp_enabled_flag) {
165 				// When collocated_from_l0_flag is not present, it is inferred to be equal to 1.
166 				Bool collocated_from_l0_flag = 1;
167 				if (si->slice_type == GF_HEVC_SLICE_TYPE_B)
168 					collocated_from_l0_flag = gf_bs_read_int_log(bs, 1, "collocated_from_l0_flag");
169 
170 				if ((collocated_from_l0_flag && (num_ref_idx_l0_active > 1))
171 					|| (!collocated_from_l0_flag && (num_ref_idx_l1_active > 1))
172 				) {
173 					gf_bs_read_ue_log(bs, "collocated_ref_idx");
174 				}
175 			}
176 
177 			if ((pps->weighted_pred_flag && si->slice_type == GF_HEVC_SLICE_TYPE_P)
178 				|| (pps->weighted_bipred_flag && si->slice_type == GF_HEVC_SLICE_TYPE_B)
179 				) {
180 				hevc_pred_weight_table(bs, hevc, si, pps, sps, num_ref_idx_l0_active, num_ref_idx_l1_active);
181 			}
182 			gf_bs_read_ue_log(bs, "five_minus_max_num_merge_cand");
183 		}
184 		si->slice_qp_delta_start_bits = (s32) (gf_bs_get_position(bs) - 1) * 8 + gf_bs_get_bit_position(bs);
185 		si->slice_qp_delta = gf_bs_read_se_log(bs, "slice_qp_delta");
186 
187 		if (pps->slice_chroma_qp_offsets_present_flag) {
188 			gf_bs_read_se_log(bs, "slice_cb_qp_offset");
189 			gf_bs_read_se_log(bs, "slice_cr_qp_offset");
190 		}
191 		if (pps->deblocking_filter_override_enabled_flag) {
192 			deblocking_filter_override_flag = gf_bs_read_int_log(bs, 1, "deblocking_filter_override_flag");
193 		}
194 
195 		if (deblocking_filter_override_flag) {
196 			slice_deblocking_filter_disabled_flag = gf_bs_read_int_log(bs, 1, "slice_deblocking_filter_disabled_flag");
197 			if (!slice_deblocking_filter_disabled_flag) {
198 				gf_bs_read_se_log(bs, "slice_beta_offset_div2");
199 				gf_bs_read_se_log(bs, "slice_tc_offset_div2");
200 			}
201 		}
202 		if (pps->loop_filter_across_slices_enabled_flag
203 			&& (slice_sao_luma_flag || slice_sao_chroma_flag || !slice_deblocking_filter_disabled_flag)
204 		) {
205 			gf_bs_read_int_log(bs, 1, "slice_loop_filter_across_slices_enabled_flag");
206 		}
207 	}
208 	//dependent slice segment
209 	else {
210 		//if not asked to parse full header, abort
211 		if (!hevc->full_slice_header_parse) return 0;
212 	}
213 
214 	si->entry_point_start_bits = ((u32)gf_bs_get_position(bs) - 1) * 8 + gf_bs_get_bit_position(bs);
215 
216 	if (pps->tiles_enabled_flag || pps->entropy_coding_sync_enabled_flag) {
217 		u32 num_entry_point_offsets = gf_bs_read_ue_log(bs, "num_entry_point_offsets");
218 		if (num_entry_point_offsets > 0) {
219 			u32 offset = gf_bs_read_ue_log(bs, "offset") + 1;
220 			u32 segments = offset >> 4;
221 			s32 remain = (offset & 15);
222 
223 			for (i = 0; i < num_entry_point_offsets; i++) {
224 				//u32 res = 0;
225 				for (j = 0; j < segments; j++) {
226 					//res <<= 16;
227 					/*res +=*/ gf_bs_read_int(bs, 16);
228 				}
229 				if (remain) {
230 					//res <<= remain;
231 					/* res += */ gf_bs_read_int(bs, remain);
232 				}
233 				// entry_point_offset = val + 1; // +1; // +1 to get the size
234 			}
235 		}
236 	}
237 
238 	if (pps->slice_segment_header_extension_present_flag) {
239 		u32 size_ext = gf_bs_read_ue_log(bs, "size_ext");
240 		while (size_ext) {
241 			gf_bs_read_int(bs, 8);
242 			size_ext--;
243 		}
244 	}
245 
246 	si->header_size_bits = (gf_bs_get_position(bs) - 1) * 8 + gf_bs_get_bit_position(bs); // av_parser.c modified on 16 jan. 2019 
247 
248 	if (gf_bs_read_int_log(bs, 1, "byte_align") == 0) {
249 		GF_LOG(GF_LOG_WARNING, GF_LOG_CODING, ("Error parsing slice header: byte_align not found at end of header !\n"));
250 	}
251 
252 	gf_bs_align(bs);
253 	si->payload_start_offset = (s32)gf_bs_get_position(bs);
254 	return 0;
255 }```
which has a CWE-125 vulnerability at line:
```
35 	pps = &hevc->pps[pps_id];
```
Please generate the patch for the vulnerability. Please follow the patch format in the exemplars.
## Reasoning for CWE-125 Patch (Out-of-bounds Read)

Here's a breakdown of the reasoning behind the patch:

**Step 1: Root Cause Analysis**

- **Potential Input Range:** The code reads `pps_id` using `gf_bs_read_ue_log`. This function likely reads an unsigned integer, meaning `pps_id` could potentially hold values from 0 to the maximum value of its data type. 
- **Buffer Size:** The `hevc->pps` array likely has a fixed size, implied to be 64 based on the original check.
- **Vulnerability Point:**  The line `pps = &hevc->pps[pps_id];` is vulnerable because it directly uses `pps_id` as an index without ensuring it's within the bounds of the `hevc->pps` array.

**Step 2: Identifying the Missing Check**

- The original code only checks if `pps_id` is greater than or equal to 64 (`pps_id >= 64`). This prevents out-of-bounds access when `pps_id` is too large.
- **Crucially, it misses the case where `pps_id` could be negative.**  In C/C++, accessing an array with a negative index leads to out-of-bounds read, often accessing memory before the array's start.

**Step 3: Fixing the Vulnerability**

- **Adding the Negative Index Check:** The patch directly addresses the vulnerability by adding the missing check: 
  ```c
  if ((pps_id < 0) || (pps_id >= 64)) 
  ```
- This ensures that `pps_id` is within the valid range (0 to 63 inclusive) before being used to index the `hevc->pps` array.

**Conclusion**

The patch prevents a potential out-of-bounds read (CWE-125) by adding a bounds check to ensure that `pps_id` is non-negative and less than the size of the `hevc->pps` array. This prevents accessing memory outside the allocated bounds, improving the security and stability of the code. 
