From 8997116e1e458335e62fdb44465f7c80ed53aa50 Mon Sep 17 00:00:00 2001 From: Marcus Winter Date: Thu, 18 Feb 2016 10:05:40 +0100 Subject: [PATCH] base_c: fixed uninitialized variables in failure cases --- base_c/src/duration.c | 18 ++++++++++++++++++ base_c/src/thread.c | 5 ++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/base_c/src/duration.c b/base_c/src/duration.c index 8db8c7c..4f6c4e2 100644 --- a/base_c/src/duration.c +++ b/base_c/src/duration.c @@ -49,10 +49,14 @@ int embb_duration_set_nanoseconds(embb_duration_t* duration, assert(duration != NULL); if (nanoseconds > 0) { if (embb_duration_min()->nanoseconds > nanoseconds) { + duration->seconds = 0; + duration->nanoseconds = 0; return EMBB_UNDERFLOW; } const embb_duration_t* max = embb_duration_max(); if (max->seconds * 1000000000 + max->nanoseconds < nanoseconds) { + duration->seconds = max->seconds; + duration->nanoseconds = max->nanoseconds; return EMBB_OVERFLOW; } } @@ -66,10 +70,14 @@ int embb_duration_set_microseconds(embb_duration_t* duration, assert(duration != NULL); if (microseconds > 0) { if (embb_duration_min()->nanoseconds > microseconds*1000) { + duration->seconds = 0; + duration->nanoseconds = 0; return EMBB_UNDERFLOW; } const embb_duration_t* max = embb_duration_max(); if (max->seconds * 1000000 + max->nanoseconds / 1000 < microseconds) { + duration->seconds = max->seconds; + duration->nanoseconds = max->nanoseconds; return EMBB_OVERFLOW; } } @@ -83,10 +91,14 @@ int embb_duration_set_milliseconds(embb_duration_t* duration, assert(duration != NULL); if (milliseconds > 0) { if (embb_duration_min()->nanoseconds > milliseconds*1000000) { + duration->seconds = 0; + duration->nanoseconds = 0; return EMBB_UNDERFLOW; } const embb_duration_t* max = embb_duration_max(); if (max->seconds * 1000 + max->nanoseconds / 1000000 < milliseconds) { + duration->seconds = max->seconds; + duration->nanoseconds = max->nanoseconds; return EMBB_OVERFLOW; } } @@ -100,10 +112,14 @@ int embb_duration_set_seconds(embb_duration_t* duration, assert(duration != NULL); if (seconds > 0) { if (embb_duration_min()->nanoseconds > seconds*1000000000) { + duration->seconds = 0; + duration->nanoseconds = 0; return EMBB_UNDERFLOW; } const embb_duration_t* max = embb_duration_max(); if (max->seconds + max->nanoseconds / 1000000000 < seconds) { + duration->seconds = max->seconds; + duration->nanoseconds = max->nanoseconds; return EMBB_OVERFLOW; } } @@ -117,6 +133,8 @@ int embb_duration_add(embb_duration_t* lhs, const embb_duration_t* rhs) { assert(rhs != NULL); int carry = (int)((lhs->nanoseconds + rhs->nanoseconds) / 1000000000); if (lhs->seconds + rhs->seconds + carry > EMBB_DURATION_MAX_SECONDS) { + lhs->seconds = 0; + lhs->nanoseconds = 0; return EMBB_OVERFLOW; } lhs->nanoseconds = (lhs->nanoseconds + rhs->nanoseconds) % 1000000000; diff --git a/base_c/src/thread.c b/base_c/src/thread.c index e7abb40..a3963aa 100644 --- a/base_c/src/thread.c +++ b/base_c/src/thread.c @@ -83,7 +83,10 @@ int embb_thread_create(embb_thread_t* thread, const embb_core_set_t* core_set, assert(thread != NULL); thread->embb_internal_arg = (embb_internal_thread_arg_t*) embb_alloc(sizeof(embb_internal_thread_arg_t)); - if (thread->embb_internal_arg == NULL) return EMBB_NOMEM; + if (thread->embb_internal_arg == NULL) { + thread->embb_internal_handle = NULL; + return EMBB_NOMEM; + } thread->embb_internal_arg->func = func; thread->embb_internal_arg->arg = arg; -- libgit2 0.26.0