Skip to content

Commit

Permalink
Merge pull request #52 from larsewi/version
Browse files Browse the repository at this point in the history
 CFE-4379: Now checks versions in patches and blocks before interpreting them
  • Loading branch information
larsewi authored Apr 24, 2024
2 parents fa8baca + 8093ffa commit 5a6effa
Show file tree
Hide file tree
Showing 16 changed files with 299 additions and 163 deletions.
3 changes: 1 addition & 2 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,9 @@ AC_MSG_CHECKING([for debug option])
if test x"$debug" = x"yes"
then
AC_MSG_RESULT(yes)
CFLAGS="$CFLAGS -g3 -O0"
else
AC_MSG_RESULT(no)
CFLAGS="$CFLAGS -O2 -DNDEBUG"
CFLAGS="$CFLAGS -DNDEBUG"
fi

AC_CONFIG_FILES([Makefile lib/Makefile bin/Makefile tests/Makefile])
Expand Down
60 changes: 41 additions & 19 deletions lib/block.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,22 @@ LCH_Json *LCH_BlockCreate(const char *const parent_id,
return NULL;
}

LCH_Buffer *const version = LCH_BufferFromString(PACKAGE_VERSION);
if (version == NULL) {
LCH_JsonDestroy(block);
return NULL;
}

if (!LCH_JsonObjectSetString(block, LCH_BufferStaticFromString("version"),
version)) {
LCH_LOG_ERROR("Failed to set version field in block");
LCH_BufferDestroy(version);
LCH_JsonDestroy(block);
return NULL;
{
const LCH_Buffer *const key = LCH_BufferStaticFromString("version");
if (!LCH_JsonObjectSetNumber(block, key, (double)LCH_BLOCK_VERSION)) {
LCH_JsonDestroy(block);
return NULL;
}
}

const double timestamp = (double)time(NULL);
if (!LCH_JsonObjectSetNumber(block, LCH_BufferStaticFromString("timestamp"),
timestamp)) {
LCH_LOG_ERROR("Failed to set timestamp field in block");
LCH_JsonDestroy(block);
return NULL;
{
const time_t timestamp = time(NULL);
const LCH_Buffer *const key = LCH_BufferStaticFromString("timestamp");
if (!LCH_JsonObjectSetNumber(block, key, (double)timestamp)) {
LCH_LOG_ERROR("Failed to set timestamp field in block");
LCH_JsonDestroy(block);
return NULL;
}
}

LCH_Buffer *const parent = LCH_BufferFromString(parent_id);
Expand Down Expand Up @@ -119,6 +115,20 @@ bool LCH_BlockStore(const LCH_Instance *const instance,
return true;
}

bool LCH_BlockGetVersion(const LCH_Json *const block, size_t *const version) {
double value;
const LCH_Buffer *const key = LCH_BufferStaticFromString("version");
if (!LCH_JsonObjectGetNumber(block, key, &value)) {
return false;
}

if (!LCH_DoubleToSize(value, version)) {
return false;
}

return true;
}

LCH_Json *LCH_BlockLoad(const char *const work_dir,
const char *const block_id) {
char path[PATH_MAX];
Expand All @@ -131,8 +141,20 @@ LCH_Json *LCH_BlockLoad(const char *const work_dir,
LCH_LOG_ERROR("Failed to parse block with identifer %.7s", block_id);
return NULL;
}
LCH_LOG_DEBUG("Parsed block with identifer %.7s", block_id);

size_t version;
if (!LCH_BlockGetVersion(block, &version)) {
LCH_JsonDestroy(block);
return NULL;
}

LCH_LOG_DEBUG("Parsed JSON from block with identifer %.7s", block_id);
if (version > LCH_PATCH_VERSION) {
LCH_LOG_ERROR("Unsupported block version %zu", version);
LCH_JsonDestroy(block);
return NULL;
}
LCH_LOG_DEBUG("Block version number is %zu", version);
return block;
}

Expand Down
1 change: 1 addition & 0 deletions lib/block.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
LCH_Json *LCH_BlockCreate(const char *parent_id, LCH_Json *const payload);
bool LCH_BlockStore(const LCH_Instance *const instance, const LCH_Json *block);
LCH_Json *LCH_BlockLoad(const char *work_dir, const char *block_id);
bool LCH_BlockGetVersion(const LCH_Json *patch, size_t *version);
const char *LCH_BlockGetParentId(const LCH_Json *block);
bool LCH_BlockIsGenisisId(const char *block_id);
const LCH_Json *LCH_BlockGetPayload(const LCH_Json *block);
Expand Down
7 changes: 2 additions & 5 deletions lib/buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,8 @@ bool LCH_BufferPrintFormat(LCH_Buffer *const self, const char *const format,
}

va_start(ap, format);
const int ret = vsnprintf(self->buffer + self->length,
self->capacity - self->length, format, ap);
#ifdef NDEBUG
LCH_UNUSED(ret);
#endif // NDEBUG
LCH_NDEBUG_UNUSED const int ret = vsnprintf(
self->buffer + self->length, self->capacity - self->length, format, ap);
assert(length >= 0);
va_end(ap);
if (length < 0) {
Expand Down
10 changes: 9 additions & 1 deletion lib/definitions.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#ifndef _LEECH_DEFINITIONS_H
#define _LEECH_DEFINITIONS_H

#define LCH_PATCH_VERSION 1
#define LCH_BLOCK_VERSION 1

#define LCH_KIBIBYTE(n) (n * 1024UL)
#define LCH_MEBIBYTE(n) (n * 1024UL * 1024UL)
#define LCH_GIBIBYTE(n) (n * 1024ULL * 1024ULL * 1024ULL)
Expand All @@ -11,7 +14,12 @@
#define LCH_MIN(a, b) ((a < b) ? a : b)
#define LCH_MAX(a, b) ((a > b) ? a : b)

#define LCH_UNUSED(x) (void)x
#define LCH_UNUSED __attribute__((unused))
#ifdef NDEBUG
#define LCH_NDEBUG_UNUSED __attribute__((unused))
#else
#define LCH_NDEBUG_UNUSED
#endif

#ifdef _WIN32
#define LCH_PATH_SEP '\\'
Expand Down
Loading

0 comments on commit 5a6effa

Please sign in to comment.