Skip to content

Commit efa8850

Browse files
galpeteryichoi
authored andcommitted
Use binary mode when opening via fopen in the tools (#2371)
In the snapshot tool the files were opened in text mode. However the snapshot files are binary files thus it is advised to use the binary mode when opening the files. Specifying the binary mode is a must on Windows platform otherwise the read/write operations are inserting extra '\r' characters. To make the tools consitent across OSes all fopen are now opening files in binary mode. Also update jerry-libc to accept the 'b' modifier and add a test case where the JS file uses CR-LF line endings. JerryScript-DCO-1.0-Signed-off-by: Peter Gal pgal.u-szeged@partner.samsung.com
1 parent 9ae60a4 commit efa8850

File tree

5 files changed

+41
-10
lines changed

5 files changed

+41
-10
lines changed

jerry-libc/target/posix/jerry-libc-target.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,32 +113,41 @@ fopen (const char *path, /**< file path */
113113
bool truncate = false;
114114
bool create_if_not_exist = false;
115115
bool position_at_end = false;
116+
int modifier_position = 1;
116117

117118
assert (path != NULL && mode != NULL);
118-
assert (mode[1] == '+' || mode[1] == '\0');
119+
assert (mode[1] == '\0'
120+
|| (mode[1] == '+' && mode[2] == '\0')
121+
|| (mode[1] == 'b' && mode[2] == '\0')
122+
|| (mode[1] == 'b' && mode[2] == '+' && mode[3] == '\0'));
123+
124+
if (mode[1] == 'b')
125+
{
126+
modifier_position = 2;
127+
}
119128

120129
switch (mode[0])
121130
{
122131
case 'r':
123132
{
124133
may_read = true;
125-
may_write = (mode[1] == '+');
134+
may_write = (mode[modifier_position] == '+');
126135
break;
127136
}
128137
case 'w':
129138
{
130139
may_write = true;
131140
truncate = true;
132141
create_if_not_exist = true;
133-
may_read = (mode[1] == '+');
142+
may_read = (mode[modifier_position] == '+');
134143
break;
135144
}
136145
case 'a':
137146
{
138147
may_write = true;
139148
position_at_end = true;
140149
create_if_not_exist = true;
141-
if (mode[1] == '+')
150+
if (mode[modifier_position] == '+')
142151
{
143152
assert (false && "unsupported mode a+");
144153
}

jerry-main/main-unix-snapshot.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ static size_t
9292
read_file (uint8_t *input_pos_p, /**< next position in the input buffer */
9393
const char *file_name) /**< file name */
9494
{
95-
FILE *file = fopen (file_name, "r");
95+
FILE *file = fopen (file_name, "rb");
9696

9797
if (file == NULL)
9898
{
@@ -331,7 +331,7 @@ process_generate (cli_state_t *cli_state_p, /**< cli state */
331331
size_t snapshot_size = (size_t) jerry_get_number_value (snapshot_result);
332332
jerry_release_value (snapshot_result);
333333

334-
FILE *snapshot_file_p = fopen (output_file_name_p, "w");
334+
FILE *snapshot_file_p = fopen (output_file_name_p, "wb");
335335
if (snapshot_file_p == NULL)
336336
{
337337
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: Unable to write snapshot file: '%s'\n", output_file_name_p);
@@ -357,7 +357,7 @@ process_generate (cli_state_t *cli_state_p, /**< cli state */
357357
return JERRY_STANDALONE_EXIT_CODE_FAIL;
358358
}
359359

360-
FILE *literal_file_p = fopen (save_literals_file_name_p, "w");
360+
FILE *literal_file_p = fopen (save_literals_file_name_p, "wb");
361361

362362
if (literal_file_p == NULL)
363363
{
@@ -486,7 +486,7 @@ process_merge (cli_state_t *cli_state_p, /**< cli state */
486486
return JERRY_STANDALONE_EXIT_CODE_FAIL;
487487
}
488488

489-
FILE *file_p = fopen (output_file_name_p, "w");
489+
FILE *file_p = fopen (output_file_name_p, "wb");
490490

491491
if (file_p != NULL)
492492
{

jerry-main/main-unix-test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ static const uint8_t *
3636
read_file (const char *file_name,
3737
size_t *out_size_p)
3838
{
39-
FILE *file = fopen (file_name, "r");
39+
FILE *file = fopen (file_name, "rb");
4040
if (file == NULL)
4141
{
4242
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: failed to open file: %s\n", file_name);

jerry-main/main-unix.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ read_file (const char *file_name,
5959
}
6060
else
6161
{
62-
file = fopen (file_name, "r");
62+
file = fopen (file_name, "rb");
6363
if (file == NULL)
6464
{
6565
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: failed to open file: %s\n", file_name);

tests/jerry/windows-line-ending.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
// This test file should use CR-LF styled line endings to test if everything is
16+
// parsed correctly
17+
18+
19+
var value =
20+
5;
21+
22+
assert (value === 5);

0 commit comments

Comments
 (0)