Formatting .c/.h files and fix memory leakage issues
Signed-off-by: John Chung <john.chung@arm.com>
Change-Id: Id8328f412c2724992d80c0b3f895c8f85bc4ae68
diff --git a/tests/ir-tests.cpp b/tests/ir-tests.cpp
index affa305..542ce8e 100644
--- a/tests/ir-tests.cpp
+++ b/tests/ir-tests.cpp
@@ -4,7 +4,7 @@
* Author: Lawrence.Tang@arm.com
**/
-#include <ctype.h>
+#include <cctype>
#include "gtest/gtest.h"
#include "test-utils.hpp"
extern "C" {
@@ -31,10 +31,11 @@
//Convert to IR, free resources.
json_object *ir;
- if (single_section)
+ if (single_section) {
ir = cper_single_section_to_ir(record);
- else
+ } else {
ir = cper_to_ir(record);
+ }
fclose(record);
free(buf);
@@ -42,6 +43,7 @@
char error_message[JSON_ERROR_MSG_MAX_LEN] = { 0 };
int valid = validate_schema_from_file("./specification/cper-json.json",
ir, error_message);
+ json_object_put(ir);
ASSERT_TRUE(valid)
<< "IR validation test failed (single section mode = "
<< single_section << ") with message: " << error_message;
@@ -58,19 +60,21 @@
//Convert to IR.
json_object *ir;
- if (single_section)
+ if (single_section) {
ir = cper_single_section_to_ir(record);
- else
+ } else {
ir = cper_to_ir(record);
+ }
//Now convert back to binary, and get a stream out.
char *cper_buf;
size_t cper_buf_size;
FILE *stream = open_memstream(&cper_buf, &cper_buf_size);
- if (single_section)
+ if (single_section) {
ir_single_section_to_cper(ir, stream);
- else
+ } else {
ir_to_cper(ir, stream);
+ }
size_t cper_len = ftell(stream);
fclose(stream);
@@ -84,6 +88,7 @@
fclose(record);
free(buf);
free(cper_buf);
+ json_object_put(ir);
}
//Tests randomly generated CPER sections for IR validity of a given type, in both single section mode and full CPER log mode.
@@ -105,20 +110,23 @@
*/
TEST(CompileTimeAssertions, TwoWayConversion)
{
- for (int i = 0; i < section_definitions_len; i++) {
+ for (size_t i = 0; i < section_definitions_len; i++) {
//If a conversion one way exists, a conversion the other way must exist.
std::string err =
"If a CPER conversion exists one way, there must be an equivalent method in reverse.";
- if (section_definitions[i].ToCPER != NULL)
- ASSERT_NE(section_definitions[i].ToIR, NULL) << err;
- if (section_definitions[i].ToIR != NULL)
- ASSERT_NE(section_definitions[i].ToCPER, NULL) << err;
+ if (section_definitions[i].ToCPER != NULL) {
+ ASSERT_NE(section_definitions[i].ToIR, nullptr) << err;
+ }
+ if (section_definitions[i].ToIR != NULL) {
+ ASSERT_NE(section_definitions[i].ToCPER, nullptr)
+ << err;
+ }
}
}
TEST(CompileTimeAssertions, ShortcodeNoSpaces)
{
- for (int i = 0; i < generator_definitions_len; i++) {
+ for (size_t i = 0; i < generator_definitions_len; i++) {
for (int j = 0;
generator_definitions[i].ShortName[j + 1] != '\0'; j++) {
ASSERT_FALSE(
@@ -302,4 +310,4 @@
{
testing::InitGoogleTest();
return RUN_ALL_TESTS();
-}
\ No newline at end of file
+}
diff --git a/tests/test-utils.cpp b/tests/test-utils.cpp
index 87cd429..c12e335 100644
--- a/tests/test-utils.cpp
+++ b/tests/test-utils.cpp
@@ -4,8 +4,8 @@
* Author: Lawrence.Tang@arm.com
**/
-#include <stdio.h>
-#include <stdlib.h>
+#include <cstdio>
+#include <cstdlib>
#include "test-utils.hpp"
extern "C" {
#include "../edk/BaseTypes.h"
@@ -14,18 +14,22 @@
//Returns a ready-for-use memory stream containing a CPER record with the given sections inside.
FILE *generate_record_memstream(const char **types, UINT16 num_types,
- char **buf, size_t *buf_size, int single_section)
+ char **buf, size_t *buf_size,
+ int single_section)
{
//Open a memory stream.
FILE *stream = open_memstream(buf, buf_size);
//Generate a section to the stream, close & return.
- if (!single_section)
- generate_cper_record((char **)types, num_types, stream);
- else
- generate_single_section_record((char*)types[0], stream);
+ if (!single_section) {
+ generate_cper_record(const_cast<char **>(types), num_types,
+ stream);
+ } else {
+ generate_single_section_record(const_cast<char *>(types[0]),
+ stream);
+ }
fclose(stream);
//Return fmemopen() buffer for reading.
return fmemopen(*buf, *buf_size, "r");
-}
\ No newline at end of file
+}
diff --git a/tests/test-utils.hpp b/tests/test-utils.hpp
index 8e15560..2dedf8b 100644
--- a/tests/test-utils.hpp
+++ b/tests/test-utils.hpp
@@ -5,6 +5,8 @@
#include "../edk/BaseTypes.h"
}
-FILE* generate_record_memstream(const char** types, UINT16 num_types, char** buf, size_t* buf_size, int single_section);
+FILE *generate_record_memstream(const char **types, UINT16 num_types,
+ char **buf, size_t *buf_size,
+ int single_section);
-#endif
\ No newline at end of file
+#endif