From b69c4465439b56c898a1ee95d49c00515e9416f3 Mon Sep 17 00:00:00 2001
From: Laurent THOMAS <laurent.thomas@open-cells.com>
Date: Fri, 22 Oct 2021 14:22:51 +0200
Subject: [PATCH] hotfix SEGV and divide by 0

---
 cmake_targets/CMakeLists.txt                  |  4 +--
 openair1/PHY/NR_TRANSPORT/pucch_rx.c          | 28 +++++++++----------
 openair2/LAYER2/NR_MAC_UE/nr_ue_scheduler.c   | 19 +++++++++++--
 .../LAYER2/NR_MAC_gNB/gNB_scheduler_uci.c     |  1 +
 4 files changed, 32 insertions(+), 20 deletions(-)

diff --git a/cmake_targets/CMakeLists.txt b/cmake_targets/CMakeLists.txt
index 9592334125b..fc09419f351 100644
--- a/cmake_targets/CMakeLists.txt
+++ b/cmake_targets/CMakeLists.txt
@@ -332,11 +332,11 @@ endif ()
 add_definitions("-DASN_DISABLE_OER_SUPPORT")
 
 #########################
-set(CMAKE_EXE_LINKER_FLAGS  "${CMAKE_EXE_LINKER_FLAGS} -ggdb3 -Wl,-rpath -Wl,${CMAKE_CURRENT_BINARY_DIR}")
+set(CMAKE_EXE_LINKER_FLAGS  "${CMAKE_EXE_LINKER_FLAGS} -ggdb2 -Wl,-rpath -Wl,${CMAKE_CURRENT_BINARY_DIR}")
 #########################
 # set a flag for changes in the source code
 # these changes are related to hardcoded path to include .h files
-set(debugOpt "-ggdb3 -DMALLOC_CHECK_=3 -fno-delete-null-pointer-checks")
+set(debugOpt "-ggdb2 -DMALLOC_CHECK_=3 -fno-delete-null-pointer-checks")
 set(CMAKE_C_FLAGS_DEBUG "${debugOpt} -O0")
 set(CMAKE_C_FLAGS_RELWITHDEBINFO "${debugOpt} -O2")
 set(CMAKE_C_FLAGS_RELEASE "-O3")
