Skip to content

Commit dcdf698

Browse files
author
Zsolt Borbély
committed
Modify the usage of ecma_string_to_utf8_string()
Parts: * Rename ecma_string_to_utf8_string() to ecma_string_copy_to_utf8_buffer. * Introduce ecma_string_to_utf8_bytes(), which wraps the usual 'function call-assertion' pair, and check strict equality of size of the string and the buffer. JerryScript-DCO-1.0-Signed-off-by: Zsolt Borbély zsborbely.u-szeged@partner.samsung.com
1 parent b5efa2e commit dcdf698

File tree

6 files changed

+51
-53
lines changed

6 files changed

+51
-53
lines changed

jerry-core/ecma/base/ecma-helpers-string.c

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -395,10 +395,8 @@ ecma_concat_ecma_strings (ecma_string_t *string1_p, /**< first ecma-string */
395395
string_desc_p->refs_and_container = ECMA_STRING_CONTAINER_HEAP_ASCII_STRING | ECMA_STRING_REF_ONE;
396396
const size_t data_size = new_size;
397397
lit_utf8_byte_t *data_p = (lit_utf8_byte_t *) jmem_heap_alloc_block (data_size);
398-
lit_utf8_size_t bytes_copied = ecma_string_to_utf8_string (string1_p, data_p, str1_size);
399-
JERRY_ASSERT (bytes_copied == str1_size);
400-
bytes_copied = ecma_string_to_utf8_string (string2_p, data_p + str1_size, str2_size);
401-
JERRY_ASSERT (bytes_copied == str2_size);
398+
ecma_string_to_utf8_bytes (string1_p, data_p, str1_size);
399+
ecma_string_to_utf8_bytes (string2_p, data_p + str1_size, str2_size);
402400

403401
string_desc_p->u.ascii_string.size = (uint16_t) new_size;
404402

@@ -411,15 +409,14 @@ ecma_concat_ecma_strings (ecma_string_t *string1_p, /**< first ecma-string */
411409
string_desc_p->refs_and_container = ECMA_STRING_CONTAINER_HEAP_UTF8_STRING | ECMA_STRING_REF_ONE;
412410
const size_t data_size = new_size + sizeof (ecma_string_heap_header_t);
413411
ecma_string_heap_header_t *data_p = (ecma_string_heap_header_t *) jmem_heap_alloc_block (data_size);
414-
lit_utf8_size_t bytes_copied = ecma_string_to_utf8_string (string1_p,
415-
(lit_utf8_byte_t *) (data_p + 1),
416-
str1_size);
417-
JERRY_ASSERT (bytes_copied == str1_size);
418-
419-
bytes_copied = ecma_string_to_utf8_string (string2_p,
420-
(lit_utf8_byte_t *) (data_p + 1) + str1_size,
421-
str2_size);
422-
JERRY_ASSERT (bytes_copied == str2_size);
412+
ecma_string_to_utf8_bytes (string1_p,
413+
(lit_utf8_byte_t *) (data_p + 1),
414+
str1_size);
415+
416+
ecma_string_to_utf8_bytes (string2_p,
417+
(lit_utf8_byte_t *) (data_p + 1) + str1_size,
418+
str2_size);
419+
423420
JERRY_ASSERT (string_length <= UINT16_MAX);
424421

425422
data_p->size = (uint16_t) new_size;
@@ -675,10 +672,10 @@ ecma_string_get_array_index (const ecma_string_t *str_p, /**< ecma-string */
675672
* @return number of bytes, actually copied to the buffer.
676673
*/
677674
lit_utf8_size_t __attr_return_value_should_be_checked___
678-
ecma_string_to_utf8_string (const ecma_string_t *string_desc_p, /**< ecma-string descriptor */
679-
lit_utf8_byte_t *buffer_p, /**< destination buffer pointer
680-
* (can be NULL if buffer_size == 0) */
681-
lit_utf8_size_t buffer_size) /**< size of buffer */
675+
ecma_string_copy_to_utf8_buffer (const ecma_string_t *string_desc_p, /**< ecma-string descriptor */
676+
lit_utf8_byte_t *buffer_p, /**< destination buffer pointer
677+
* (can be NULL if buffer_size == 0) */
678+
lit_utf8_size_t buffer_size) /**< size of buffer */
682679
{
683680
JERRY_ASSERT (string_desc_p != NULL);
684681
JERRY_ASSERT (string_desc_p->refs_and_container >= ECMA_STRING_REF_ONE);
@@ -741,7 +738,24 @@ ecma_string_to_utf8_string (const ecma_string_t *string_desc_p, /**< ecma-string
741738

742739
JERRY_ASSERT (size <= buffer_size);
743740
return size;
744-
} /* ecma_string_to_utf8_string */
741+
} /* ecma_string_copy_to_utf8_buffer */
742+
743+
/**
744+
* Convert ecma-string's contents to a cesu-8 string and put it to the buffer.
745+
* It is the caller's responsibility to make sure that the string fits in the buffer.
746+
* Check if the size of the string is equal with the size of the buffer.
747+
*/
748+
void __attr_always_inline___
749+
ecma_string_to_utf8_bytes (const ecma_string_t *string_desc_p, /**< ecma-string descriptor */
750+
lit_utf8_byte_t *buffer_p, /**< destination buffer pointer
751+
* (can be NULL if buffer_size == 0) */
752+
lit_utf8_size_t buffer_size) /**< size of buffer */
753+
{
754+
JERRY_ASSERT (ecma_string_get_size (string_desc_p) == buffer_size);
755+
756+
lit_utf8_size_t size = ecma_string_copy_to_utf8_buffer (string_desc_p, buffer_p, buffer_size);
757+
JERRY_ASSERT (size == buffer_size);
758+
} /* ecma_string_to_utf8_bytes */
745759

746760
/**
747761
* Lengths for numeric string values
@@ -1018,8 +1032,7 @@ ecma_compare_ecma_strings_longpath (const ecma_string_t *string1_p, /* ecma-stri
10181032
{
10191033
utf8_string1_p = (lit_utf8_byte_t *) jmem_heap_alloc_block ((size_t) strings_size);
10201034

1021-
lit_utf8_size_t bytes_copied = ecma_string_to_utf8_string (string1_p, utf8_string1_p, strings_size);
1022-
JERRY_ASSERT (bytes_copied == strings_size);
1035+
ecma_string_to_utf8_bytes (string1_p, utf8_string1_p, strings_size);
10231036

10241037
is_utf8_string1_on_heap = true;
10251038
}
@@ -1049,11 +1062,11 @@ ecma_compare_ecma_strings_longpath (const ecma_string_t *string1_p, /* ecma-stri
10491062
{
10501063
utf8_string2_p = (lit_utf8_byte_t *) jmem_heap_alloc_block ((size_t) strings_size);
10511064

1052-
lit_utf8_size_t bytes_copied = ecma_string_to_utf8_string (string2_p, utf8_string2_p, strings_size);
1053-
JERRY_ASSERT (bytes_copied == strings_size);
1065+
ecma_string_to_utf8_bytes (string2_p, utf8_string2_p, strings_size);
10541066

10551067
is_utf8_string2_on_heap = true;
10561068
}
1069+
10571070
const bool is_equal = !strncmp ((char *) utf8_string1_p, (char *) utf8_string2_p, (size_t) strings_size);
10581071

10591072
if (is_utf8_string1_on_heap)
@@ -1160,8 +1173,7 @@ ecma_compare_ecma_strings_relational (const ecma_string_t *string1_p, /**< ecma-
11601173
utf8_string1_p = utf8_string1_buffer;
11611174
}
11621175

1163-
lit_utf8_size_t bytes_copied = ecma_string_to_utf8_string (string1_p, utf8_string1_p, utf8_string1_size);
1164-
JERRY_ASSERT (bytes_copied == utf8_string1_size);
1176+
ecma_string_to_utf8_bytes (string1_p, utf8_string1_p, utf8_string1_size);
11651177
}
11661178

11671179
if (ECMA_STRING_GET_CONTAINER (string2_p) == ECMA_STRING_CONTAINER_HEAP_UTF8_STRING)
@@ -1202,8 +1214,7 @@ ecma_compare_ecma_strings_relational (const ecma_string_t *string1_p, /**< ecma-
12021214
utf8_string2_p = utf8_string2_buffer;
12031215
}
12041216

1205-
lit_utf8_size_t bytes_copied = ecma_string_to_utf8_string (string2_p, utf8_string2_p, utf8_string2_size);
1206-
JERRY_ASSERT (bytes_copied == utf8_string2_size);
1217+
ecma_string_to_utf8_bytes (string2_p, utf8_string2_p, utf8_string2_size);
12071218
}
12081219

12091220
bool is_first_less_than_second = lit_compare_utf8_strings_relational (utf8_string1_p,
@@ -1345,8 +1356,7 @@ ecma_string_get_char_at_pos (const ecma_string_t *string_p, /**< ecma-string */
13451356

13461357
JMEM_DEFINE_LOCAL_ARRAY (utf8_str_p, buffer_size, lit_utf8_byte_t);
13471358

1348-
lit_utf8_size_t sz = ecma_string_to_utf8_string (string_p, utf8_str_p, buffer_size);
1349-
JERRY_ASSERT (sz == buffer_size);
1359+
ecma_string_to_utf8_bytes (string_p, utf8_str_p, buffer_size);
13501360

13511361
JERRY_ASSERT (ECMA_STRING_GET_CONTAINER (string_p) == ECMA_STRING_CONTAINER_UINT32_IN_DESC);
13521362
/* Both above must be ascii strings. */
@@ -1504,8 +1514,7 @@ ecma_string_substr (const ecma_string_t *string_p, /**< pointer to an ecma strin
15041514

15051515
JMEM_DEFINE_LOCAL_ARRAY (utf8_str_p, buffer_size, lit_utf8_byte_t);
15061516

1507-
lit_utf8_size_t sz = ecma_string_to_utf8_string (string_p, utf8_str_p, buffer_size);
1508-
JERRY_ASSERT (sz == buffer_size);
1517+
ecma_string_to_utf8_bytes (string_p, utf8_str_p, buffer_size);
15091518

15101519
/**
15111520
* II. Extract substring

jerry-core/ecma/base/ecma-helpers.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@
6666
if (utf8_ptr == NULL) \
6767
{ \
6868
utf8_ptr = (const lit_utf8_byte_t *) jmem_heap_alloc_block (utf8_str_size); \
69-
lit_utf8_size_t sz = ecma_string_to_utf8_string (ecma_str_ptr, (lit_utf8_byte_t *) utf8_ptr, utf8_str_size); \
70-
JERRY_ASSERT (sz == utf8_str_size); \
69+
ecma_string_to_utf8_bytes (ecma_str_ptr, (lit_utf8_byte_t *) utf8_ptr, utf8_str_size); \
7170
utf8_ptr ## must_be_freed = true; \
7271
}
7372

@@ -177,7 +176,8 @@ extern ecma_number_t ecma_string_to_number (const ecma_string_t *);
177176
extern bool ecma_string_get_array_index (const ecma_string_t *, uint32_t *);
178177

179178
extern lit_utf8_size_t __attr_return_value_should_be_checked___
180-
ecma_string_to_utf8_string (const ecma_string_t *, lit_utf8_byte_t *, lit_utf8_size_t);
179+
ecma_string_copy_to_utf8_buffer (const ecma_string_t *, lit_utf8_byte_t *, lit_utf8_size_t);
180+
extern void ecma_string_to_utf8_bytes (const ecma_string_t *, lit_utf8_byte_t *, lit_utf8_size_t);
181181
extern const lit_utf8_byte_t *ecma_string_raw_chars (const ecma_string_t *, lit_utf8_size_t *, bool *);
182182

183183
extern bool ecma_compare_ecma_strings_equal_hashes (const ecma_string_t *, const ecma_string_t *);

jerry-core/ecma/builtin-objects/ecma-builtin-error-prototype.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ ecma_builtin_error_prototype_object_to_string (ecma_value_t this_arg) /**< this
142142
JMEM_DEFINE_LOCAL_ARRAY (ret_str_buffer, size, lit_utf8_byte_t);
143143
lit_utf8_byte_t *ret_str_buffer_p = ret_str_buffer;
144144

145-
lit_utf8_size_t bytes = ecma_string_to_utf8_string (name_string_p, ret_str_buffer_p, name_size);
145+
lit_utf8_size_t bytes = ecma_string_copy_to_utf8_buffer (name_string_p, ret_str_buffer_p, name_size);
146146
JERRY_ASSERT (bytes == name_size);
147147
ret_str_buffer_p = ret_str_buffer_p + bytes;
148148
JERRY_ASSERT (ret_str_buffer_p <= ret_str_buffer + size);
@@ -157,7 +157,7 @@ ecma_builtin_error_prototype_object_to_string (ecma_value_t this_arg) /**< this
157157
space_size);
158158
JERRY_ASSERT (ret_str_buffer_p <= ret_str_buffer + size);
159159

160-
bytes = ecma_string_to_utf8_string (msg_string_p, ret_str_buffer_p, msg_size);
160+
bytes = ecma_string_copy_to_utf8_buffer (msg_string_p, ret_str_buffer_p, msg_size);
161161
JERRY_ASSERT (bytes == msg_size);
162162
ret_str_buffer_p = ret_str_buffer_p + bytes;
163163
JERRY_ASSERT (ret_str_buffer_p == ret_str_buffer + size);

jerry-core/ecma/builtin-objects/ecma-builtin-global.c

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,7 @@ ecma_builtin_global_object_print (ecma_value_t this_arg __attr_unused___, /**< t
8585
utf8_str_size,
8686
lit_utf8_byte_t);
8787

88-
lit_utf8_size_t actual_sz = ecma_string_to_utf8_string (str_p, utf8_str_p, utf8_str_size);
89-
JERRY_ASSERT (actual_sz == utf8_str_size);
88+
ecma_string_to_utf8_bytes (str_p, utf8_str_p, utf8_str_size);
9089

9190
const lit_utf8_byte_t *utf8_str_curr_p = utf8_str_p;
9291
const lit_utf8_byte_t *utf8_str_end_p = utf8_str_p + utf8_str_size;
@@ -739,10 +738,7 @@ ecma_builtin_global_object_decode_uri_helper (ecma_value_t uri __attr_unused___,
739738
input_size + 1,
740739
lit_utf8_byte_t);
741740

742-
lit_utf8_size_t sz = ecma_string_to_utf8_string (input_string_p,
743-
input_start_p,
744-
input_size);
745-
JERRY_ASSERT (sz == input_size);
741+
ecma_string_to_utf8_bytes (input_string_p, input_start_p, input_size);
746742

747743
input_start_p[input_size] = LIT_BYTE_NULL;
748744

@@ -1017,10 +1013,7 @@ ecma_builtin_global_object_encode_uri_helper (ecma_value_t uri, /**< uri argumen
10171013
input_size,
10181014
lit_utf8_byte_t);
10191015

1020-
lit_utf8_size_t sz = ecma_string_to_utf8_string (input_string_p,
1021-
input_start_p,
1022-
input_size);
1023-
JERRY_ASSERT (sz == input_size);
1016+
ecma_string_to_utf8_bytes (input_string_p, input_start_p, input_size);
10241017

10251018
/*
10261019
* The URI encoding has two major phases: first we validate the input,
@@ -1238,10 +1231,7 @@ ecma_builtin_global_object_escape (ecma_value_t this_arg __attr_unused___, /**<
12381231
input_size,
12391232
lit_utf8_byte_t);
12401233

1241-
lit_utf8_size_t sz = ecma_string_to_utf8_string (input_string_p,
1242-
input_start_p,
1243-
input_size);
1244-
JERRY_ASSERT (sz == input_size);
1234+
ecma_string_to_utf8_bytes (input_string_p, input_start_p, input_size);
12451235

12461236
/*
12471237
* The escape routine has two major phases: first we compute
@@ -1358,8 +1348,7 @@ ecma_builtin_global_object_unescape (ecma_value_t this_arg __attr_unused___, /**
13581348

13591349
/* 3. */
13601350
JMEM_DEFINE_LOCAL_ARRAY (input_start_p, input_size, lit_utf8_byte_t);
1361-
lit_utf8_size_t sz = ecma_string_to_utf8_string (input_string_p, input_start_p, input_size);
1362-
JERRY_ASSERT (sz == input_size);
1351+
ecma_string_to_utf8_bytes (input_string_p, input_start_p, input_size);
13631352

13641353
const lit_utf8_byte_t *input_curr_p = input_start_p;
13651354
const lit_utf8_byte_t *input_end_p = input_start_p + input_size;

jerry-core/ecma/builtin-objects/ecma-builtin-json.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ ecma_builtin_json_parse (ecma_value_t this_arg __attr_unused___, /**< 'this' arg
712712

713713
JMEM_DEFINE_LOCAL_ARRAY (str_start_p, buffer_size, lit_utf8_byte_t);
714714

715-
const lit_utf8_size_t sz = ecma_string_to_utf8_string (string_p, str_start_p, buffer_size);
715+
const lit_utf8_size_t sz = ecma_string_copy_to_utf8_buffer (string_p, str_start_p, buffer_size);
716716
JERRY_ASSERT (sz == string_size);
717717

718718
str_start_p[string_size] = LIT_BYTE_NULL;

jerry-core/jerry.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ jerry_string_to_char_buffer (const jerry_string_t *string_p, /**< string descrip
540540
{
541541
jerry_assert_api_available ();
542542

543-
return ecma_string_to_utf8_string (string_p, (lit_utf8_byte_t *) buffer_p, buffer_size);
543+
return ecma_string_copy_to_utf8_buffer (string_p, (lit_utf8_byte_t *) buffer_p, buffer_size);
544544
} /* jerry_string_to_char_buffer */
545545

546546
/**

0 commit comments

Comments
 (0)