Skip to content

Commit 64dd2f9

Browse files
committed
Revert previous commit and fix issue introduced in 67cccf0.
1 parent 083a785 commit 64dd2f9

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

libyara/exec.c

+9-3
Original file line numberDiff line numberDiff line change
@@ -470,9 +470,10 @@ int yr_execute_code(YR_SCAN_CONTEXT* context)
470470
yr_arena_create(1, 512 * sizeof(YR_OBJECT*), &obj_arena),
471471
yr_free(stack.items));
472472

473-
FAIL_ON_ERROR_WITH_CLEANUP(yr_notebook_create(1048576, &it_notebook),
474-
yr_arena_release(obj_arena);
475-
yr_free(stack.items));
473+
FAIL_ON_ERROR_WITH_CLEANUP(
474+
yr_notebook_create(512 * sizeof(YR_ITERATOR), &it_notebook),
475+
yr_arena_release(obj_arena);
476+
yr_free(stack.items));
476477

477478
#ifdef YR_PROFILING_ENABLED
478479
start_time = yr_stopwatch_elapsed_ns(&context->stopwatch);
@@ -651,6 +652,11 @@ int yr_execute_code(YR_SCAN_CONTEXT* context)
651652
it_notebook,
652653
sizeof(YR_ITERATOR) + sizeof(SIZED_STRING*) * (size_t) r1.i);
653654

655+
memset(
656+
r3.p,
657+
0xcc,
658+
sizeof(YR_ITERATOR) + sizeof(SIZED_STRING*) * (size_t) r1.i);
659+
654660
if (r3.p == NULL)
655661
{
656662
result = ERROR_INSUFFICIENT_MEMORY;

libyara/notebook.c

+17-12
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,17 @@ typedef struct YR_NOTEBOOK_PAGE YR_NOTEBOOK_PAGE;
4848
// all the buffers allocated via yr_notebook_alloc().
4949
struct YR_NOTEBOOK
5050
{
51-
// Size of pages in the notebook. Most pages are this size, but some
52-
// of them can be 2x, 3x, or in general Nx this size. This happens when
53-
// yr_notebook_alloc is called with a size that is larger than page_size,
54-
// which means that the notebook needs to allocate a page that is larger
55-
// than the rest for accomodating the requested buffer.
56-
size_t page_size;
51+
// The mininum size of each page in the notebook.
52+
size_t min_page_size;
5753
// Pointer to the first page in the book, this is also the most recently
5854
// created page, the one that is being filled.
5955
YR_NOTEBOOK_PAGE* page_list_head;
6056
};
6157

6258
struct YR_NOTEBOOK_PAGE
6359
{
60+
// Size of this page.
61+
size_t size;
6462
// Amount of bytes in the page that are actually used.
6563
size_t used;
6664
// Pointer to next page.
@@ -81,23 +79,24 @@ struct YR_NOTEBOOK_PAGE
8179
// ERROR_SUCCESS
8280
// ERROR_INSUFFICIENT_MEMORY
8381
//
84-
int yr_notebook_create(size_t page_size, YR_NOTEBOOK** notebook)
82+
int yr_notebook_create(size_t min_page_size, YR_NOTEBOOK** notebook)
8583
{
8684
YR_NOTEBOOK* new_notebook = yr_malloc(sizeof(YR_NOTEBOOK));
8785

8886
if (new_notebook == NULL)
8987
return ERROR_INSUFFICIENT_MEMORY;
9088

9189
new_notebook->page_list_head = yr_malloc(
92-
sizeof(YR_NOTEBOOK_PAGE) + page_size);
90+
sizeof(YR_NOTEBOOK_PAGE) + min_page_size);
9391

9492
if (new_notebook->page_list_head == NULL)
9593
{
9694
yr_free(new_notebook);
9795
return ERROR_INSUFFICIENT_MEMORY;
9896
}
9997

100-
new_notebook->page_size = page_size;
98+
new_notebook->min_page_size = min_page_size;
99+
new_notebook->page_list_head->size = min_page_size;
101100
new_notebook->page_list_head->used = 0;
102101
new_notebook->page_list_head->next = NULL;
103102

@@ -151,20 +150,26 @@ void* yr_notebook_alloc(YR_NOTEBOOK* notebook, size_t size)
151150
// deferrencing pointers to types larger than a byte.
152151
size = (size + 7) & ~0x7;
153152

153+
YR_NOTEBOOK_PAGE* current_page = notebook->page_list_head;
154+
154155
// If the requested size doesn't fit in current page's free space, allocate
155156
// a new page.
156-
if (notebook->page_size - notebook->page_list_head->used < size)
157+
if (current_page->size - current_page->used < size)
157158
{
159+
size_t min_size = notebook->min_page_size;
160+
158161
// The new page must be able to fit the requested buffer, so find the
159-
// multiple of notebook->page_size that is larger than size.
160-
size_t page_size = (size / notebook->page_size + 1) * notebook->page_size;
162+
// multiple of notebook->min_page_size that is larger or equal than than
163+
// size.
164+
size_t page_size = (size / min_size) * min_size + min_size;
161165

162166
YR_NOTEBOOK_PAGE* new_page = yr_malloc(
163167
sizeof(YR_NOTEBOOK_PAGE) + page_size);
164168

165169
if (new_page == NULL)
166170
return NULL;
167171

172+
new_page->size = page_size;
168173
new_page->used = 0;
169174
new_page->next = notebook->page_list_head;
170175
notebook->page_list_head = new_page;

0 commit comments

Comments
 (0)