diff --git a/lib/include/srslte/phy/utils/vector.h b/lib/include/srslte/phy/utils/vector.h index 4ec95639a..fbf000a70 100644 --- a/lib/include/srslte/phy/utils/vector.h +++ b/lib/include/srslte/phy/utils/vector.h @@ -73,7 +73,7 @@ SRSLTE_API void srslte_vec_fprint_byte(FILE *stream, uint8_t *x, const uint32_t SRSLTE_API void srslte_vec_fprint_i(FILE *stream, int *x, const uint32_t len); SRSLTE_API void srslte_vec_fprint_s(FILE *stream, short *x, const uint32_t len); SRSLTE_API void srslte_vec_fprint_hex(FILE *stream, uint8_t *x, const uint32_t len); -SRSLTE_API void srslte_vec_sprint_hex(char *str, uint8_t *x, const uint32_t len); +SRSLTE_API void srslte_vec_sprint_hex(char *str, const uint32_t max_str_len, uint8_t *x, const uint32_t len); /* Saves/loads a vector to a file */ SRSLTE_API void srslte_vec_save_file(char *filename, const void *buffer, const uint32_t len); diff --git a/lib/src/phy/utils/vector.c b/lib/src/phy/utils/vector.c index b97583bb8..9ccd21559 100644 --- a/lib/src/phy/utils/vector.c +++ b/lib/src/phy/utils/vector.c @@ -200,10 +200,16 @@ void srslte_vec_fprint_hex(FILE *stream, uint8_t *x, const uint32_t len) { fprintf(stream, "];\n"); } -void srslte_vec_sprint_hex(char *str, uint8_t *x, const uint32_t len) { +void srslte_vec_sprint_hex(char *str, const uint32_t max_str_len, uint8_t *x, const uint32_t len) { uint32_t i, nbytes; uint8_t byte; nbytes = len/8; + // check that hex string fits in buffer (every byte takes 3 characters, plus brackets) + if ((3*(len/8 + ((len%8)?1:0))) + 2 >= max_str_len) { + fprintf(stderr, "Buffer too small for printing hex string (max_str_len=%d, payload_len=%d).\n", max_str_len, len); + return; + } + int n=0; n+=sprintf(&str[n], "["); for (i=0;iwrite_pdu(pdu_msg->get()->get_sdu_lcid(), pdu_msg->get()->get_sdu_ptr(), pdu_msg->get()->get_payload_size()); } else { char tmp[1024]; - srslte_vec_sprint_hex(tmp, pdu_msg->get()->get_sdu_ptr(), 32); + srslte_vec_sprint_hex(tmp, sizeof(tmp), pdu_msg->get()->get_sdu_ptr(), 32); Error("PDU size %d exceeds maximum PDU buffer size, lcid=%d, hex=[%s]\n", pdu_msg->get()->get_payload_size(), pdu_msg->get()->get_sdu_lcid(), tmp); } diff --git a/srsue/src/phy/phch_worker.cc b/srsue/src/phy/phch_worker.cc index 19106c514..8a6c5cd8b 100644 --- a/srsue/src/phy/phch_worker.cc +++ b/srsue/src/phy/phch_worker.cc @@ -577,7 +577,7 @@ bool phch_worker::decode_pdcch_dl(srsue::mac_interface_phy::mac_grant_t* grant) char hexstr[16]; hexstr[0]='\0'; if (log_h->get_level() >= srslte::LOG_LEVEL_INFO) { - srslte_vec_sprint_hex(hexstr, dci_msg.data, dci_msg.nof_bits); + srslte_vec_sprint_hex(hexstr, sizeof(hexstr), dci_msg.data, dci_msg.nof_bits); } Info("PDCCH: DL DCI %s cce_index=%2d, L=%d, n_data_bits=%d, hex=%s\n", srslte_dci_format_string(dci_msg.format), last_dl_pdcch_ncce, (1<get_level() >= srslte::LOG_LEVEL_INFO) { - srslte_vec_sprint_hex(hexstr, dci_msg.data, dci_msg.nof_bits); + srslte_vec_sprint_hex(hexstr, sizeof(hexstr), dci_msg.data, dci_msg.nof_bits); } // Change to last_location_ul Info("PDCCH: UL DCI Format0 cce_index=%d, L=%d, n_data_bits=%d, hex=%s\n",