Skip to content

Commit c05686b

Browse files
ossy-szegeddbatyai
authored andcommitted
Fix jerry_get_context_data() API function (#3127)
If manager_p->bytes_needed == 0, jerry_get_context_data() should return NULL pointer. Additionally init_cb, deinit_cb and finalize_cb should be called with NULL pointer in this case. JerryScript-DCO-1.0-Signed-off-by: Csaba Osztrogonác oszi@inf.u-szeged.hu
1 parent 40e63d1 commit c05686b

File tree

3 files changed

+59
-8
lines changed

3 files changed

+59
-8
lines changed

docs/02.API-REFERENCE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ for the item by default, and if the `init_cb` field is not NULL, it will be call
224224
an additional custom initializer. The `deinit_cb` (if non-`NULL`) is called during a call to `jerry_cleanup ()` to run
225225
any custom deinitialization *before* the VM has been fully cleaned up. The `finalize_cb` (if non-`NULL`) is also called
226226
during a call to `jerry_cleanup ()` to run any custom deinitialization *after* the VM has been fully cleaned up.
227+
If bytes_needed field is 0, no buffer is allocated for the manager, callback functions are called with NULL pointer.
227228

228229
**Prototype**
229230

jerry-core/api/jerry.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,8 @@ jerry_cleanup (void)
206206
{
207207
if (this_p->manager_p->deinit_cb)
208208
{
209-
this_p->manager_p->deinit_cb (JERRY_CONTEXT_DATA_HEADER_USER_DATA (this_p));
209+
void *data = (this_p->manager_p->bytes_needed > 0) ? JERRY_CONTEXT_DATA_HEADER_USER_DATA (this_p) : NULL;
210+
this_p->manager_p->deinit_cb (data);
210211
}
211212
}
212213

@@ -223,7 +224,8 @@ jerry_cleanup (void)
223224
next_p = this_p->next_p;
224225
if (this_p->manager_p->finalize_cb)
225226
{
226-
this_p->manager_p->finalize_cb (JERRY_CONTEXT_DATA_HEADER_USER_DATA (this_p));
227+
void *data = (this_p->manager_p->bytes_needed > 0) ? JERRY_CONTEXT_DATA_HEADER_USER_DATA (this_p) : NULL;
228+
this_p->manager_p->finalize_cb (data);
227229
}
228230
jmem_heap_free_block (this_p, sizeof (jerry_context_data_header_t) + this_p->manager_p->bytes_needed);
229231
}
@@ -249,17 +251,21 @@ jerry_get_context_data (const jerry_context_data_manager_t *manager_p)
249251
{
250252
if (item_p->manager_p == manager_p)
251253
{
252-
return JERRY_CONTEXT_DATA_HEADER_USER_DATA (item_p);
254+
return (manager_p->bytes_needed > 0) ? JERRY_CONTEXT_DATA_HEADER_USER_DATA (item_p) : NULL;
253255
}
254256
}
255257

256258
item_p = jmem_heap_alloc_block (sizeof (jerry_context_data_header_t) + manager_p->bytes_needed);
257259
item_p->manager_p = manager_p;
258260
item_p->next_p = JERRY_CONTEXT (context_data_p);
259261
JERRY_CONTEXT (context_data_p) = item_p;
260-
ret = JERRY_CONTEXT_DATA_HEADER_USER_DATA (item_p);
261262

262-
memset (ret, 0, manager_p->bytes_needed);
263+
if (manager_p->bytes_needed > 0)
264+
{
265+
ret = JERRY_CONTEXT_DATA_HEADER_USER_DATA (item_p);
266+
memset (ret, 0, manager_p->bytes_needed);
267+
}
268+
263269
if (manager_p->init_cb)
264270
{
265271
manager_p->init_cb (ret);

tests/unit-core/test-context-data.c

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@
2020
static bool test_context_data1_new_called = false;
2121
static bool test_context_data2_new_called = false;
2222
static bool test_context_data3_new_called = false;
23+
static bool test_context_data4_new_called = false;
2324
static bool test_context_data1_free_called = false;
2425
static bool test_context_data2_free_called = false;
26+
static bool test_context_data4_free_called = false;
2527
static bool test_context_data1_finalize_called = false;
28+
static bool test_context_data4_finalize_called = false;
2629

2730
/* Context item 1 */
2831
const char *string1 = "item1";
@@ -84,13 +87,12 @@ static const jerry_context_data_manager_t manager2 =
8487
};
8588

8689
/* Context item 3 */
87-
const char *string3 = "item3";
8890

8991
static void
9092
test_context_data3_new (void *user_data_p)
9193
{
94+
JERRY_UNUSED (user_data_p);
9295
test_context_data3_new_called = true;
93-
*((const char **) user_data_p) = string3;
9496
} /* test_context_data3_new */
9597

9698
static const jerry_context_data_manager_t manager3 =
@@ -102,6 +104,41 @@ static const jerry_context_data_manager_t manager3 =
102104
.bytes_needed = 0,
103105
};
104106

107+
/* Context item 4 */
108+
109+
static void
110+
test_context_data4_new (void *user_data_p)
111+
{
112+
test_context_data4_new_called = true;
113+
TEST_ASSERT (user_data_p == NULL);
114+
} /* test_context_data4_new */
115+
116+
117+
static void
118+
test_context_data4_free (void *user_data_p)
119+
{
120+
test_context_data4_free_called = true;
121+
TEST_ASSERT (user_data_p == NULL);
122+
TEST_ASSERT (!test_context_data4_finalize_called);
123+
} /* test_context_data4_free */
124+
125+
static void
126+
test_context_data4_finalize (void *user_data_p)
127+
{
128+
TEST_ASSERT (!test_context_data4_finalize_called);
129+
test_context_data4_finalize_called = true;
130+
TEST_ASSERT (user_data_p == NULL);
131+
} /* test_context_data4_finalize */
132+
133+
static const jerry_context_data_manager_t manager4 =
134+
{
135+
.init_cb = test_context_data4_new,
136+
.deinit_cb = test_context_data4_free,
137+
.finalize_cb = test_context_data4_finalize,
138+
.bytes_needed = 0
139+
};
140+
141+
105142
int
106143
main (void)
107144
{
@@ -111,19 +148,26 @@ main (void)
111148

112149
TEST_ASSERT (!strcmp (*((const char **) jerry_get_context_data (&manager1)), "item1"));
113150
TEST_ASSERT (!strcmp (*((const char **) jerry_get_context_data (&manager2)), "item2"));
114-
TEST_ASSERT (!strcmp (*((const char **) jerry_get_context_data (&manager3)), "item3"));
151+
TEST_ASSERT (jerry_get_context_data (&manager3) == NULL);
152+
TEST_ASSERT (jerry_get_context_data (&manager4) == NULL);
115153

116154
TEST_ASSERT (test_context_data1_new_called);
117155
TEST_ASSERT (test_context_data2_new_called);
118156
TEST_ASSERT (test_context_data3_new_called);
157+
TEST_ASSERT (test_context_data4_new_called);
119158

120159
TEST_ASSERT (!test_context_data1_free_called);
121160
TEST_ASSERT (!test_context_data2_free_called);
161+
TEST_ASSERT (!test_context_data4_free_called);
122162

123163
jerry_cleanup ();
124164

125165
TEST_ASSERT (test_context_data1_free_called);
126166
TEST_ASSERT (test_context_data2_free_called);
167+
TEST_ASSERT (test_context_data4_free_called);
168+
169+
TEST_ASSERT (test_context_data1_finalize_called);
170+
TEST_ASSERT (test_context_data4_finalize_called);
127171

128172
return 0;
129173
} /* main */

0 commit comments

Comments
 (0)