@@ -187,9 +187,11 @@ HRESULT Helpers::LoadScriptFromFile(LPCSTR filenameToLoad, LPCSTR& contents, UIN
187
187
188
188
HRESULT hr = S_OK;
189
189
BYTE * pRawBytes = nullptr ;
190
+ BYTE * pRawBytesFromMap = nullptr ;
190
191
UINT lengthBytes = 0 ;
191
192
contents = nullptr ;
192
193
FILE * file = NULL ;
194
+ size_t bufferLength = 0 ;
193
195
194
196
LPCSTR filename = filenameToLoad;
195
197
if (sHostApplicationPathBufferLength == (uint)-1 )
@@ -225,11 +227,8 @@ HRESULT Helpers::LoadScriptFromFile(LPCSTR filenameToLoad, LPCSTR& contents, UIN
225
227
if (SourceMap::Find (filenameToLoad, strlen (filenameToLoad), &data) ||
226
228
SourceMap::Find (filename, strlen (filename), &data))
227
229
{
228
- contents = data->GetString ();
229
- if (lengthBytesOut != nullptr )
230
- {
231
- *lengthBytesOut = (UINT) data->GetLength ();
232
- }
230
+ pRawBytesFromMap = (BYTE*) data->GetString ();
231
+ lengthBytes = (UINT) data->GetLength ();
233
232
}
234
233
else
235
234
{
@@ -263,88 +262,109 @@ HRESULT Helpers::LoadScriptFromFile(LPCSTR filenameToLoad, LPCSTR& contents, UIN
263
262
264
263
IfFailGo (E_FAIL);
265
264
}
265
+ }
266
266
267
- if (file != NULL )
268
- {
269
- // Determine the file length, in bytes.
270
- fseek (file, 0 , SEEK_END);
271
- lengthBytes = ftell (file);
272
- fseek (file, 0 , SEEK_SET);
273
- const size_t bufferLength = lengthBytes + sizeof (BYTE);
274
- pRawBytes = (LPBYTE)malloc (bufferLength);
275
- if (nullptr == pRawBytes)
276
- {
277
- fwprintf (stderr, _u (" out of memory" ));
278
- IfFailGo (E_OUTOFMEMORY);
279
- }
267
+ if (file != NULL )
268
+ {
269
+ // Determine the file length, in bytes.
270
+ fseek (file, 0 , SEEK_END);
271
+ lengthBytes = ftell (file);
272
+ fseek (file, 0 , SEEK_SET);
273
+ }
280
274
281
- //
282
- // Read the entire content as a binary block.
283
- //
284
- size_t readBytes = fread (pRawBytes, sizeof (BYTE), lengthBytes, file);
285
- if (readBytes < lengthBytes * sizeof (BYTE))
286
- {
287
- IfFailGo (E_FAIL);
288
- }
275
+ if (lengthBytes != 0 )
276
+ {
277
+ bufferLength = lengthBytes + sizeof (BYTE);
278
+ pRawBytes = (LPBYTE)malloc (bufferLength);
279
+ }
289
280
290
- pRawBytes[lengthBytes] = 0 ; // Null terminate it. Could be UTF16
281
+ if (nullptr == pRawBytes)
282
+ {
283
+ fwprintf (stderr, _u (" out of memory" ));
284
+ IfFailGo (E_OUTOFMEMORY);
285
+ }
291
286
292
- //
293
- // Read encoding to make sure it's supported
294
- //
295
- // Warning: The UNICODE buffer for parsing is supposed to be provided by the host.
296
- // This is not a complete read of the encoding. Some encodings like UTF7, UTF1, EBCDIC, SCSU, BOCU could be
297
- // wrongly classified as ANSI
298
- //
299
- {
300
- #pragma warning(push)
301
- // suppressing prefast warning that "readable size is bufferLength bytes but 2 may be read" as bufferLength is clearly > 2 in the code that follows
302
- #pragma warning(disable:6385)
303
- C_ASSERT (sizeof (WCHAR) == 2 );
304
- if (bufferLength > 2 )
305
- {
306
- __analysis_assume (bufferLength > 2 );
307
- #pragma prefast(push)
308
- #pragma prefast(disable:6385, "PREfast incorrectly reports this as an out-of-bound access.");
309
- if ((pRawBytes[0 ] == 0xFE && pRawBytes[1 ] == 0xFF ) ||
310
- (pRawBytes[0 ] == 0xFF && pRawBytes[1 ] == 0xFE ) ||
311
- (bufferLength > 4 && pRawBytes[0 ] == 0x00 && pRawBytes[1 ] == 0x00 &&
312
- ((pRawBytes[2 ] == 0xFE && pRawBytes[3 ] == 0xFF ) ||
313
- (pRawBytes[2 ] == 0xFF && pRawBytes[3 ] == 0xFE ))))
314
-
315
- {
316
- // unicode unsupported
317
- fwprintf (stderr, _u (" unsupported file encoding. Only ANSI and UTF8 supported" ));
318
- IfFailGo (E_UNEXPECTED);
319
- }
320
- #pragma prefast(pop)
321
- }
322
- }
323
- #pragma warning(pop)
287
+ if (file != NULL )
288
+ {
289
+ //
290
+ // Read the entire content as a binary block.
291
+ //
292
+ size_t readBytes = fread (pRawBytes, sizeof (BYTE), lengthBytes, file);
293
+ if (readBytes < lengthBytes * sizeof (BYTE))
294
+ {
295
+ IfFailGo (E_FAIL);
324
296
}
297
+ }
298
+ else // from module source register
299
+ {
300
+ // Q: module source is on persistent memory. Why do we use the copy instead?
301
+ // A: if we use the same memory twice, ch doesn't know that during FinalizeCallback free.
302
+ // the copy memory will be freed by the finalizer
303
+ Assert (pRawBytesFromMap);
304
+ memcpy_s (pRawBytes, bufferLength, pRawBytesFromMap, lengthBytes);
305
+ }
325
306
326
- contents = reinterpret_cast <LPCSTR>(pRawBytes);
307
+ if (pRawBytes)
308
+ {
309
+ pRawBytes[lengthBytes] = 0 ; // Null terminate it. Could be UTF16
310
+ }
327
311
328
- Error:
329
- if (SUCCEEDED (hr))
312
+ if (file != NULL )
313
+ {
314
+ //
315
+ // Read encoding to make sure it's supported
316
+ //
317
+ // Warning: The UNICODE buffer for parsing is supposed to be provided by the host.
318
+ // This is not a complete read of the encoding. Some encodings like UTF7, UTF1, EBCDIC, SCSU, BOCU could be
319
+ // wrongly classified as ANSI
320
+ //
321
+ #pragma warning(push)
322
+ // suppressing prefast warning that "readable size is bufferLength
323
+ // bytes but 2 may be read" as bufferLength is clearly > 2 in the code that follows
324
+ #pragma warning(disable:6385)
325
+ C_ASSERT (sizeof (WCHAR) == 2 );
326
+ if (bufferLength > 2 )
330
327
{
331
- if (lengthBytesOut)
328
+ __analysis_assume (bufferLength > 2 );
329
+ #pragma prefast(push)
330
+ #pragma prefast(disable:6385, "PREfast incorrectly reports this as an out-of-bound access.");
331
+ if ((pRawBytes[0 ] == 0xFE && pRawBytes[1 ] == 0xFF ) ||
332
+ (pRawBytes[0 ] == 0xFF && pRawBytes[1 ] == 0xFE ) ||
333
+ (bufferLength > 4 && pRawBytes[0 ] == 0x00 && pRawBytes[1 ] == 0x00 &&
334
+ ((pRawBytes[2 ] == 0xFE && pRawBytes[3 ] == 0xFF ) ||
335
+ (pRawBytes[2 ] == 0xFF && pRawBytes[3 ] == 0xFE ))))
336
+
332
337
{
333
- *lengthBytesOut = lengthBytes;
338
+ // unicode unsupported
339
+ fwprintf (stderr, _u (" unsupported file encoding. Only ANSI and UTF8 supported" ));
340
+ IfFailGo (E_UNEXPECTED);
334
341
}
342
+ #pragma prefast(pop)
335
343
}
344
+ #pragma warning(pop)
345
+ }
336
346
337
- if (file != NULL )
338
- {
339
- fclose (file);
340
- }
347
+ contents = reinterpret_cast <LPCSTR>(pRawBytes);
341
348
342
- if (pRawBytes && reinterpret_cast <LPCSTR>(pRawBytes) != contents)
349
+ Error:
350
+ if (SUCCEEDED (hr))
351
+ {
352
+ if (lengthBytesOut)
343
353
{
344
- free (pRawBytes) ;
354
+ *lengthBytesOut = lengthBytes ;
345
355
}
346
356
}
347
357
358
+ if (file != NULL )
359
+ {
360
+ fclose (file);
361
+ }
362
+
363
+ if (pRawBytes && reinterpret_cast <LPCSTR>(pRawBytes) != contents)
364
+ {
365
+ free (pRawBytes);
366
+ }
367
+
348
368
return hr;
349
369
}
350
370
0 commit comments