• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

polserver / polserver / 21066490217

16 Jan 2026 12:22PM UTC coverage: 60.507%. Remained the same
21066490217

push

github

web-flow
misc clang-tidy (#853)

* trigger tidy

* Automated clang-tidy change: modernize-use-equals-delete,modernize-make-shared,modernize-make-unique,modernize-use-constraints,readability-container-size-empty,modernize-redundant-void-arg,modernize-use-emplace

* removed non needed macros

* missed to disable tidy build

---------

Co-authored-by: Clang Tidy <clang-tidy@users.noreply.github.com>

174 of 248 new or added lines in 74 files covered. (70.16%)

2 existing lines in 2 files now uncovered.

44459 of 73477 relevant lines covered (60.51%)

515895.79 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

60.76
/pol-core/bscript/eprog_read.cpp
1
/** @file
2
 *
3
 * @par History
4
 * - 2005/12/07 MuadDib: Added "Recompile required" to Bad version reports.
5
 */
6

7
#include <cstdio>
8
#include <exception>
9
#include <map>
10
#include <string>
11

12
#include "../clib/logfacility.h"
13
#include "../clib/rawtypes.h"
14
#include "../clib/strutil.h"
15
#include "eprog.h"
16
#include "executor.h"
17
#include "filefmt.h"
18
#include "fmodule.h"
19
#include "modules.h"
20
#include "objmethods.h"
21
#include "symcont.h"
22
#include "token.h"
23
#include "tokens.h"
24

25
namespace Pol
26
{
27
namespace Bscript
28
{
29
/**
30
 * Opens and ECL file containing bytecode and reads it
31
 *
32
 * This is where script bytecode processing is done
33
 */
34
int EScriptProgram::read( const char* fname )
2,143✔
35
{
36
  FILE* fp = nullptr;
2,143✔
37

38
  try
39
  {
40
    name = fname;
2,143✔
41

42
    fp = fopen( fname, "rb" );
2,143✔
43
    if ( !fp )
2,143✔
44
      throw std::runtime_error( std::string( "Unable to open " ) + fname + " for reading." );
1✔
45

46
    BSCRIPT_FILE_HDR hdr;
47
    if ( fread( &hdr, sizeof hdr, 1, fp ) != 1 )
2,142✔
48
    {
49
      ERROR_PRINTLN( "Error loading script {}: error reading header", fname );
×
50
      fclose( fp );
×
51
      return -1;
×
52
    }
53
    if ( hdr.magic2[0] != BSCRIPT_FILE_MAGIC0 || hdr.magic2[1] != BSCRIPT_FILE_MAGIC1 )
2,142✔
54
    {
55
      ERROR_PRINTLN( "Error loading script {}: bad magic value '{}{}'", fname, hdr.magic2[0],
×
56
                     hdr.magic2[1] );
57
      fclose( fp );
×
58
      return -1;
×
59
    }
60
    // auto-check for latest version (see filefmt.h for setting)
61
    if ( hdr.version != ESCRIPT_FILE_VER_CURRENT )
2,142✔
62
    {
63
      ERROR_PRINTLN( "Error loading script {}: Recompile required. Bad version number {}", fname,
×
64
                     hdr.version );
65
      fclose( fp );
×
66
      return -1;
×
67
    }
68
    version = hdr.version;
2,142✔
69
    nglobals = hdr.globals;
2,142✔
70
    BSCRIPT_SECTION_HDR sechdr;
71
    while ( fread( &sechdr, sizeof sechdr, 1, fp ) == 1 )
10,172✔
72
    {
73
      switch ( sechdr.type )
8,030✔
74
      {
75
      case BSCRIPT_SECTION_PROGDEF:
364✔
76
        if ( read_progdef_hdr( fp ) )
364✔
77
        {
78
          ERROR_PRINTLN( "Error loading script {}: error reading progdef section", fname );
×
79
          fclose( fp );
×
80
          return -1;
×
81
        }
82
        break;
364✔
83

84
      case BSCRIPT_SECTION_MODULE:
2,686✔
85
        if ( read_module( fp ) )
2,686✔
86
        {
87
          ERROR_PRINTLN( "Error loading script {}: error reading module section", fname );
×
88
          fclose( fp );
×
89
          return -1;
×
90
        }
91
        break;
2,686✔
92
      case BSCRIPT_SECTION_CODE:
2,142✔
93
        tokens.read( fp );
2,142✔
94
        break;
2,142✔
95
      case BSCRIPT_SECTION_SYMBOLS:
2,142✔
96
        symbols.read( fp );
2,142✔
97
        break;
2,142✔
98
      case BSCRIPT_SECTION_GLOBALVARNAMES:
×
99
        if ( read_globalvarnames( fp ) )
×
100
        {
101
          ERROR_PRINTLN( "Error loading script {}: error reading global variable name section",
×
102
                         fname );
103
          fclose( fp );
×
104
          return -1;
×
105
        }
106
        break;
×
107
      case BSCRIPT_SECTION_EXPORTED_FUNCTIONS:
105✔
108
        if ( read_exported_functions( fp, &sechdr ) )
105✔
109
        {
110
          ERROR_PRINTLN( "Error loading script {}: error reading exported functions section",
×
111
                         fname );
112
          fclose( fp );
×
113
          return -1;
×
114
        }
115
        break;
105✔
116
      case BSCRIPT_SECTION_FUNCTION_REFERENCES:
385✔
117
        if ( read_function_references( fp, &sechdr ) )
385✔
118
        {
119
          ERROR_PRINTLN( "Error loading script {}: error reading function references section",
×
120
                         fname );
121
          fclose( fp );
×
122
          return -1;
×
123
        }
124
        break;
385✔
125
      case BSCRIPT_SECTION_CLASS_TABLE:
206✔
126
        if ( read_class_table( fp ) )
206✔
127
        {
128
          ERROR_PRINTLN( "Error loading script {}: error reading class table section", fname );
×
129
          fclose( fp );
×
130
          return -1;
×
131
        }
132
        break;
206✔
133
      default:
×
134
        ERROR_PRINTLN( "Error loading script {}: unknown section type {}", fname, sechdr.type );
×
135
        fclose( fp );
×
136
        return -1;
×
137
      }
138
    }
139
    fclose( fp );
2,142✔
140

141
    return create_instructions();
2,142✔
142
  }
143
  catch ( std::exception& ex )
1✔
144
  {
145
    ERROR_PRINTLN( "Exception caught while loading script {}: {}", fname, ex.what() );
1✔
146
    if ( fp != nullptr )
1✔
147
      fclose( fp );
×
148
    return -1;
1✔
149
  }
1✔
150
#ifndef WIN32
151
  catch ( ... )
×
152
  {
153
    ERROR_PRINTLN( "Exception caught while loading script {}", fname );
×
154
    if ( fp != nullptr )
×
155
      fclose( fp );
×
156
    return -1;
×
157
  }
×
158
#endif
159
}
160
int EScriptProgram::create_instructions()
2,142✔
161
{
162
  int nLines = tokens.length() / sizeof( StoredToken );
2,142✔
163
  instr.resize( nLines );  // = new Instruction[ nLines ];
2,142✔
164

165
  for ( int i = 0; i < nLines; i++ )
162,498✔
166
  {
167
    Instruction& ins = instr[i];
160,356✔
168
    if ( _readToken( ins.token, i ) )
160,356✔
169
      return -1;
×
170

171
    // executor only:
172
    ins.func = Executor::GetInstrFunc( ins.token );
160,356✔
173
  }
174
  return 0;
2,142✔
175
}
176

177
/**
178
 * Reads the Program Header section from the file pointer
179
 */
180
int EScriptProgram::read_progdef_hdr( FILE* fp )
364✔
181
{
182
  BSCRIPT_PROGDEF_HDR hdr;
183
  if ( fread( &hdr, sizeof hdr, 1, fp ) != 1 )
364✔
184
    return -1;
×
185

186
  haveProgram = true;
364✔
187
  expectedArgs = hdr.expectedArgs;
364✔
188
  return 0;
364✔
189
}
190

191
/**
192
 * Reads a module "usages" section from the file pointer
193
 */
194
int EScriptProgram::read_module( FILE* fp )
2,686✔
195
{
196
  BSCRIPT_MODULE_HDR hdr;
197
  if ( fread( &hdr, sizeof hdr, 1, fp ) != 1 )
2,686✔
198
    return -1;
×
199
  auto fm = new FunctionalityModule( hdr.modulename );
2,686✔
200
  for ( unsigned i = 0; i < hdr.nfuncs; i++ )
6,256✔
201
  {
202
    BSCRIPT_MODULE_FUNCTION func;
203
    if ( fread( &func, sizeof func, 1, fp ) != 1 )
3,570✔
204
    {
205
      delete fm;
×
206
      return -1;
×
207
    }
208
    fm->addFunction( func.funcname, func.nargs );
3,570✔
209
  }
210
  modules.push_back( fm );
2,686✔
211
  return 0;
2,686✔
212
}
213

214
/* Note: This function is ONLY used from Executor::read(). */
215
int EScriptProgram::_readToken( Token& token, unsigned position ) const
160,356✔
216
{
217
  StoredToken st;
160,356✔
218
  tokens.atGet1( position, st );
160,356✔
219
  // StoredToken& st = tokens[position];
220

221
  token.module = (ModuleID)st.module;
160,356✔
222
  token.id = static_cast<BTokenId>( st.id );
160,356✔
223
  token.type = static_cast<BTokenType>( st.type );
160,356✔
224
  token.lval = st.offset;
160,356✔
225

226
  token.nulStr();
160,356✔
227

228
  // FIXME: USED to set lval to 0 for TYP_FUNC.  Not sure if needed anymore.
229
  // FIXME: Doesn't seem to be, but must be sure before removal
230
  switch ( st.id )
160,356✔
231
  {
232
  case INS_CASEJMP:
95✔
233
    if ( st.offset >= symbols.length() )
95✔
234
    {
235
      throw std::runtime_error(
×
236
          "Symbol offset of " + Clib::tostring( st.offset ) + " exceeds symbol store length of " +
×
237
          Clib::tostring( symbols.length() ) + " at PC=" + Clib::tostring( position ) );
×
238
    }
239
    token.dataptr = reinterpret_cast<const unsigned char*>( symbols.array() + st.offset );
95✔
240
    return 0;
95✔
241
  case TOK_LONG:
13,609✔
242
    if ( st.offset >= symbols.length() )
13,609✔
243
    {
244
      throw std::runtime_error(
×
245
          "Symbol offset of " + Clib::tostring( st.offset ) + " exceeds symbol store length of " +
×
246
          Clib::tostring( symbols.length() ) + " at PC=" + Clib::tostring( position ) );
×
247
    }
248
    std::memcpy( &token.lval, symbols.array() + st.offset, sizeof( int ) );
13,609✔
249
    return 0;
13,609✔
250
  case TOK_DOUBLE:
424✔
251
    if ( st.offset >= symbols.length() )
424✔
252
    {
253
      throw std::runtime_error(
×
254
          "Symbol offset of " + Clib::tostring( st.offset ) + " exceeds symbol store length of " +
×
255
          Clib::tostring( symbols.length() ) + " at PC=" + Clib::tostring( position ) );
×
256
    }
257
    std::memcpy( &token.dval, symbols.array() + st.offset, sizeof( double ) );
424✔
258
    return 0;
424✔
259

260
  case CTRL_STATEMENTBEGIN:
×
261
    if ( st.offset )
×
262
    {
263
      if ( st.offset >= symbols.length() )
×
264
      {
265
        throw std::runtime_error(
×
266
            "Symbol offset of " + Clib::tostring( st.offset ) + " exceeds symbol store length of " +
×
267
            Clib::tostring( symbols.length() ) + " at PC=" + Clib::tostring( position ) );
×
268
      }
269
      DebugToken* dt = (DebugToken*)( symbols.array() + st.offset );
×
270
      token.sourceFile = dt->sourceFile;
×
271
      token.lval = dt->offset;
×
272

273
      if ( dt->strOffset >= symbols.length() )
×
274
      {
275
        throw std::runtime_error( "Symbol offset of " + Clib::tostring( dt->strOffset ) +
×
276
                                  " exceeds symbol store length of " +
×
277
                                  Clib::tostring( symbols.length() ) +
×
278
                                  " at PC=" + Clib::tostring( position ) );
×
279
      }
280
      if ( dt->strOffset )
×
281
        token.setStr( symbols.array() + dt->strOffset );
×
282
    }
283
    return 0;
×
284

285
  case INS_INITFOREACH:
52,723✔
286
  case INS_STEPFOREACH:
287
  case INS_INITFOR:
288
  case INS_NEXTFOR:
289
  case TOK_GLOBALVAR:
290
  case TOK_LOCALVAR:
291
  case CTRL_JSR_USERFUNC:
292
  case CTRL_LEAVE_BLOCK:
293
  case TOK_ARRAY_SUBSCRIPT:
294
  case RSV_JMPIFFALSE:
295
  case RSV_JMPIFTRUE:
296
  case RSV_GOTO:
297
  case RSV_LOCAL:
298
  case RSV_GLOBAL:
299
  case INS_ASSIGN_GLOBALVAR:
300
  case INS_ASSIGN_LOCALVAR:
301
  case INS_GET_MEMBER_ID:
302
  case INS_SET_MEMBER_ID:
303
  case INS_SET_MEMBER_ID_CONSUME:
304
  case INS_CALL_METHOD_ID:
305
  case INS_CHECK_MRO:
306
  case INS_SET_MEMBER_ID_CONSUME_PLUSEQUAL:
307
  case INS_SET_MEMBER_ID_CONSUME_MINUSEQUAL:
308
  case INS_SET_MEMBER_ID_CONSUME_TIMESEQUAL:
309
  case INS_SET_MEMBER_ID_CONSUME_DIVIDEEQUAL:
310
  case INS_SET_MEMBER_ID_CONSUME_MODULUSEQUAL:
311
  case INS_SET_MEMBER_ID_UNPLUSPLUS:
312
  case INS_SET_MEMBER_ID_UNMINUSMINUS:
313
  case INS_SET_MEMBER_ID_UNPLUSPLUS_POST:
314
  case INS_SET_MEMBER_ID_UNMINUSMINUS_POST:
315
  case INS_SKIPIFTRUE_ELSE_CONSUME:
316
  case TOK_BOOL:
317
  case TOK_INTERPOLATE_STRING:
318
  case TOK_FUNCREF:
319
  case TOK_FUNCTOR:
320
  case TOK_SPREAD:
321
  case INS_TAKE_GLOBAL:
322
  case INS_TAKE_LOCAL:
323
  case INS_UNPACK_SEQUENCE:
324
  case INS_UNPACK_INDICES:
325
  case INS_LOGICAL_JUMP:
326
  case INS_LOGICAL_CONVERT:
327
    token.lval = st.offset;
52,723✔
328
    return 0;
52,723✔
329

330
  case INS_CALL_METHOD:
12,405✔
331
  case TOK_FUNC:
332
  case TOK_USERFUNC:
333
    token.lval = st.type;
12,405✔
334
    token.type = ( st.id == TOK_FUNC ) ? TYP_FUNC : TYP_USERFUNC;
12,405✔
335
    if ( st.offset )
12,405✔
336
    {
337
      if ( st.offset >= symbols.length() )
364✔
338
      {
339
        throw std::runtime_error(
×
340
            "Symbol offset of " + Clib::tostring( st.offset ) + " exceeds symbol store length of " +
×
341
            Clib::tostring( symbols.length() ) + " at PC=" + Clib::tostring( position ) );
×
342
      }
343
      token.setStr( symbols.array() + st.offset );
364✔
344
    }
345
    else if ( token.type == TYP_FUNC )
12,041✔
346
    {
347
      FunctionalityModule* modl = modules[token.module];
12,041✔
348
      const ModuleFunction* modfunc;
349

350
      // executor only:
351
      modfunc = modl->functions.at( token.lval );
12,041✔
352

353
      token.setStr( modfunc->name.get().c_str() );
12,041✔
354
    }
355
    return 0;
12,405✔
356

357
  default:
81,100✔
358
    if ( st.offset )
81,100✔
359
    {
360
      if ( st.offset >= symbols.length() )
32,076✔
361
      {
362
        throw std::runtime_error(
×
363
            "Symbol offset of " + Clib::tostring( st.offset ) + " exceeds symbol store length of " +
×
364
            Clib::tostring( symbols.length() ) + " at PC=" + Clib::tostring( position ) );
×
365
      }
366
      token.setStr( symbols.array() + st.offset );
32,076✔
367
    }
368
    return 0;
81,100✔
369
  }
370
}
371

372
int EScriptProgram::read_globalvarnames( FILE* fp )
×
373
{
374
  BSCRIPT_GLOBALVARNAMES_HDR hdr;
375
  if ( fread( &hdr, sizeof hdr, 1, fp ) != 1 )
×
376
    return -1;
×
377
  int res = 0;
×
378
  unsigned bufalloc = 20;
×
379
  auto buffer = new char[bufalloc];
×
380
  for ( unsigned idx = 0; idx < hdr.nGlobalVars; ++idx )
×
381
  {
382
    BSCRIPT_GLOBALVARNAME_HDR ghdr;
383
    if ( fread( &ghdr, sizeof ghdr, 1, fp ) != 1 )
×
384
    {
385
      res = -1;
×
386
      break;
×
387
    }
388
    if ( ghdr.namelen >= bufalloc )
×
389
    {
390
      bufalloc = ghdr.namelen + 5;
×
391
      delete[] buffer;
×
392
      buffer = new char[bufalloc];
×
393
    }
394
    if ( fread( buffer, ghdr.namelen + 1, 1, fp ) != 1 )
×
395
    {
396
      res = -1;
×
397
      break;
×
398
    }
NEW
399
    globalvarnames.emplace_back( buffer );
×
400
  }
401
  delete[] buffer;
×
402
  buffer = nullptr;
×
403
  return res;
×
404
}
405

406
int EScriptProgram::read_exported_functions( FILE* fp, BSCRIPT_SECTION_HDR* hdr )
105✔
407
{
408
  BSCRIPT_EXPORTED_FUNCTION bef;
409
  ObjMethod* mth;
410

411
  unsigned nexports = hdr->length / sizeof bef;
105✔
412
  while ( nexports-- )
436✔
413
  {
414
    if ( fread( &bef, sizeof bef, 1, fp ) != 1 )
331✔
415
      return -1;
×
416
    EPExportedFunction ef;
331✔
417
    ef.name = bef.funcname;
331✔
418
    ef.nargs = bef.nargs;
331✔
419
    ef.PC = bef.PC;
331✔
420
    exported_functions.push_back( ef );
331✔
421
    if ( ( mth = getKnownObjMethod( ef.name.c_str() ) ) != nullptr )
331✔
422
      mth->overridden = true;
×
423
  }
331✔
424
  return 0;
105✔
425
}
426

427
int EScriptProgram::read_function_references( FILE* fp, BSCRIPT_SECTION_HDR* hdr )
385✔
428
{
429
  BSCRIPT_FUNCTION_REFERENCE bfr;
430
  BSCRIPT_FUNCTION_REFERENCE_DEFAULT_PARAMETER bfrdp;
431

432
  unsigned nfuncrefs = hdr->length / sizeof bfr;
385✔
433
  while ( nfuncrefs-- )
1,941✔
434
  {
435
    if ( fread( &bfr, sizeof bfr, 1, fp ) != 1 )
1,556✔
436
      return -1;
×
437
    EPFunctionReference fr;
1,556✔
438
    fr.address = bfr.address;
1,556✔
439
    fr.parameter_count = bfr.parameter_count;
1,556✔
440
    fr.capture_count = bfr.capture_count;
1,556✔
441
    fr.is_variadic = bfr.is_variadic;
1,556✔
442
    fr.class_index = bfr.class_index;
1,556✔
443
    fr.is_constructor = bfr.is_constructor;
1,556✔
444

445
    auto default_parameter_count = bfr.default_parameter_count;
1,556✔
446

447
    while ( default_parameter_count-- )
1,743✔
448
    {
449
      if ( fread( &bfrdp, sizeof bfrdp, 1, fp ) != 1 )
187✔
450
        return -1;
×
451

452
      fr.default_parameter_addresses.push_back( bfrdp.address );
187✔
453
    }
454

455
    function_references.push_back( fr );
1,556✔
456
  }
1,556✔
457
  return 0;
385✔
458
}
459

460
int EScriptProgram::read_class_table( FILE* fp )
206✔
461
{
462
  BSCRIPT_CLASS_TABLE bct;
463
  if ( fread( &bct, sizeof bct, 1, fp ) != 1 )
206✔
464
    return -1;
×
465

466
  // For each class...
467
  while ( bct.class_count-- )
713✔
468
  {
469
    // Handle class
470
    BSCRIPT_CLASS_TABLE_ENTRY bcte;
471
    if ( fread( &bcte, sizeof bcte, 1, fp ) != 1 )
507✔
472
      return -1;
×
473

474
    // Handle constructors
475
    EPConstructorList constructors;
507✔
476
    constructors.reserve( bcte.constructor_count );
507✔
477

478
    while ( bcte.constructor_count-- )
1,206✔
479
    {
480
      BSCRIPT_CLASS_TABLE_CONSTRUCTOR_ENTRY bctce;
481
      if ( fread( &bctce, sizeof bctce, 1, fp ) != 1 )
699✔
482
        return -1;
×
483
      constructors.push_back( EPConstructorDescriptor{ bctce.type_tag_offset } );
699✔
484
    }
485

486
    // Handle methods
487
    EPMethodMap methods;
507✔
488
    while ( bcte.method_count-- )
828✔
489
    {
490
      BSCRIPT_CLASS_TABLE_METHOD_ENTRY bctme;
491
      if ( fread( &bctme, sizeof bctme, 1, fp ) != 1 )
321✔
492
        return -1;
×
493

494
      methods[bctme.name_offset] = EPMethodDescriptor{ bctme.function_reference_index };
321✔
495
    }
496

497
    class_descriptors.push_back(
1,014✔
498
        EPClassDescriptor{ bcte.name_offset, bcte.constructor_function_reference_index,
1,014✔
499
                           std::move( constructors ), std::move( methods ) } );
1,014✔
500
  }
507✔
501
  return 0;
206✔
502
}
503

504
int EScriptProgram::read_dbg_file( bool quiet )
47✔
505
{
506
  if ( debug_loaded )
47✔
507
    return 0;
38✔
508

509
  std::string mname = name;
9✔
510
  mname.replace( mname.size() - 3, 3, "dbg" );
9✔
511
  FILE* fp = fopen( mname.c_str(), "rb" );
9✔
512
  if ( !fp )
9✔
513
  {
514
    if ( !quiet )
2✔
515
      ERROR_PRINTLN( "Unable to open {}", mname );
×
516
    return -1;
2✔
517
  }
518

519
  u32 dbgversion;
520
  size_t fread_res = fread( &dbgversion, sizeof dbgversion, 1, fp );
7✔
521
  if ( fread_res != 1 || ( dbgversion != 2 && dbgversion != 3 ) )
7✔
522
  {
523
    ERROR_PRINTLN( "Recompile required. Bad version {} in {}, expected version 2", dbgversion,
×
524
                   mname );
525
    fclose( fp );
×
526
    return -1;
×
527
  }
528

529
  size_t bufalloc = 20;
7✔
530
  auto buffer = std::unique_ptr<char[]>( new char[bufalloc] );
7✔
531
  int res = 0;
7✔
532

533
  u32 count;
534
  fread_res = fread( &count, sizeof count, 1, fp );
7✔
535
  if ( fread_res != 1 )
7✔
536
  {
537
    fclose( fp );
×
538
    return -1;
×
539
  }
540
  dbg_filenames.resize( count );
7✔
541
  for ( auto& elem : dbg_filenames )
83✔
542
  {
543
    fread_res = fread( &count, sizeof count, 1, fp );
76✔
544
    if ( fread_res != 1 )
76✔
545
    {
546
      fclose( fp );
×
547
      return -1;
×
548
    }
549
    if ( count >= bufalloc )
76✔
550
    {
551
      bufalloc = count * 2;
7✔
552
      buffer.reset( new char[bufalloc] );
7✔
553
    }
554
    fread_res = fread( buffer.get(), count, 1, fp );
76✔
555
    if ( fread_res != 1 )
76✔
556
    {
557
      fclose( fp );
×
558
      return -1;
×
559
    }
560
    elem = buffer.get();
76✔
561
  }
562

563
  fread_res = fread( &count, sizeof count, 1, fp );
7✔
564
  if ( fread_res != 1 )
7✔
565
  {
566
    fclose( fp );
×
567
    return -1;
×
568
  }
569
  globalvarnames.resize( count );
7✔
570
  for ( auto& elem : globalvarnames )
29✔
571
  {
572
    fread_res = fread( &count, sizeof count, 1, fp );
22✔
573
    if ( fread_res != 1 )
22✔
574
    {
575
      fclose( fp );
×
576
      return -1;
×
577
    }
578
    if ( count >= bufalloc )
22✔
579
    {
580
      bufalloc = count * 2;
×
581
      buffer.reset( new char[bufalloc] );
×
582
    }
583
    fread_res = fread( buffer.get(), count, 1, fp );
22✔
584
    if ( fread_res != 1 )
22✔
585
    {
586
      fclose( fp );
×
587
      return -1;
×
588
    }
589
    elem = buffer.get();
22✔
590
  }
591

592
  fread_res = fread( &count, sizeof count, 1, fp );
7✔
593
  if ( fread_res != 1 )
7✔
594
  {
595
    fclose( fp );
×
596
    return -1;
×
597
  }
598
  dbg_filenum.resize( count );
7✔
599
  dbg_linenum.resize( count );
7✔
600
  dbg_ins_blocks.resize( count );
7✔
601
  dbg_ins_statementbegin.resize( count );
7✔
602
  for ( unsigned i = 0; i < tokens.count(); ++i )
8,836✔
603
  {
604
    BSCRIPT_DBG_INSTRUCTION ins;
605
    fread_res = fread( &ins, sizeof ins, 1, fp );
8,829✔
606
    if ( fread_res != 1 )
8,829✔
607
    {
608
      fclose( fp );
×
609
      return -1;
×
610
    }
611
    dbg_filenum[i] = ins.filenum;
8,829✔
612
    dbg_linenum[i] = ins.linenum;
8,829✔
613
    dbg_ins_blocks[i] = ins.blocknum;
8,829✔
614
    dbg_ins_statementbegin[i] = ins.statementbegin ? true : false;
8,829✔
615
  }
616
  fread_res = fread( &count, sizeof count, 1, fp );
7✔
617
  if ( fread_res != 1 )
7✔
618
  {
619
    fclose( fp );
×
620
    return -1;
×
621
  }
622
  blocks.resize( count );
7✔
623
  for ( auto& block : blocks )
190✔
624
  {
625
    u32 tmp;
626

627
    fread_res = fread( &tmp, sizeof tmp, 1, fp );
183✔
628
    if ( fread_res != 1 )
183✔
629
    {
630
      fclose( fp );
×
631
      return -1;
×
632
    }
633
    block.parentblockidx = tmp;
183✔
634

635
    fread_res = fread( &tmp, sizeof tmp, 1, fp );
183✔
636
    if ( fread_res != 1 )
183✔
637
    {
638
      fclose( fp );
×
639
      return -1;
×
640
    }
641
    block.parentvariables = tmp;
183✔
642

643
    fread_res = fread( &tmp, sizeof tmp, 1, fp );
183✔
644
    if ( fread_res != 1 )
183✔
645
    {
646
      fclose( fp );
×
647
      return -1;
×
648
    }
649
    block.localvarnames.resize( tmp );
183✔
650

651
    for ( auto& elem : block.localvarnames )
684✔
652
    {
653
      fread_res = fread( &count, sizeof count, 1, fp );
501✔
654
      if ( fread_res != 1 )
501✔
655
      {
656
        fclose( fp );
×
657
        return -1;
×
658
      }
659
      if ( count >= bufalloc )
501✔
660
      {
661
        bufalloc = count * 2;
×
662
        buffer.reset( new char[bufalloc] );
×
663
      }
664
      fread_res = fread( buffer.get(), count, 1, fp );
501✔
665
      if ( fread_res != 1 )
501✔
666
      {
667
        fclose( fp );
×
668
        return -1;
×
669
      }
670
      elem = buffer.get();
501✔
671
    }
672
  }
673
  if ( dbgversion >= 3 )
7✔
674
  {
675
    fread_res = fread( &count, sizeof count, 1, fp );
7✔
676
    if ( fread_res != 1 )
7✔
677
    {
678
      fclose( fp );
×
679
      return -1;
×
680
    }
681
    dbg_functions.resize( count );
7✔
682
    for ( auto& func : dbg_functions )
122✔
683
    {
684
      u32 tmp;
685
      fread_res = fread( &tmp, sizeof tmp, 1, fp );
115✔
686
      if ( fread_res != 1 )
115✔
687
      {
688
        fclose( fp );
×
689
        return -1;
×
690
      }
691
      if ( tmp >= bufalloc )
115✔
692
      {
693
        bufalloc = tmp * 2;
×
694
        buffer.reset( new char[bufalloc] );
×
695
      }
696
      fread_res = fread( buffer.get(), tmp, 1, fp );
115✔
697
      if ( fread_res != 1 )
115✔
698
      {
699
        fclose( fp );
×
700
        return -1;
×
701
      }
702
      func.name = buffer.get();
115✔
703

704
      fread_res = fread( &tmp, sizeof tmp, 1, fp );
115✔
705
      if ( fread_res != 1 )
115✔
706
      {
707
        fclose( fp );
×
708
        return -1;
×
709
      }
710
      func.firstPC = tmp;
115✔
711
      fread_res = fread( &tmp, sizeof tmp, 1, fp );
115✔
712
      if ( fread_res != 1 )
115✔
713
      {
714
        fclose( fp );
×
715
        return -1;
×
716
      }
717
      func.lastPC = tmp;
115✔
718
    }
719
  }
720

721
  fclose( fp );
7✔
722
  debug_loaded = true;
7✔
723
  return res;
7✔
724
}
9✔
725
}  // namespace Bscript
726
}  // namespace Pol
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc