aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJuergen Haug2018-06-26 09:08:41 -0400
committerJuergen Haug2018-06-27 04:16:08 -0400
commit34954ef402fb6571701cc758edd5e9b4cf5d8cae (patch)
tree7f7412d50404d61989877e52eb061a509bfd4880
parent17ef390250d8af3d9dec55068b1701007807dcbd (diff)
downloadorg.eclipse.etrice-34954ef402fb6571701cc758edd5e9b4cf5d8cae.tar.gz
org.eclipse.etrice-34954ef402fb6571701cc758edd5e9b4cf5d8cae.tar.xz
org.eclipse.etrice-34954ef402fb6571701cc758edd5e9b4cf5d8cae.zip
[runtime.c] etunit buffer overflow protection + shorter file names
-rw-r--r--runtime/org.eclipse.etrice.runtime.c/src/common/etUnit/etUnit.c27
-rw-r--r--runtime/org.eclipse.etrice.runtime.c/src/common/etUnit/etUnit.h39
2 files changed, 34 insertions, 32 deletions
diff --git a/runtime/org.eclipse.etrice.runtime.c/src/common/etUnit/etUnit.c b/runtime/org.eclipse.etrice.runtime.c/src/common/etUnit/etUnit.c
index 77e832b80..603b14c07 100644
--- a/runtime/org.eclipse.etrice.runtime.c/src/common/etUnit/etUnit.c
+++ b/runtime/org.eclipse.etrice.runtime.c/src/common/etUnit/etUnit.c
@@ -65,7 +65,6 @@ typedef etFloat32 etUnitFloat;
#endif
#endif
-
/* forward declarations of private functions */
static void expect_equal_int(etInt16 id, const char* message, etInt32 expected, etInt32 actual, const char* file, int line);
/* currently not used
@@ -93,9 +92,9 @@ void etUnit_open(const char* testResultPath, const char* testFileName) {
int i;
if (testResultPath != NULL)
- sprintf(filename, "%s/%s.etu", testResultPath, testFileName);
+ snprintf(filename, sizeof(filename), "%s/%s.etu", testResultPath, testFileName);
else
- sprintf(filename, "%s.etu", testFileName);
+ snprintf(filename, sizeof(filename),"%s.etu", testFileName);
/* init global data */
for (i = 0; i < ETUNIT_ORDER_MAX; ++i)
@@ -198,7 +197,7 @@ void etUnit_testFinished(etInt16 id) {
void expectTrue(etInt16 id, const char* message, etBool condition, const char* file, int line) {
if (condition == ET_FALSE) {
char testresult[ETUNIT_FAILURE_TEXT_LEN];
- sprintf(testresult, "%s: *** EXPECT_TRUE == FALSE", message);
+ snprintf(testresult, sizeof(testresult), "%s: *** EXPECT_TRUE == FALSE", message);
etUnit_handleExpect(id, ET_FALSE, testresult, "TRUE", "FALSE", file, line);
} else {
etUnit_handleExpect(id, ET_TRUE, "", NULL, NULL, file, line);
@@ -208,7 +207,7 @@ void expectTrue(etInt16 id, const char* message, etBool condition, const char* f
void expectFalse(etInt16 id, const char* message, etBool condition, const char* file, int line) {
if (condition == ET_TRUE) {
char testresult[ETUNIT_FAILURE_TEXT_LEN];
- sprintf(testresult, "%s: EXPECT_FALSE == TRUE", message);
+ snprintf(testresult, sizeof(testresult), "%s: EXPECT_FALSE == TRUE", message);
etUnit_handleExpect(id, ET_FALSE, testresult, "FALSE", "TRUE", file, line);
} else {
etUnit_handleExpect(id, ET_TRUE, "", NULL, NULL, file, line);
@@ -243,7 +242,7 @@ void expect_equal_void_ptr(etInt16 id, const char* message, const void* expected
if (expected != actual) {
char testresult[ETUNIT_FAILURE_TEXT_LEN];
char exp[16], act[16];
- sprintf(testresult, "%s: expected=%ld, actual=%ld", message, (etUInt32) expected, (etUInt32) actual);
+ snprintf(testresult, sizeof(testresult), "%s: expected=%ld, actual=%ld", message, (etUInt32) expected, (etUInt32) actual);
sprintf(exp, "%ld", (etUInt32) expected);
sprintf(act, "%ld", (etUInt32) actual);
etUnit_handleExpect(id, ET_FALSE, testresult, exp, act, file, line);
@@ -255,7 +254,7 @@ void expect_equal_void_ptr(etInt16 id, const char* message, const void* expected
void expectEqualStr(etInt16 id, const char* message, const char* expected, const char* actual, const char* file, int line) {
if (!(expected || actual || strcmp(expected, actual) == 0)) {
char testresult[ETUNIT_FAILURE_TEXT_LEN];
- sprintf(testresult, "%s: expected=%s, actual=%s", message, expected, actual);
+ snprintf(testresult, sizeof(testresult), "%s: expected=%s, actual=%s", message, expected, actual);
etUnit_handleExpect(id, ET_FALSE, testresult, expected, actual, file, line);
} else {
etUnit_handleExpect(id, ET_TRUE, "", NULL, NULL, file, line);
@@ -301,7 +300,7 @@ void expectOrder(etInt16 id, const char* message, etInt16 identifier, const char
if (info->list[info->currentIndex] != identifier) {
char testresult[ETUNIT_FAILURE_TEXT_LEN];
char exp[16], act[16];
- sprintf(testresult, "EXPECT_ORDER %s: index=%d, expected=%d, actual=%d", message, info->currentIndex, identifier,
+ snprintf(testresult, sizeof(testresult), "EXPECT_ORDER %s: index=%d, expected=%d, actual=%d", message, info->currentIndex, identifier,
info->list[info->currentIndex]);
sprintf(exp, "%d", identifier);
sprintf(act, "%d", info->list[info->currentIndex]);
@@ -312,7 +311,7 @@ void expectOrder(etInt16 id, const char* message, etInt16 identifier, const char
}
} else {
char testresult[ETUNIT_FAILURE_TEXT_LEN];
- sprintf(testresult, "EXPECT_ORDER: index(%d) is too big in %s", info->currentIndex, message);
+ snprintf(testresult, sizeof(testresult), "EXPECT_ORDER: index(%d) is too big in %s", info->currentIndex, message);
etUnit_handleExpect(id, ET_FALSE, testresult, NULL, NULL, file, line);
etLogger_logInfoF("EXPECT_ORDER: index too big in %s", message);
}
@@ -324,7 +323,7 @@ void expectOrderEnd(etInt16 id, const char* message, etInt16 identifier, const c
expectOrder(id, message, identifier, file, line);
if (info->currentIndex != info->size) {
char testresult[ETUNIT_FAILURE_TEXT_LEN];
- sprintf(testresult, "EXPECT_ORDER_END %s: wrong index at the end: expected=%d, actual=%d", message, info->size, info->currentIndex);
+ snprintf(testresult, sizeof(testresult), "EXPECT_ORDER_END %s: wrong index at the end: expected=%d, actual=%d", message, info->size, info->currentIndex);
etUnit_handleExpect(id, ET_FALSE, testresult, NULL, NULL, file, line);
}
}
@@ -339,7 +338,7 @@ static void expect_equal_int(etInt16 id, const char* message, etInt32 expected,
if (expected != actual) {
char testresult[ETUNIT_FAILURE_TEXT_LEN];
char exp[16], act[16];
- sprintf(testresult, "%s: expected=%ld, actual=%ld", message, expected, actual);
+ snprintf(testresult, sizeof(testresult), "%s: expected=%ld, actual=%ld", message, expected, actual);
sprintf(exp, "%ld", expected);
sprintf(act, "%ld", actual);
etUnit_handleExpect(id, ET_FALSE, testresult, exp, act, file, line);
@@ -372,7 +371,7 @@ static void expect_equal_uint(etInt16 id, const char* message, etUInt32 expected
if (expected != actual) {
char testresult[ETUNIT_FAILURE_TEXT_LEN];
char exp[16], act[16];
- sprintf(testresult, "%s: expected=%lu, actual=%lu", message, expected, actual);
+ snprintf(testresult, sizeof(testresult), "%s: expected=%lu, actual=%lu", message, expected, actual);
sprintf(exp, "%lu", expected);
sprintf(act, "%lu", actual);
etUnit_handleExpect(id, ET_FALSE, testresult, exp, act, file, line);
@@ -406,7 +405,7 @@ static void expect_equal_float(etInt16 id, const char* message, etUnitFloat expe
if (expected - actual < -precision || expected - actual > precision) {
char testresult[ETUNIT_FAILURE_TEXT_LEN];
char exp[16], act[16];
- sprintf(testresult, "%s: expected=%f, actual=%f", message, expected, actual);
+ snprintf(testresult, sizeof(testresult), "%s: expected=%f, actual=%f", message, expected, actual);
sprintf(exp, "%f", expected);
sprintf(act, "%f", actual);
etUnit_handleExpect(id, ET_FALSE, testresult, exp, act, file, line);
@@ -419,7 +418,7 @@ static void expect_range_float(etInt16 id, const char* message, etUnitFloat min,
if (actual < min || actual > max) {
char testresult[ETUNIT_FAILURE_TEXT_LEN];
char exp[64], act[16];
- sprintf(testresult, "%s: min=%f, max=%f, actual=%f", message, min, max, actual);
+ snprintf(testresult, sizeof(testresult), "%s: min=%f, max=%f, actual=%f", message, min, max, actual);
if (actual < min) {
sprintf(exp, ">=%f(min)", min);
sprintf(act, "%f", actual);
diff --git a/runtime/org.eclipse.etrice.runtime.c/src/common/etUnit/etUnit.h b/runtime/org.eclipse.etrice.runtime.c/src/common/etUnit/etUnit.h
index 004f2843f..f427e2d65 100644
--- a/runtime/org.eclipse.etrice.runtime.c/src/common/etUnit/etUnit.h
+++ b/runtime/org.eclipse.etrice.runtime.c/src/common/etUnit/etUnit.h
@@ -27,7 +27,10 @@ extern "C" {
#define _ETUNIT_H_
#include "etDatatypes.h"
+#include <string.h>
+// compile time evaluated
+#define ETUNIT_FILENAME (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__))
/**
* opens a file to protocol the test results
@@ -107,43 +110,43 @@ void etUnit_closeAll(etInt16 id);
/**
* calls \ref expectTrue()
*/
-#define EXPECT_TRUE(id, msg, condition) expectTrue(id, msg, condition, __FILE__, __LINE__)
+#define EXPECT_TRUE(id, msg, condition) expectTrue(id, msg, condition, ETUNIT_FILENAME, __LINE__)
/** calls \ref expectFalse() */
-#define EXPECT_FALSE(id, msg, condition) expectFalse(id, msg, condition, __FILE__, __LINE__)
+#define EXPECT_FALSE(id, msg, condition) expectFalse(id, msg, condition, ETUNIT_FILENAME, __LINE__)
/*
* signed integer values
*/
/** calls \ref expectEqualInt8() */
-#define EXPECT_EQUAL_INT8(id, msg, expected, actual) expectEqualInt8(id, msg, expected, actual, __FILE__, __LINE__)
+#define EXPECT_EQUAL_INT8(id, msg, expected, actual) expectEqualInt8(id, msg, expected, actual, ETUNIT_FILENAME, __LINE__)
/** calls \ref expectEqualInt16() */
-#define EXPECT_EQUAL_INT16(id, msg, expected, actual) expectEqualInt16(id, msg, expected, actual, __FILE__, __LINE__)
+#define EXPECT_EQUAL_INT16(id, msg, expected, actual) expectEqualInt16(id, msg, expected, actual, ETUNIT_FILENAME, __LINE__)
/** calls \ref expectEqualInt32() */
-#define EXPECT_EQUAL_INT32(id, msg, expected, actual) expectEqualInt32(id, msg, expected, actual, __FILE__, __LINE__)
+#define EXPECT_EQUAL_INT32(id, msg, expected, actual) expectEqualInt32(id, msg, expected, actual, ETUNIT_FILENAME, __LINE__)
/*
* unsigned integer values
*/
/** calls \ref expectEqualUInt8() */
-#define EXPECT_EQUAL_UINT8(id, msg, expected, actual) expectEqualUInt8(id, msg, expected, actual, __FILE__, __LINE__)
+#define EXPECT_EQUAL_UINT8(id, msg, expected, actual) expectEqualUInt8(id, msg, expected, actual, ETUNIT_FILENAME, __LINE__)
/** calls \ref expectEqualUInt16() */
-#define EXPECT_EQUAL_UINT16(id, msg, expected, actual) expectEqualUInt16(id, msg, expected, actual, __FILE__, __LINE__)
+#define EXPECT_EQUAL_UINT16(id, msg, expected, actual) expectEqualUInt16(id, msg, expected, actual, ETUNIT_FILENAME, __LINE__)
/** calls \ref expectEqualUInt32() */
-#define EXPECT_EQUAL_UINT32(id, msg, expected, actual) expectEqualUInt32(id, msg, expected, actual, __FILE__, __LINE__)
+#define EXPECT_EQUAL_UINT32(id, msg, expected, actual) expectEqualUInt32(id, msg, expected, actual, ETUNIT_FILENAME, __LINE__)
/*
* float values
*/
#ifdef ET_FLOAT32
/** calls \ref expectEqualFloat32() */
-#define EXPECT_EQUAL_FLOAT32(id, msg, expected, actual, precision) expectEqualFloat32(id, msg, expected, actual, precision, __FILE__, __LINE__)
-#define EXPECT_RANGE_FLOAT32(id, msg, min, max, actual) expectRangeFloat32(id, msg, min, max, actual, __FILE__, __LINE__)
+#define EXPECT_EQUAL_FLOAT32(id, msg, expected, actual, precision) expectEqualFloat32(id, msg, expected, actual, precision, ETUNIT_FILENAME, __LINE__)
+#define EXPECT_RANGE_FLOAT32(id, msg, min, max, actual) expectRangeFloat32(id, msg, min, max, actual, ETUNIT_FILENAME, __LINE__)
#endif
#ifdef ET_FLOAT64
/** calls \ref expectEqualFloat64() */
-#define EXPECT_EQUAL_FLOAT64(id, msg, expected, actual, precision) expectEqualFloat64(id, msg, expected, actual, precision, __FILE__, __LINE__)
-#define EXPECT_RANGE_FLOAT64(id, msg, min, max, actual) expectRangeFloat64(id, msg, min, max, actual, __FILE__, __LINE__)
+#define EXPECT_EQUAL_FLOAT64(id, msg, expected, actual, precision) expectEqualFloat64(id, msg, expected, actual, precision, ETUNIT_FILENAME, __LINE__)
+#define EXPECT_RANGE_FLOAT64(id, msg, min, max, actual) expectRangeFloat64(id, msg, min, max, actual, ETUNIT_FILENAME, __LINE__)
#endif
@@ -152,26 +155,26 @@ void etUnit_closeAll(etInt16 id);
*/
/** calls \ref expect_equal_void_ptr() */
#define EXPECT_EQUAL_PTR(id, msg, expected, actual) \
- expect_equal_void_ptr(id, msg, (const void*) expected, (const void*) actual, __FILE__, __LINE__)
+ expect_equal_void_ptr(id, msg, (const void*) expected, (const void*) actual, ETUNIT_FILENAME, __LINE__)
/*
* Strings
*/
-#define EXPECT_EQUAL_STR(id, msg, expected, actual) expectEqualStr(id, msg, expected, actual, __FILE__, __LINE__)
+#define EXPECT_EQUAL_STR(id, msg, expected, actual) expectEqualStr(id, msg, expected, actual, ETUNIT_FILENAME, __LINE__)
/*
* more specialized functions
*/
/** calls \ref expectOrderStart() */
#ifdef __cplusplus
-#define EXPECT_ORDER_START(id, list, size) expectOrderStart(id, list.getData(), size, __FILE__, __LINE__)
+#define EXPECT_ORDER_START(id, list, size) expectOrderStart(id, list.getData(), size, ETUNIT_FILENAME, __LINE__)
#else
-#define EXPECT_ORDER_START(id, list, size) expectOrderStart(id, list, size, __FILE__, __LINE__)
+#define EXPECT_ORDER_START(id, list, size) expectOrderStart(id, list, size, ETUNIT_FILENAME, __LINE__)
#endif
/** calls \ref expectOrder() */
-#define EXPECT_ORDER(id, msg, val) expectOrder(id, msg, val, __FILE__, __LINE__)
+#define EXPECT_ORDER(id, msg, val) expectOrder(id, msg, val, ETUNIT_FILENAME, __LINE__)
/** calls \ref expectOrderEnd() */
-#define EXPECT_ORDER_END(id, msg, val) expectOrderEnd(id, msg, val, __FILE__, __LINE__)
+#define EXPECT_ORDER_END(id, msg, val) expectOrderEnd(id, msg, val, ETUNIT_FILENAME, __LINE__)
/*