summaryrefslogtreecommitdiff
path: root/src/domain
diff options
context:
space:
mode:
Diffstat (limited to 'src/domain')
-rw-r--r--src/domain/message.c49
-rw-r--r--src/domain/search.c53
2 files changed, 87 insertions, 15 deletions
diff --git a/src/domain/message.c b/src/domain/message.c
index d973be4..b1db3e1 100644
--- a/src/domain/message.c
+++ b/src/domain/message.c
@@ -3,6 +3,8 @@
#include <stdlib.h>
#include <string.h>
+#include "base/allocator.h"
+
static char *
duplicate_string(const char *value)
{
@@ -15,9 +17,20 @@ duplicate_string(const char *value)
return copy;
}
+static int
+copy_failed(tiny_message_t *message)
+{
+ tiny_message_free(message);
+ return -1;
+}
+
void
tiny_message_init(tiny_message_t *message)
{
+ if (message == NULL)
+ {
+ return;
+ }
memset(message, 0, sizeof(*message));
message->metadata.memory_tier = TINY_TIER_SHORT;
}
@@ -26,6 +39,10 @@ void
tiny_message_free(tiny_message_t *message)
{
size_t index;
+ if (message == NULL)
+ {
+ return;
+ }
free(message->session_id);
free(message->content);
free(message->metadata.tool_call_id);
@@ -41,6 +58,12 @@ int
tiny_message_copy(tiny_message_t *destination, const tiny_message_t *source)
{
size_t index;
+ if (destination == NULL || source == NULL || destination == source || source->session_id == NULL
+ || source->content == NULL
+ || (source->metadata.tag_count != 0 && source->metadata.tags == NULL))
+ {
+ return -1;
+ }
tiny_message_init(destination);
memcpy(destination->message_id, source->message_id, sizeof(destination->message_id));
destination->timestamp_ms = source->timestamp_ms;
@@ -50,14 +73,14 @@ tiny_message_copy(tiny_message_t *destination, const tiny_message_t *source)
destination->content = duplicate_string(source->content);
if (destination->session_id == NULL || destination->content == NULL)
{
- goto failure;
+ return copy_failed(destination);
}
if (source->metadata.tool_call_id != NULL)
{
destination->metadata.tool_call_id = duplicate_string(source->metadata.tool_call_id);
if (destination->metadata.tool_call_id == NULL)
{
- goto failure;
+ return copy_failed(destination);
}
}
if (source->metadata.tag_count > 0)
@@ -65,23 +88,19 @@ tiny_message_copy(tiny_message_t *destination, const tiny_message_t *source)
destination->metadata.tags = calloc(source->metadata.tag_count, sizeof(char *));
if (destination->metadata.tags == NULL)
{
- goto failure;
+ return copy_failed(destination);
}
for (index = 0; index < source->metadata.tag_count; ++index)
{
destination->metadata.tags[index] = duplicate_string(source->metadata.tags[index]);
if (destination->metadata.tags[index] == NULL)
{
- goto failure;
+ return copy_failed(destination);
}
destination->metadata.tag_count++;
}
}
return 0;
-
-failure:
- tiny_message_free(destination);
- return -1;
}
const char *
@@ -103,6 +122,10 @@ tiny_role_name(tiny_role_t role)
bool
tiny_role_parse(const char *value, tiny_role_t *role)
{
+ if (value == NULL || role == NULL)
+ {
+ return false;
+ }
if (strcmp(value, "user") == 0)
{
*role = TINY_ROLE_USER;
@@ -143,6 +166,10 @@ tiny_tier_name(tiny_memory_tier_t tier)
bool
tiny_tier_parse(const char *value, tiny_memory_tier_t *tier)
{
+ if (value == NULL || tier == NULL)
+ {
+ return false;
+ }
if (strcmp(value, "short") == 0)
{
*tier = TINY_TIER_SHORT;
@@ -170,6 +197,10 @@ bool
tiny_utf8_valid(const char *value, size_t length)
{
size_t index = 0;
+ if (value == NULL)
+ {
+ return length == 0;
+ }
while (index < length)
{
const unsigned char first = (unsigned char)value[index++];
@@ -240,7 +271,7 @@ tiny_message_id_valid(const char *value)
{
static const char alphabet[] = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
size_t index;
- if (strlen(value) != TINY_MESSAGE_ID_LENGTH || value[0] > '7')
+ if (value == NULL || strlen(value) != TINY_MESSAGE_ID_LENGTH || value[0] > '7')
{
return false;
}
diff --git a/src/domain/search.c b/src/domain/search.c
index a24b667..b0ac440 100644
--- a/src/domain/search.c
+++ b/src/domain/search.c
@@ -4,6 +4,8 @@
#include <stdlib.h>
#include <string.h>
+#include "base/allocator.h"
+
typedef struct gram_bucket
{
uint64_t hash;
@@ -25,9 +27,25 @@ struct tiny_search_index
static bool
decode_codepoints(const char *text, uint32_t **output, size_t *count)
{
- const size_t bytes = strlen(text);
+ size_t bytes;
size_t offset = 0, used = 0;
- uint32_t *points = malloc((bytes + 1) * sizeof(uint32_t));
+ uint32_t *points;
+ if (output == NULL || count == NULL)
+ {
+ return false;
+ }
+ *output = NULL;
+ *count = 0;
+ if (text == NULL)
+ {
+ return false;
+ }
+ bytes = strlen(text);
+ if (!tiny_utf8_valid(text, bytes) || bytes > SIZE_MAX / sizeof(uint32_t) - 1)
+ {
+ return false;
+ }
+ points = malloc((bytes + 1) * sizeof(uint32_t));
if (points == NULL)
{
return false;
@@ -198,6 +216,12 @@ tiny_search_index_add(tiny_search_index_t *index,
size_t message_index)
{
size_t tag;
+ if (index == NULL || message == NULL || message->content == NULL
+ || (message->metadata.tag_count != 0 && message->metadata.tags == NULL)
+ || message_index == SIZE_MAX)
+ {
+ return false;
+ }
if (!index_text(index, message->content, message_index))
{
return false;
@@ -226,8 +250,16 @@ tiny_search_index_candidates(const tiny_search_index_t *index,
size_t point_count = 0, width, offset, used = 0;
bool *seen;
size_t *items;
+ if (output == NULL || count == NULL)
+ {
+ return false;
+ }
*output = NULL;
*count = 0;
+ if (index == NULL || query == NULL || index->message_count > SIZE_MAX / sizeof(*items))
+ {
+ return false;
+ }
if (!decode_codepoints(query, &points, &point_count))
{
return false;
@@ -283,10 +315,20 @@ tiny_search_score(const char *query, const tiny_message_t *message)
size_t query_count = 0, message_count = 0, width, query_offset, message_offset, matches = 0,
total, tag;
double score = 0.0;
- if (!decode_codepoints(query, &query_points, &query_count)
- || !decode_codepoints(message->content, &message_points, &message_count) || query_count == 0)
+ if (query == NULL || message == NULL || message->content == NULL
+ || (message->metadata.tag_count != 0 && message->metadata.tags == NULL))
+ {
+ return 0.0;
+ }
+ if (!decode_codepoints(query, &query_points, &query_count) || query_count == 0)
+ {
+ free(query_points);
+ return 0.0;
+ }
+ if (!decode_codepoints(message->content, &message_points, &message_count))
{
- goto done;
+ free(query_points);
+ return 0.0;
}
width = query_count >= 3 ? 3 : (query_count >= 2 ? 2 : 1);
total = query_count - width + 1;
@@ -343,7 +385,6 @@ tiny_search_score(const char *query, const tiny_message_t *message)
}
}
}
-done:
free(query_points);
free(message_points);
return score;