Commit e36aee0b authored by Frédéric Leroy's avatar Frédéric Leroy

UE: factorize calloc() calls and exit on failure

parent dcf35b2a
......@@ -1757,6 +1757,7 @@ add_executable(lte-softmodem
${OPENAIR_TARGETS}/ARCH/COMMON/common_lib.c
${OPENAIR1_DIR}/SIMULATION/ETH_TRANSPORT/netlink_init.c
${OPENAIR3_DIR}/NAS/UE/nas_ue_task.c
${OPENAIR_DIR}/common/utils/utils.c
${GTPU_need_ITTI}
${HW_SOURCE}
${TRANSPORT_SOURCE}
......@@ -1886,6 +1887,7 @@ add_executable(oaisim
${OPENAIR2_DIR}/RRC/NAS/nas_config.c
${OPENAIR2_DIR}/RRC/NAS/rb_config.c
${OPENAIR3_DIR}/NAS/UE/nas_ue_task.c
${OPENAIR_DIR}/common/utils/utils.c
${GTPU_need_ITTI}
${OPENAIR_TARGETS}/COMMON/create_tasks.c
${HW_SOURCE}
......
......@@ -410,7 +410,6 @@ target_include_directories(emm PRIVATE
# esm LIB
################################################################################
set(esm_SRC
${OPENAIR_NAS_DIR}/UE/ESM/esm_main.c
${OPENAIR_NAS_DIR}/UE/ESM/DedicatedEpsBearerContextActivation.c
${OPENAIR_NAS_DIR}/UE/ESM/DefaultEpsBearerContextActivation.c
${OPENAIR_NAS_DIR}/UE/ESM/EpsBearerContextDeactivation.c
......@@ -482,11 +481,13 @@ set(esm_HDR
${OPENAIR_NAS_DIR}/UE/ESM/SAP/esm_recv.h
${OPENAIR_NAS_DIR}/UE/ESM/SAP/esm_sap.h
${OPENAIR_NAS_DIR}/UE/ESM/SAP/esm_send.h
${OPENAIR_DIR}/common/utils/utils.h
)
add_library(esm ${esm_SRC} ${esm_HDR})
target_include_directories(esm PRIVATE
${OPENAIR_DIR}/common/utils
${OPENAIR_NAS_DIR}/UE
${OPENAIR_NAS_DIR}/UE/API/USER
${OPENAIR_NAS_DIR}/UE/ESM
......@@ -673,6 +674,7 @@ target_include_directories(ies PRIVATE
################################################################################
include_directories(
${OPENAIR_NAS_DIR}/UE
${OPENAIR_DIR}/common/utils
${OPENAIR_DIR}/common/utils/msc
${OPENAIR3_DIR}/COMMON
${OPENAIR3_DIR}/SECU
......@@ -694,6 +696,7 @@ ADD_EXECUTABLE(at_nas_ue ${OPENAIR_NAS_DIR}/UE/UEprocess.c
${OPENAIR_NAS_DIR}/UE/nas_parser.c
${OPENAIR_NAS_DIR}/UE/nas_proc.c
${OPENAIR_NAS_DIR}/UE/nas_user.c
${OPENAIR_DIR}/common/utils/utils.c
)
target_link_libraries (at_nas_ue
......
export KERNEL_DIR=/lib/modules/$(shell uname -r)/
CC=gcc
CCC=gcc
linux := $(shell if [ `uname` = "Linux" ] ; then echo "1" ; else echo "0" ; fi)
CFLAGS += -std=gnu99
CFLAGS += -Wall -g -ggdb -Wstrict-prototypes -fno-strict-aliasing
# Need to force this option because default kernel module builder is wrong
CFLAGS += $(call cc-option,-mpreferred-stack-boundary=4)
#For performance, if some option doesn't exist in all gcc versions, use $(call cc-option,MY_OPTION)
#CFLAGS += -O2
#CFLAGS += -funroll-loops
CFLAGS += -Wno-packed-bitfield-compat
# This is the minimum CPU faetures for OAI
CFLAGS += -mmmx -msse -msse2 -mssse3 -msse4.1
# Add CPU features from local CPU
#CFLAGS += -march=native
ifeq ($(OSTYPE),Cygwin)
cygwin=1
CFLAGS += -DCYGWIN
else
cygwin=0
endif
ifeq ($(linux),1)
CFLAGS +=
LDFLAGS +=
endif
ITTI_DIR = $(COMMON_UTILS_DIR)/itti
ITTI_OBJS = $(ITTI_DIR)/intertask_interface.o
ITTI_OBJS += $(ITTI_DIR)/intertask_interface_dump.o
ITTI_OBJS += $(ITTI_DIR)/backtrace.o
ITTI_OBJS += $(ITTI_DIR)/memory_pools.o
ITTI_OBJS += $(ITTI_DIR)/signals.o
ITTI_OBJS += $(ITTI_DIR)/timer.o
HASHTABLE_DIR = $(COMMON_UTILS_DIR)/collection/hashtable
MSC_DIR = $(COMMON_UTILS_DIR)/msc
HASHTABLE_OBJS = $(HASHTABLE_DIR)/hashtable.o
HASHTABLE_OBJS += $(HASHTABLE_DIR)/obj_hashtable.o
UTILS_OBJS = $(ITTI_OBJS) $(HASHTABLE_OBJS)
UTILS_incl = \
-I$(COMMON_UTILS_DIR) \
-I$(HASHTABLE_DIR) \
-I$(ITTI_DIR) \
-I$(MSC_DIR)
print-% : ; @echo $* = $($*)
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "utils.h"
void *calloc_or_fail(size_t size) {
void *ptr = calloc(1, size);
if (ptr == NULL) {
fprintf(stderr, "[UE] Failed to calloc %zu bytes", size);
exit(EXIT_FAILURE);
}
return ptr;
}
void *malloc_or_fail(size_t size) {
void *ptr = malloc(size);
if (ptr == NULL) {
fprintf(stderr, "[UE] Failed to malloc %zu bytes", size);
exit(EXIT_FAILURE);
}
return ptr;
}
#ifndef _UTILS_H
#define _UTILS_H
#include <sys/types.h>
void *calloc_or_fail(size_t size);
void *malloc_or_fail(size_t size);
#endif
......@@ -59,6 +59,7 @@ Description Defines EMM procedures executed by the Non-Access Stratum
#include "emm_proc.h"
#include "nas_log.h"
#include "utils.h"
#include "emm_sap.h"
......@@ -106,11 +107,7 @@ static IdleMode_callback_t _emm_indication_notify;
***************************************************************************/
void IdleMode_initialize(nas_user_t *user, IdleMode_callback_t cb)
{
emm_plmn_list_t *emm_plmn_list = calloc(1, sizeof(emm_plmn_list_t));
if ( emm_plmn_list == NULL ) {
LOG_TRACE(ERROR, "EMM - Can't alloc emm_plmn_list");
exit(EXIT_FAILURE);
}
emm_plmn_list_t *emm_plmn_list = calloc_or_fail( sizeof(emm_plmn_list_t));
user->emm_plmn_list = emm_plmn_list;
/* Initialize the list of available PLMNs */
emm_plmn_list->n_plmns = 0;
......
......@@ -39,6 +39,7 @@ Description Defines the EPS Mobility Management procedure call manager,
#include "emm_main.h"
#include "nas_log.h"
#include "utils.h"
#include "emmData.h"
#include "MobileIdentity.h"
#include "emm_proc_defs.h"
......@@ -134,11 +135,7 @@ void _emm_detach_initialize(emm_detach_data_t *emm_detach) {
void emm_main_initialize(nas_user_t *user, emm_indication_callback_t cb, const char *imei)
{
LOG_FUNC_IN;
user->emm_data = calloc(1, sizeof(emm_data_t));
if ( user->emm_data == NULL ) {
LOG_TRACE(ERROR, "EMM-MAIN - Failed to get allocate emm_data");
exit(EXIT_FAILURE);
}
user->emm_data = calloc_or_fail(sizeof(emm_data_t));
/* USIM validity indicator */
user->emm_data->usim_is_valid = FALSE;
/* The IMEI read from the UE's non-volatile memory */
......@@ -397,31 +394,19 @@ void emm_main_initialize(nas_user_t *user, emm_indication_callback_t cb, const c
/*
* Initialize EMM timers
*/
user->emm_data->emm_timers = calloc(1, sizeof(emm_timers_t));
if ( user->emm_data->emm_timers == NULL ) {
LOG_TRACE(ERROR, "EMM-MAIN - Failed to alloc emm_timers");
exit(EXIT_FAILURE);
}
user->emm_data->emm_timers = calloc_or_fail(sizeof(emm_timers_t));
_emm_timers_initialize(user->emm_data->emm_timers);
/*
* Initialize Internal data used for detach procedure
*/
user->emm_data->emm_detach_data = calloc(1, sizeof(emm_detach_data_t));
if ( user->emm_data->emm_detach_data == NULL ) {
LOG_TRACE(ERROR, "EMM-MAIN - Failed to alloc emm_timers");
exit(EXIT_FAILURE);
}
user->emm_data->emm_detach_data = calloc_or_fail(sizeof(emm_detach_data_t));
_emm_detach_initialize(user->emm_data->emm_detach_data);
/*
* Initialize Internal data used for attach procedure
*/
user->emm_data->emm_attach_data = calloc(1, sizeof(emm_attach_data_t));
if ( user->emm_data->emm_attach_data == NULL ) {
LOG_TRACE(ERROR, "EMM-MAIN - Failed to alloc emm_timers");
exit(EXIT_FAILURE);
}
user->emm_data->emm_attach_data = calloc_or_fail(sizeof(emm_attach_data_t));
_emm_attach_initialize(user->emm_data->emm_attach_data);
/*
......
......@@ -44,6 +44,7 @@ Description Defines functions used to handle state of EPS bearer contexts
#include "esm_ebr.h"
#include "commonDef.h"
#include "nas_log.h"
#include "utils.h"
/****************************************************************************/
......@@ -115,12 +116,7 @@ esm_ebr_data_t *esm_ebr_initialize(void)
LOG_FUNC_IN;
int i;
esm_ebr_data_t *esm_ebr_data = calloc(1, sizeof(esm_ebr_data_t));
if ( esm_ebr_data == NULL ) {
LOG_TRACE(ERROR, "ESM-EBR - Can't malloc esm_ebr_data");
exit(EXIT_FAILURE);
}
esm_ebr_data_t *esm_ebr_data = calloc_or_fail(sizeof(esm_ebr_data_t));
esm_ebr_data->index = 0;
......
......@@ -40,6 +40,7 @@ Description Defines the EPS Session Management procedure call manager,
#include "esm_main.h"
#include "commonDef.h"
#include "nas_log.h"
#include "utils.h"
#include "emmData.h"
#include "esmData.h"
......@@ -78,11 +79,7 @@ void esm_main_initialize(nas_user_t *user, esm_indication_callback_t cb)
int i;
esm_data_t *esm_data = calloc(1, sizeof(esm_data_t));
if ( esm_data == NULL ) {
LOG_TRACE(ERROR, "ESM-MAIN - Can't malloc esm_data");
exit(EXIT_FAILURE);
}
esm_data_t *esm_data = calloc_or_fail(sizeof(esm_data_t));
user->esm_data = esm_data;
default_eps_bearer_context_data_t *default_eps_bearer_context = calloc(1, sizeof(default_eps_bearer_context_data_t));
......
......@@ -42,6 +42,7 @@ Description Defines functions used to handle ESM procedure transactions.
#include "commonDef.h"
#include "nas_log.h"
#include "utils.h"
#include <stdlib.h> // malloc, free
#include <string.h> // memcpy
......@@ -84,14 +85,9 @@ static int _esm_pt_get_available_entry(esm_pt_data_t *esm_pt_data);
esm_pt_data_t *esm_pt_initialize(void)
{
LOG_FUNC_IN;
esm_pt_data_t *esm_pt_data = calloc(1, sizeof(esm_pt_data_t));
esm_pt_data_t *esm_pt_data = calloc_or_fail(sizeof(esm_pt_data_t));
int i;
if ( esm_pt_data == NULL ) {
LOG_TRACE(ERROR, "ESM-PT - Can't alloc esm_pt_data");
exit(EXIT_FAILURE);
}
esm_pt_data->index = 0;
for (i = 0; i < ESM_PT_DATA_SIZE + 1; i++) {
......
......@@ -39,6 +39,7 @@ Description NAS procedure call manager
#include "nas_proc.h"
#include "nas_log.h"
#include "nas_user.h"
#include "utils.h"
#include "emm_main.h"
#include "emm_sap.h"
......@@ -97,17 +98,8 @@ void nas_proc_initialize(nas_user_t *user, emm_indication_callback_t emm_cb,
user->proc.rsrq = NAS_PROC_RSRQ_UNKNOWN;
user->proc.rsrp = NAS_PROC_RSRP_UNKNOWN;
user->authentication_data = calloc(1, sizeof(authentication_data_t));
if ( user->authentication_data == NULL ) {
LOG_TRACE(ERROR, "NAS-PROC - Failed to alloc authentication_data");
exit(EXIT_FAILURE);
}
user->security_data = calloc(1, sizeof(security_data_t));
if ( user->security_data == NULL ) {
LOG_TRACE(ERROR, "NAS-PROC - Failed to alloc security_data");
exit(EXIT_FAILURE);
}
user->authentication_data = calloc_or_fail(sizeof(authentication_data_t));
user->security_data = calloc_or_fail( sizeof(security_data_t));
/* Initialize the EMM procedure manager */
emm_main_initialize(user, emm_cb, imei);
......
......@@ -19,6 +19,7 @@
* contact@openairinterface.org
*/
#include "utils.h"
#if defined(ENABLE_ITTI)
# include "assertions.h"
# include "intertask_interface.h"
......@@ -81,13 +82,7 @@ void *nas_ue_task(void *args_p)
{
/* Initialize user interface (to exchange AT commands with user process) */
{
user_api_id_t *user_api_id = calloc(1, sizeof(user_api_id_t));
if (user_api_id == NULL) {
LOG_E(NAS, "[UE] Failed to alloc user_api_id_t");
exit(EXIT_FAILURE);
}
user_api_id_t *user_api_id = calloc_or_fail(sizeof(user_api_id_t));
user->user_api_id = user_api_id;
if (user_api_initialize (user_api_id, NAS_PARSER_DEFAULT_USER_HOSTNAME, NAS_PARSER_DEFAULT_USER_PORT_NUMBER, NULL,
......@@ -99,23 +94,9 @@ void *nas_ue_task(void *args_p)
itti_subscribe_event_fd (TASK_NAS_UE, user_api_get_fd(user_api_id));
}
user->user_at_commands = calloc(1, sizeof(user_at_commands_t));
if ( user->user_at_commands == NULL ) {
LOG_E(NAS, "[UE %d] Can't allocate memory for user_at_commands\n", 0);
exit(EXIT_FAILURE);
}
user->at_response = calloc(1, sizeof(at_response_t));
if ( user->at_response == NULL ) {
LOG_E(NAS, "[UE %d] Can't allocate memory for at_response\n", 0);
exit(EXIT_FAILURE);
}
user->lowerlayer_data = calloc(1, sizeof(lowerlayer_data_t));
if ( user->lowerlayer_data == NULL ) {
LOG_E(NAS, "[UE %d] Can't allocate memory for lowerlayer_data\n", 0);
exit(EXIT_FAILURE);
}
user->user_at_commands = calloc_or_fail(sizeof(user_at_commands_t));
user->at_response = calloc_or_fail(sizeof(at_response_t));
user->lowerlayer_data = calloc_or_fail(sizeof(lowerlayer_data_t));
/* Initialize NAS user */
nas_user_initialize (user, &user_api_emm_callback, &user_api_esm_callback, FIRMWARE_VERSION);
......
......@@ -47,6 +47,7 @@ Description NAS procedure functions triggered by the user
#include "user_indication.h"
#include "nas_proc.h"
#include "nas_user.h"
#include "utils.h"
#include "user_api.h"
#include <string.h> // memset, strncpy, strncmp
......@@ -158,11 +159,7 @@ void nas_user_initialize(nas_user_t *user, emm_indication_callback_t emm_cb,
{
LOG_FUNC_IN;
user->nas_user_nvdata = calloc(1, sizeof(user_nvdata_t));
if ( user->nas_user_nvdata == NULL ) {
LOG_TRACE(ERROR, "USR-MAIN - Failed to alloc nas_user_nvdata");
exit(EXIT_FAILURE);
}
user->nas_user_nvdata = calloc_or_fail(sizeof(user_nvdata_t));
/* Get UE's data pathname */
char *path = memory_get_path(USER_NVRAM_DIRNAME, USER_NVRAM_FILENAME);
......@@ -179,11 +176,7 @@ void nas_user_initialize(nas_user_t *user, emm_indication_callback_t emm_cb,
}
free(path);
user->nas_user_context = calloc(1, sizeof(nas_user_context_t));
if ( user->nas_user_context == NULL ) {
LOG_TRACE(ERROR, "USR-MAIN - Failed to alloc nas_user_context");
exit(EXIT_FAILURE);
}
user->nas_user_context = calloc_or_fail(sizeof(nas_user_context_t));
_nas_user_context_initialize(user->nas_user_context, version);
/* Initialize the internal NAS processing data */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment