Skip to content

Commit cae38f4

Browse files
committed
Add new input validator API functions
Fixes #1549 JerryScript-DCO-1.0-Signed-off-by: László Langó [email protected]
1 parent 75d8226 commit cae38f4

File tree

7 files changed

+180
-9
lines changed

7 files changed

+180
-9
lines changed

docs/02.API-REFERENCE.md

Lines changed: 116 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1233,6 +1233,7 @@ jerry_get_string_size (const jerry_value_t value);
12331233

12341234
- [jerry_create_string](#jerry_create_string)
12351235
- [jerry_get_string_length](#jerry_get_string_length)
1236+
- [jerry_is_cesu8_string_valid](#jerry_is_cesu8_string_valid)
12361237

12371238

12381239
## jerry_get_utf8_string_size
@@ -1272,6 +1273,8 @@ jerry_get_utf8_string_size (const jerry_value_t value);
12721273

12731274
- [jerry_create_string_from_utf8](#jerry_create_string_from_utf8)
12741275
- [jerry_get_utf8_string_length](#jerry_get_utf8_string_length)
1276+
- [jerry_is_utf8_string_valid](#jerry_is_utf8_string_valid)
1277+
12751278

12761279
## jerry_get_string_length
12771280

@@ -1308,6 +1311,8 @@ jerry_get_string_length (const jerry_value_t value);
13081311

13091312
- [jerry_create_string](#jerry_create_string)
13101313
- [jerry_get_string_size](#jerry_get_string_size)
1314+
- [jerry_is_cesu8_string_valid](#jerry_is_cesu8_string_valid)
1315+
13111316

13121317
## jerry_get_utf8_string_length
13131318

@@ -1347,6 +1352,8 @@ jerry_get_utf8_string_length (const jerry_value_t value);
13471352

13481353
- [jerry_create_string_from_utf8](#jerry_create_string_from_utf8)
13491354
- [jerry_get_utf8_string_size](#jerry_get_utf8_string_size)
1355+
- [jerry_is_utf8_string_valid](#jerry_is_utf8_string_valid)
1356+
13501357

13511358
## jerry_string_to_char_buffer
13521359

@@ -1394,6 +1401,8 @@ jerry_string_to_char_buffer (const jerry_value_t value,
13941401

13951402
- [jerry_create_string](#jerry_create_string)
13961403
- [jerry_get_string_size](#jerry_get_string_size)
1404+
- [jerry_is_cesu8_string_valid](#jerry_is_cesu8_string_valid)
1405+
13971406

13981407
## jerry_string_to_utf8_char_buffer
13991408

@@ -1441,6 +1450,8 @@ jerry_string_to_utf8_char_buffer (const jerry_value_t value,
14411450

14421451
- [jerry_create_string_from_utf8](#jerry_create_string_from_utf8)
14431452
- [jerry_get_utf8_string_size](#jerry_get_utf8_string_size)
1453+
- [jerry_is_utf8_string_valid](#jerry_is_utf8_string_valid)
1454+
14441455

14451456
## jerry_substring_to_char_buffer
14461457

@@ -1496,6 +1507,8 @@ jerry_substring_to_char_buffer (const jerry_value_t value,
14961507
- [jerry_create_string](#jerry_create_string)
14971508
- [jerry_get_string_size](#jerry_get_string_size)
14981509
- [jerry_get_string_length](#jerry_get_string_length)
1510+
- [jerry_is_cesu8_string_valid](#jerry_is_cesu8_string_valid)
1511+
14991512

15001513
## jerry_substring_to_utf8_char_buffer
15011514

@@ -1548,9 +1561,12 @@ jerry_substring_to_utf8_char_buffer (const jerry_value_t value,
15481561

15491562
**See also**
15501563

1551-
- [jerry_create_string_from_utf8](#jerry_create_string)
1564+
- [jerry_create_string_from_utf8](#jerry_create_string_from_utf8)
15521565
- [jerry_get_utf8_string_size](#jerry_get_utf8_string_size)
15531566
- [jerry_get_utf8_string_length](#jerry_get_utf8_string_length)
1567+
- [jerry_is_utf8_string_valid](#jerry_is_utf8_string_valid)
1568+
1569+
15541570
# Functions for array object values
15551571

15561572
## jerry_get_array_length
@@ -2260,6 +2276,7 @@ jerry_create_string (const jerry_char_t *str_p);
22602276

22612277
**See also**
22622278

2279+
- [jerry_is_cesu8_string_valid](#jerry_is_cesu8_string_valid)
22632280
- [jerry_create_string_sz](#jerry_create_string_sz)
22642281

22652282

@@ -2298,8 +2315,10 @@ jerry_create_string_sz (const jerry_char_t *str_p,
22982315

22992316
**See also**
23002317

2318+
- [jerry_is_cesu8_string_valid](#jerry_is_cesu8_string_valid)
23012319
- [jerry_create_string](#jerry_create_string)
23022320

2321+
23032322
## jerry_create_string_from_utf8
23042323

23052324
**Summary**
@@ -2333,6 +2352,7 @@ jerry_create_string_from_utf8 (const jerry_char_t *str_p);
23332352

23342353
**See also**
23352354

2355+
- [jerry_is_utf8_string_valid](#jerry_is_utf8_string_valid)
23362356
- [jerry_create_string_sz_from_utf8](#jerry_create_string_sz_from_utf8)
23372357

23382358

@@ -2373,8 +2393,10 @@ jerry_create_string_sz (const jerry_char_t *str_p,
23732393

23742394
**See also**
23752395

2396+
- [jerry_is_utf8_string_valid](#jerry_is_utf8_string_valid)
23762397
- [jerry_create_string_from_utf8](#jerry_create_string_from_utf8)
23772398

2399+
23782400
## jerry_create_undefined
23792401

23802402
**Summary**
@@ -3287,6 +3309,99 @@ bool foreach_function (const jerry_value_t prop_name,
32873309
- [jerry_object_property_foreach_t](#jerry_object_property_foreach_t)
32883310

32893311

3312+
# Input validatator functions
3313+
3314+
## jerry_is_utf8_string_valid
3315+
3316+
**Summary**
3317+
3318+
Validate UTF-8 string.
3319+
3320+
**Prototype**
3321+
3322+
```c
3323+
bool
3324+
jerry_is_utf8_string_valid (const jerry_char_t *utf8_buf_p, /**< UTF-8 string */
3325+
jerry_size_t buf_size) /**< string size */
3326+
```
3327+
3328+
- `utf8_buf_p` - UTF-8 input string
3329+
- `buf_size` - input string size
3330+
3331+
**Example**
3332+
3333+
```c
3334+
{
3335+
const jerry_char_t script[] = "print ('Hello, World!');";
3336+
size_t script_size = strlen ((const char *) script);
3337+
3338+
if (jerry_is_utf8_string_valid (script, (jerry_size_t) script_size))
3339+
{
3340+
jerry_run_simple (script, script_size, JERRY_INIT_EMPTY);
3341+
}
3342+
}
3343+
```
3344+
3345+
**See also**
3346+
3347+
- [jerry_run_simple](#jerry_run_simple)
3348+
- [jerry_create_string_from_utf8](#jerry_create_string_from_utf8)
3349+
- [jerry_create_string_sz_from_utf8](#jerry_create_string_sz_from_utf8)
3350+
- [jerry_get_utf8_string_size](#jerry_get_utf8_string_size)
3351+
- [jerry_get_utf8_string_length](#jerry_get_utf8_string_length)
3352+
- [jerry_string_to_utf8_char_buffer](#jerry_string_to_utf8_char_buffer)
3353+
- [jerry_substring_to_utf8_char_buffer](#jerry_substring_to_utf8_char_buffer)
3354+
3355+
## jerry_is_cesu8_string_valid
3356+
3357+
**Summary**
3358+
3359+
Validate CESU-8 string.
3360+
3361+
**Prototype**
3362+
3363+
```c
3364+
bool
3365+
jerry_is_cesu8_string_valid (const jerry_char_t *cesu8_buf_p, /**< CESU-8 string */
3366+
jerry_size_t buf_size) /**< string size */
3367+
```
3368+
3369+
- `cesu8_buf_p` - CESU-8 input string
3370+
- `buf_size` - input string size
3371+
3372+
**Example**
3373+
3374+
```c
3375+
{
3376+
jerry_init (JERRY_INIT_EMPTY);
3377+
3378+
const jerry_char_t script[] = "Hello, World!";
3379+
size_t script_size = strlen ((const char *) script);
3380+
3381+
if (jerry_is_cesu8_string_valid (script, (jerry_size_t) script_size))
3382+
{
3383+
jerry_value_t string_value = jerry_create_string_sz (script,
3384+
(jerry_size_t) script_size));
3385+
3386+
... // usage of string_value
3387+
3388+
jerry_release_value (string_value);
3389+
}
3390+
3391+
jerry_cleanup ();
3392+
}
3393+
```
3394+
3395+
**See also**
3396+
3397+
- [jerry_create_string](#jerry_create_string)
3398+
- [jerry_create_string_sz](#jerry_create_string_sz)
3399+
- [jerry_get_string_size](#jerry_get_string_size)
3400+
- [jerry_get_string_length](#jerry_get_string_length)
3401+
- [jerry_string_to_char_buffer](#jerry_string_to_char_buffer)
3402+
- [jerry_substring_to_char_buffer](#jerry_substring_to_char_buffer)
3403+
3404+
32903405
# Snapshot functions
32913406

32923407
## jerry_parse_and_save_snapshot

jerry-core/jerry.c

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -982,7 +982,7 @@ jerry_create_object (void)
982982
} /* jerry_create_object */
983983

984984
/**
985-
* Create string from a valid UTF8 string
985+
* Create string from a valid UTF-8 string
986986
*
987987
* Note:
988988
* returned value must be freed with jerry_release_value when it is no longer needed.
@@ -996,7 +996,7 @@ jerry_create_string_from_utf8 (const jerry_char_t *str_p) /**< pointer to string
996996
} /* jerry_create_string_from_utf8 */
997997

998998
/**
999-
* Create string from a valid UTF8 string
999+
* Create string from a valid UTF-8 string
10001000
*
10011001
* Note:
10021002
* returned value must be freed with jerry_release_value when it is no longer needed.
@@ -1016,7 +1016,7 @@ jerry_create_string_sz_from_utf8 (const jerry_char_t *str_p, /**< pointer to str
10161016
} /* jerry_create_string_sz_from_utf8 */
10171017

10181018
/**
1019-
* Create string from a valid CESU8 string
1019+
* Create string from a valid CESU-8 string
10201020
*
10211021
* Note:
10221022
* returned value must be freed with jerry_release_value, when it is no longer needed.
@@ -1030,7 +1030,7 @@ jerry_create_string (const jerry_char_t *str_p) /**< pointer to string */
10301030
} /* jerry_create_string */
10311031

10321032
/**
1033-
* Create string from a valid CESU8 string
1033+
* Create string from a valid CESU-8 string
10341034
*
10351035
* Note:
10361036
* returned value must be freed with jerry_release_value when it is no longer needed.
@@ -2006,6 +2006,34 @@ jerry_foreach_object_property (const jerry_value_t obj_val, /**< object value */
20062006
return false;
20072007
} /* jerry_foreach_object_property */
20082008

2009+
/**
2010+
* Validate UTF-8 string
2011+
*
2012+
* @return true - if UTF-8 string is well-formed
2013+
* false - otherwise
2014+
*/
2015+
bool
2016+
jerry_is_utf8_string_valid (const jerry_char_t *utf8_buf_p, /**< UTF-8 string */
2017+
jerry_size_t buf_size) /**< string size */
2018+
{
2019+
return lit_is_utf8_string_valid ((lit_utf8_byte_t *) utf8_buf_p,
2020+
(lit_utf8_size_t) buf_size);
2021+
} /* jerry_is_utf8_string_valid */
2022+
2023+
/**
2024+
* Validate CESU-8 string
2025+
*
2026+
* @return true - if CESU-8 string is well-formed
2027+
* false - otherwise
2028+
*/
2029+
bool
2030+
jerry_is_cesu8_string_valid (const jerry_char_t *cesu8_buf_p, /**< CESU-8 string */
2031+
jerry_size_t buf_size) /**< string size */
2032+
{
2033+
return lit_is_cesu8_string_valid ((lit_utf8_byte_t *) cesu8_buf_p,
2034+
(lit_utf8_size_t) buf_size);
2035+
} /* jerry_is_cesu8_string_valid */
2036+
20092037
/**
20102038
* @}
20112039
*/

jerry-core/jerryscript.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,12 @@ void jerry_set_object_native_handle (const jerry_value_t obj_val, uintptr_t hand
329329
bool jerry_foreach_object_property (const jerry_value_t obj_val, jerry_object_property_foreach_t foreach_p,
330330
void *user_data_p);
331331

332+
/**
333+
* Input validatator functions
334+
*/
335+
bool jerry_is_utf8_string_valid (const jerry_char_t *utf8_buf_p, jerry_size_t buf_size);
336+
bool jerry_is_cesu8_string_valid (const jerry_char_t *cesu8_buf_p, jerry_size_t buf_size);
337+
332338
/**
333339
* Snapshot functions
334340
*/

jerry-core/lit/lit-strings.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,14 @@ lit_is_utf8_string_valid (const lit_utf8_byte_t *utf8_buf_p, /**< utf-8 string *
125125
* false otherwise
126126
*/
127127
bool
128-
lit_is_cesu8_string_valid (const lit_utf8_byte_t *utf8_buf_p, /**< utf-8 string */
128+
lit_is_cesu8_string_valid (const lit_utf8_byte_t *cesu8_buf_p, /**< cesu-8 string */
129129
lit_utf8_size_t buf_size) /**< string size */
130130
{
131131
lit_utf8_size_t idx = 0;
132132

133133
while (idx < buf_size)
134134
{
135-
lit_utf8_byte_t c = utf8_buf_p[idx++];
135+
lit_utf8_byte_t c = cesu8_buf_p[idx++];
136136
if ((c & LIT_UTF8_1_BYTE_MASK) == LIT_UTF8_1_BYTE_MARKER)
137137
{
138138
continue;
@@ -166,7 +166,7 @@ lit_is_cesu8_string_valid (const lit_utf8_byte_t *utf8_buf_p, /**< utf-8 string
166166

167167
for (lit_utf8_size_t offset = 0; offset < extra_bytes_count; ++offset)
168168
{
169-
c = utf8_buf_p[idx + offset];
169+
c = cesu8_buf_p[idx + offset];
170170
if ((c & LIT_UTF8_EXTRA_BYTE_MASK) != LIT_UTF8_EXTRA_BYTE_MARKER)
171171
{
172172
/* invalid continuation byte */

jerry-core/lit/lit-strings.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686

8787
/* validation */
8888
bool lit_is_utf8_string_valid (const lit_utf8_byte_t *utf8_buf_p, lit_utf8_size_t buf_size);
89-
bool lit_is_cesu8_string_valid (const lit_utf8_byte_t *utf8_buf_p, lit_utf8_size_t buf_size);
89+
bool lit_is_cesu8_string_valid (const lit_utf8_byte_t *cesu8_buf_p, lit_utf8_size_t buf_size);
9090

9191
/* checks */
9292
bool lit_is_code_point_utf16_low_surrogate (lit_code_point_t code_point);

jerry-main/main-unix.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,12 @@ main (int argc,
663663
break;
664664
}
665665

666+
if (!jerry_is_utf8_string_valid (source_p, (jerry_size_t) source_size))
667+
{
668+
ret_value = jerry_create_error (JERRY_ERROR_COMMON, (jerry_char_t *) ("Input must be a valid UTF-8 string."));
669+
break;
670+
}
671+
666672
if (jerry_is_feature_enabled (JERRY_FEATURE_SNAPSHOT_SAVE) && (is_save_snapshot_mode || is_save_literals_mode))
667673
{
668674
static uint8_t snapshot_save_buffer[ JERRY_BUFFER_SIZE ];
@@ -686,6 +692,7 @@ main (int argc,
686692
fclose (snapshot_file_p);
687693
}
688694
}
695+
689696
if (!jerry_value_has_error_flag (ret_value) && is_save_literals_mode)
690697
{
691698
const size_t literal_buffer_size = jerry_parse_and_save_literals ((jerry_char_t *) source_p,
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright JS Foundation and other contributors, http://js.foundation
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
va'Ôc=

0 commit comments

Comments
 (0)