diff --git a/openair1/PHY/NR_TRANSPORT/pucch_rx.c b/openair1/PHY/NR_TRANSPORT/pucch_rx.c
index 96842426f57..80bd37dac1f 100644
--- a/openair1/PHY/NR_TRANSPORT/pucch_rx.c
+++ b/openair1/PHY/NR_TRANSPORT/pucch_rx.c
@@ -238,7 +238,7 @@ void nr_decode_pucch0(PHY_VARS_gNB *gNB,
 
   // x_n contains the sequence r_u_v_alpha_delta(n)
 
-  int n,i,l;
+  int n,i;
   int prb_offset[2] = {pucch_pdu->bwp_start+pucch_pdu->prb_start, pucch_pdu->bwp_start+pucch_pdu->prb_start};
 
   nr_group_sequence_hopping(pucch_GroupHopping,pucch_pdu->hopping_id,0,slot,&u[0],&v[0]); // calculating u and v value first hop
@@ -266,14 +266,14 @@ void nr_decode_pucch0(PHY_VARS_gNB *gNB,
   int64_t xrtmag=0,xrtmag_next=0;
   uint8_t maxpos=0;
   uint8_t index=0;
-  for (l=0; l<pucch_pdu->nr_of_symbols; l++) {
+  for (int l=0; l<pucch_pdu->nr_of_symbols; l++) {
     for (int aarx=0;aarx<frame_parms->nb_antennas_rx;aarx++) {
       memset((void*)xr[aarx][l],0,24*sizeof(int16_t));
     }
   }
   int n2;
 
-  for (l=0; l<pucch_pdu->nr_of_symbols; l++) {
+  for (int l=0; l<pucch_pdu->nr_of_symbols; l++) {
     l2 = l+pucch_pdu->start_symbol_index;
     re_offset[l] = (12*prb_offset[l]) + frame_parms->first_carrier_offset;
     if (re_offset[l]>= frame_parms->ofdm_symbol_size)
@@ -300,11 +300,10 @@ void nr_decode_pucch0(PHY_VARS_gNB *gNB,
   //int32_t no_corr = 0;
   int seq_index;
   int64_t temp;
-  int64_t av_corr=0;
 
   for(i=0;i<nr_sequences;i++){
 
-    for (l=0;l<pucch_pdu->nr_of_symbols;l++) {
+    for (int l=0;l<pucch_pdu->nr_of_symbols;l++) {
       seq_index = (pucch_pdu->initial_cyclic_shift+
 		   mcs[i]+
 		   gNB->pucch0_lut.lut[cs_ind][slot][l+pucch_pdu->start_symbol_index])%12;
@@ -323,14 +322,14 @@ void nr_decode_pucch0(PHY_VARS_gNB *gNB,
       }
     }
     LOG_D(PHY,"PUCCH IDFT[%d/%d] = (%d,%d)=>%f\n",mcs[i],seq_index,corr_re[0][0],corr_im[0][0],10*log10((double)corr_re[0][0]*corr_re[0][0] + (double)corr_im[0][0]*corr_im[0][0]));
-    if (l>1) LOG_D(PHY,"PUCCH 2nd symbol IDFT[%d/%d] = (%d,%d)=>%f\n",mcs[i],seq_index,corr_re[0][1],corr_im[0][1],10*log10((double)corr_re[0][1]*corr_re[0][1] + (double)corr_im[0][1]*corr_im[0][1]));
-    if (pucch_pdu->freq_hop_flag == 0 && l==1) {// non-coherent correlation
+    if (pucch_pdu->nr_of_symbols) LOG_D(PHY,"PUCCH 2nd symbol IDFT[%d/%d] = (%d,%d)=>%f\n",mcs[i],seq_index,corr_re[0][1],corr_im[0][1],10*log10((double)corr_re[0][1]*corr_re[0][1] + (double)corr_im[0][1]*corr_im[0][1]));
+    if (pucch_pdu->freq_hop_flag == 0 && pucch_pdu->nr_of_symbols==1) {// non-coherent correlation
       temp=0;
       for (int aa=0;aa<frame_parms->nb_antennas_rx;aa++)
         temp+=(int64_t)corr_re[aa][0]*corr_re[aa][0] + (int64_t)corr_im[aa][0]*corr_im[aa][0];
     }
 
-    else if (pucch_pdu->freq_hop_flag == 0 && l==2) {
+    else if (pucch_pdu->freq_hop_flag == 0 && pucch_pdu->nr_of_symbols==2) {
       int64_t corr_re2=0;
       int64_t corr_im2=0;
       temp=0;
@@ -349,7 +348,6 @@ void nr_decode_pucch0(PHY_VARS_gNB *gNB,
     }
     else AssertFatal(1==0,"shouldn't happen\n");
 
-    av_corr+=temp;
     if (temp>xrtmag) {
       xrtmag_next = xrtmag;
       xrtmag=temp;
@@ -359,19 +357,19 @@ void nr_decode_pucch0(PHY_VARS_gNB *gNB,
       int64_t temp2=0,temp3=0;;
       for (int aa=0;aa<frame_parms->nb_antennas_rx;aa++) {
         temp2 += ((int64_t)corr_re[aa][0]*corr_re[aa][0] + (int64_t)corr_im[aa][0]*corr_im[aa][0]);
-        if (l==2) temp3 += ((int64_t)corr_re[aa][1]*corr_re[aa][1] + (int64_t)corr_im[aa][1]*corr_im[aa][1]);
+        if (pucch_pdu->nr_of_symbols==2)
+	  temp3 += ((int64_t)corr_re[aa][1]*corr_re[aa][1] + (int64_t)corr_im[aa][1]*corr_im[aa][1]);
       }
       uci_stats->current_pucch0_stat0= dB_fixed64(temp2);
-      if (l==2) uci_stats->current_pucch0_stat1= dB_fixed64(temp3);
+      if ( pucch_pdu->nr_of_symbols==2)
+	uci_stats->current_pucch0_stat1= dB_fixed64(temp3);
     }
     else if (temp>xrtmag_next)
       xrtmag_next = temp;
   }
 
-  av_corr/=nr_sequences/l;
-
-  int xrtmag_dBtimes10 = 10*(int)dB_fixed64(xrtmag/(12*l));
-  int xrtmag_next_dBtimes10 = 10*(int)dB_fixed64(xrtmag_next/(12*l));
+  int xrtmag_dBtimes10 = 10*(int)dB_fixed64(xrtmag/(12*pucch_pdu->nr_of_symbols));
+  int xrtmag_next_dBtimes10 = 10*(int)dB_fixed64(xrtmag_next/(12*pucch_pdu->nr_of_symbols));
 #ifdef DEBUG_NR_PUCCH_RX
   printf("PUCCH 0 : maxpos %d\n",maxpos);
 #endif
diff --git a/openair2/LAYER2/NR_MAC_UE/nr_ue_scheduler.c b/openair2/LAYER2/NR_MAC_UE/nr_ue_scheduler.c
index 145b24f00e2..a4008b34c40 100644
--- a/openair2/LAYER2/NR_MAC_UE/nr_ue_scheduler.c
+++ b/openair2/LAYER2/NR_MAC_UE/nr_ue_scheduler.c
@@ -164,13 +164,26 @@ fapi_nr_ul_config_request_t *get_ul_config_request(NR_UE_MAC_INST_t *mac, int sl
 void ul_layers_config(NR_UE_MAC_INST_t * mac, nfapi_nr_ue_pusch_pdu_t *pusch_config_pdu, dci_pdu_rel15_t *dci) {
 
   NR_ServingCellConfigCommon_t *scc = mac->scc;
-  NR_PUSCH_Config_t *pusch_Config = mac->ULbwp[0]->bwp_Dedicated->pusch_Config->choice.setup;
+  NR_BWP_UplinkDedicated_t *ubwpd=NULL;
+
+  if (mac->cg &&
+      mac->cg->spCellConfig &&
+      mac->cg->spCellConfig->spCellConfigDedicated &&
+      mac->cg->spCellConfig->spCellConfigDedicated->uplinkConfig &&
+      mac->cg->spCellConfig->spCellConfigDedicated->uplinkConfig->initialUplinkBWP)
+    ubwpd = mac->cg->spCellConfig->spCellConfigDedicated->uplinkConfig->initialUplinkBWP;
+
+  NR_PUSCH_Config_t *pusch_Config = mac->ULbwp[0] ?
+    mac->ULbwp[0]->bwp_Dedicated->pusch_Config->choice.setup :
+    (ubwpd?
+     ubwpd->pusch_Config->choice.setup:
+     NULL);
 
   long	transformPrecoder;
-  if (pusch_Config->transformPrecoder)
+  if (pusch_Config && pusch_Config->transformPrecoder)
     transformPrecoder = *pusch_Config->transformPrecoder;
   else {
-    if(scc->uplinkConfigCommon->initialUplinkBWP->rach_ConfigCommon->choice.setup->msg3_transformPrecoder)
+    if(scc && scc->uplinkConfigCommon->initialUplinkBWP->rach_ConfigCommon->choice.setup->msg3_transformPrecoder)
       transformPrecoder = NR_PUSCH_Config__transformPrecoder_enabled;
     else
       transformPrecoder = NR_PUSCH_Config__transformPrecoder_disabled;
diff --git a/openair2/LAYER2/NR_MAC_gNB/gNB_scheduler_uci.c b/openair2/LAYER2/NR_MAC_gNB/gNB_scheduler_uci.c
index f2ce8498fda..67b26a79e9a 100644
--- a/openair2/LAYER2/NR_MAC_gNB/gNB_scheduler_uci.c
+++ b/openair2/LAYER2/NR_MAC_gNB/gNB_scheduler_uci.c
@@ -294,6 +294,7 @@ uint8_t compute_ri_bitlen(struct NR_CSI_ReportConfig *csi_reportconfig,
   }
   else
     AssertFatal(1==0,"Other configurations not yet implemented\n");
+  return -1;
 }
 
 void compute_li_bitlen(struct NR_CSI_ReportConfig *csi_reportconfig,
-- 
GitLab