Coverage Report

Created: 2026-12-01 07:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libnk2/libcerror/libcerror_error.c
Line
Count
Source
1
/*
2
 * Error functions
3
 *
4
 * Copyright (C) 2008-2026, Joachim Metz <joachim.metz@gmail.com>
5
 *
6
 * Refer to AUTHORS for acknowledgements.
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Lesser General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public License
19
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20
 */
21
22
#include <common.h>
23
#include <memory.h>
24
#include <narrow_string.h>
25
#include <system_string.h>
26
#include <types.h>
27
#include <wide_string.h>
28
29
#if defined( HAVE_STDARG_H ) || defined( WINAPI )
30
#include <stdarg.h>
31
#elif defined( HAVE_VARARGS_H )
32
#include <varargs.h>
33
#else
34
#error Missing headers stdarg.h and varargs.h
35
#endif
36
37
#include <errno.h>
38
39
#include "libcerror_definitions.h"
40
#include "libcerror_error.h"
41
#include "libcerror_types.h"
42
43
/* Creates an error
44
 * Returns 1 if successful or -1 on error
45
 */
46
int libcerror_error_initialize(
47
     libcerror_error_t **error,
48
     int error_domain,
49
     int error_code )
50
4.21k
{
51
4.21k
  libcerror_internal_error_t *internal_error = NULL;
52
53
4.21k
  if( error == NULL )
54
0
  {
55
0
    return( -1 );
56
0
  }
57
4.21k
  if( *error != NULL )
58
0
  {
59
0
    return( -1 );
60
0
  }
61
4.21k
  internal_error = memory_allocate_structure(
62
4.21k
                    libcerror_internal_error_t );
63
64
4.21k
  if( internal_error == NULL )
65
0
  {
66
0
    return( -1 );
67
0
  }
68
4.21k
  internal_error->domain             = error_domain;
69
4.21k
  internal_error->code               = error_code;
70
4.21k
  internal_error->number_of_messages = 0;
71
4.21k
  internal_error->messages           = NULL;
72
4.21k
  internal_error->sizes              = NULL;
73
74
4.21k
  *error = (libcerror_error_t *) internal_error;
75
76
4.21k
  return( 1 );
77
4.21k
}
78
79
/* Free an error and its elements
80
 */
81
void libcerror_error_free(
82
      libcerror_error_t **error )
83
50.2M
{
84
50.2M
  libcerror_internal_error_t *internal_error = NULL;
85
50.2M
  int message_index                          = 0;
86
87
50.2M
  if( error == NULL )
88
50.2M
  {
89
50.2M
    return;
90
50.2M
  }
91
5.21k
  if( *error != NULL )
92
4.21k
  {
93
4.21k
    internal_error = (libcerror_internal_error_t *) *error;
94
95
4.21k
    if( internal_error->messages != NULL )
96
4.21k
    {
97
4.21k
      for( message_index = 0;
98
18.9k
           message_index < internal_error->number_of_messages;
99
14.7k
           message_index++ )
100
14.7k
      {
101
14.7k
        if( internal_error->messages[ message_index ] != NULL )
102
14.7k
        {
103
14.7k
          memory_free(
104
14.7k
           internal_error->messages[ message_index ] );
105
14.7k
        }
106
14.7k
      }
107
4.21k
      memory_free(
108
4.21k
       internal_error->messages );
109
4.21k
    }
110
4.21k
    if( internal_error->sizes != NULL )
111
4.21k
    {
112
4.21k
      memory_free(
113
4.21k
       internal_error->sizes );
114
4.21k
    }
115
4.21k
    memory_free(
116
4.21k
     *error );
117
118
4.21k
    *error = NULL;
119
4.21k
  }
120
5.21k
}
121
122
/* Resizes an error
123
 * Returns 1 if successful or -1 on error
124
 */
125
int libcerror_error_resize(
126
     libcerror_internal_error_t *internal_error )
127
14.7k
{
128
14.7k
  void *reallocation     = NULL;
129
14.7k
  int message_index      = 0;
130
14.7k
  int number_of_messages = 0;
131
132
14.7k
  if( internal_error == NULL )
133
0
  {
134
0
    return( -1 );
135
0
  }
136
14.7k
  message_index      = internal_error->number_of_messages;
137
14.7k
  number_of_messages = internal_error->number_of_messages + 1;
138
139
14.7k
  reallocation = memory_reallocate(
140
14.7k
                  internal_error->messages,
141
14.7k
                  sizeof( system_character_t * ) * number_of_messages );
142
143
14.7k
  if( reallocation == NULL )
144
0
  {
145
0
    return( -1 );
146
0
  }
147
14.7k
  internal_error->messages = (system_character_t **) reallocation;
148
149
14.7k
  internal_error->messages[ message_index ] = NULL;
150
151
14.7k
  reallocation = memory_reallocate(
152
14.7k
                  internal_error->sizes,
153
14.7k
                  sizeof( size_t ) * number_of_messages );
154
155
14.7k
  if( reallocation == NULL )
156
0
  {
157
0
    return( -1 );
158
0
  }
159
14.7k
  internal_error->sizes = (size_t *) reallocation;
160
161
14.7k
  internal_error->sizes[ message_index ] = 0;
162
163
14.7k
  internal_error->number_of_messages += 1;
164
165
14.7k
  return( 1 );
166
14.7k
}
167
168
#if defined( HAVE_WIDE_SYSTEM_CHARACTER )
169
170
/* Retrieves the format string as a system string
171
 */
172
void libcerror_error_get_system_format_string(
173
      const char *format_string,
174
      size_t format_string_length,
175
      system_character_t **system_format_string )
176
{
177
  void *reallocation               = NULL;
178
  size_t next_format_string_length = 0;
179
  int print_count                  = 0;
180
181
#if defined( __BORLANDC__ ) || defined( _MSC_VER )
182
  size_t string_index              = 0;
183
#endif
184
185
  if( format_string == NULL )
186
  {
187
    return;
188
  }
189
  if( format_string_length > (size_t) SSIZE_MAX )
190
  {
191
    return;
192
  }
193
  if( system_format_string == NULL )
194
  {
195
    return;
196
  }
197
  next_format_string_length = format_string_length + 1;
198
199
  do
200
  {
201
    if( next_format_string_length >= LIBCERROR_MESSAGE_MAXIMUM_SIZE )
202
    {
203
      next_format_string_length = LIBCERROR_MESSAGE_MAXIMUM_SIZE;
204
    }
205
    reallocation = memory_reallocate(
206
                    *system_format_string,
207
                    sizeof( system_character_t ) * next_format_string_length );
208
209
    if( reallocation == NULL )
210
    {
211
      memory_free(
212
       *system_format_string );
213
214
      *system_format_string = NULL;
215
216
      return;
217
    }
218
    *system_format_string = (system_character_t *) reallocation;
219
220
    format_string_length = next_format_string_length;
221
222
#if defined( __BORLANDC__ ) || defined( _MSC_VER )
223
    print_count = wide_string_snwprintf(
224
                   *system_format_string,
225
                   format_string_length,
226
                   L"%S",
227
                   format_string );
228
#else
229
    print_count = wide_string_snwprintf(
230
                   *system_format_string,
231
                   format_string_length,
232
                   L"%s",
233
                   format_string );
234
#endif
235
    if( print_count <= -1 )
236
    {
237
      next_format_string_length += LIBCERROR_MESSAGE_INCREMENT_SIZE;
238
    }
239
    else if( ( (size_t) print_count > format_string_length )
240
          || ( ( *system_format_string )[ print_count ] != 0 ) )
241
    {
242
      next_format_string_length = (size_t) print_count;
243
      print_count               = -1;
244
    }
245
    if( next_format_string_length >= LIBCERROR_MESSAGE_MAXIMUM_SIZE )
246
    {
247
/* TODO handle similar to error string */
248
      memory_free(
249
       *system_format_string );
250
251
      *system_format_string = NULL;
252
253
      return;
254
    }
255
  }
256
  while( print_count <= -1 );
257
258
#if defined( __BORLANDC__ ) || defined( _MSC_VER )
259
  /* Rewrite %s to %S
260
   */
261
  string_index = 0;
262
263
  while( string_index < format_string_length )
264
  {
265
    if( ( *system_format_string )[ string_index ] == 0 )
266
    {
267
      break;
268
    }
269
    else if( ( *system_format_string )[ string_index ] == (system_character_t) '%' )
270
    {
271
      string_index++;
272
273
      if( ( *system_format_string )[ string_index ] == (system_character_t) 's' )
274
      {
275
         ( *system_format_string )[ string_index ] = (system_character_t) 'S';
276
      }
277
    }
278
    string_index++;
279
  }
280
#endif /* defined( __BORLANDC__ ) || defined( _MSC_VER ) */
281
}
282
283
#endif /* defined( HAVE_WIDE_SYSTEM_CHARACTER ) */
284
285
#if defined( HAVE_STDARG_H ) || defined( WINAPI )
286
#define VARARGS( function, error, error_domain, error_code, type, argument ) \
287
        function( error, error_domain, error_code, type argument, ... )
288
#define VASTART( argument_list, type, name ) \
289
26.7k
        va_start( argument_list, name )
290
#define VAEND( argument_list ) \
291
26.7k
        va_end( argument_list )
292
293
#elif defined( HAVE_VARARGS_H )
294
#define VARARGS( function, error, error_domain, error_code, type, argument ) \
295
        function( error, error_domain, error_code, va_alist ) va_dcl
296
#define VASTART( argument_list, type, name ) \
297
        { type name; va_start( argument_list ); name = va_arg( argument_list, type )
298
#define VAEND( argument_list ) \
299
        va_end( argument_list ); }
300
301
#endif /* defined( HAVE_STDARG_H ) || defined( WINAPI ) */
302
303
/* Sets an error
304
 * Creates the error if necessary
305
 * The error domain and code are set only the first time and the error message is appended for back tracing
306
 */
307
void VARARGS(
308
      libcerror_error_set,
309
      libcerror_error_t **error,
310
      int error_domain,
311
      int error_code,
312
      const char *,
313
      format_string )
314
214M
{
315
214M
  va_list argument_list;
316
317
214M
  libcerror_internal_error_t *internal_error = NULL;
318
214M
  system_character_t *error_string           = NULL;
319
214M
  system_character_t *system_format_string   = NULL;
320
214M
  void *reallocation                         = NULL;
321
214M
  size_t error_string_size                   = 0;
322
214M
  size_t format_string_length                = 0;
323
214M
  size_t message_size                        = 0;
324
214M
  size_t next_message_size                   = LIBCERROR_MESSAGE_INCREMENT_SIZE;
325
214M
  int message_index                          = 0;
326
214M
  int print_count                            = 0;
327
328
214M
  if( error == NULL )
329
214M
  {
330
214M
    return;
331
214M
  }
332
14.7k
  if( format_string == NULL )
333
0
  {
334
0
    return;
335
0
  }
336
14.7k
  format_string_length = narrow_string_length(
337
14.7k
                          format_string );
338
339
#if defined( HAVE_WIDE_SYSTEM_CHARACTER )
340
  libcerror_error_get_system_format_string(
341
   format_string,
342
   format_string_length,
343
   &system_format_string );
344
345
  if( system_format_string == NULL )
346
  {
347
    return;
348
  }
349
#else
350
14.7k
  system_format_string = (system_character_t *) format_string;
351
14.7k
#endif
352
14.7k
  if( *error == NULL )
353
4.21k
  {
354
4.21k
    if( libcerror_error_initialize(
355
4.21k
         error,
356
4.21k
         error_domain,
357
4.21k
         error_code ) != 1 )
358
0
    {
359
0
      goto on_error;
360
0
    }
361
4.21k
  }
362
14.7k
  internal_error = (libcerror_internal_error_t *) *error;
363
364
14.7k
  if( libcerror_error_resize(
365
14.7k
       internal_error ) != 1 )
366
0
  {
367
0
    goto on_error;
368
0
  }
369
14.7k
  if( format_string_length > next_message_size )
370
104
  {
371
104
    next_message_size = ( ( format_string_length / LIBCERROR_MESSAGE_INCREMENT_SIZE ) + 1 )
372
104
                      * LIBCERROR_MESSAGE_INCREMENT_SIZE;
373
104
  }
374
14.7k
  do
375
26.7k
  {
376
26.7k
    if( next_message_size >= LIBCERROR_MESSAGE_MAXIMUM_SIZE )
377
0
    {
378
0
      next_message_size = LIBCERROR_MESSAGE_MAXIMUM_SIZE;
379
0
    }
380
26.7k
    reallocation = memory_reallocate(
381
26.7k
                    error_string,
382
26.7k
                    sizeof( system_character_t ) * next_message_size );
383
384
26.7k
    if( reallocation == NULL )
385
0
    {
386
0
      memory_free(
387
0
       error_string );
388
389
0
      goto on_error;
390
0
    }
391
26.7k
    error_string = (system_character_t *) reallocation;
392
393
26.7k
    message_size = next_message_size;
394
395
    /* argument_list cannot be reused in successive calls to vsnprintf
396
     */
397
26.7k
    VASTART(
398
26.7k
     argument_list,
399
26.7k
     const char *,
400
26.7k
     format_string );
401
402
26.7k
    print_count = system_string_vsnprintf(
403
26.7k
                   error_string,
404
26.7k
                   message_size,
405
26.7k
                   system_format_string,
406
26.7k
                   argument_list );
407
408
26.7k
    VAEND(
409
26.7k
     argument_list );
410
411
26.7k
    if( print_count <= -1 )
412
0
    {
413
0
      next_message_size += LIBCERROR_MESSAGE_INCREMENT_SIZE;
414
0
    }
415
26.7k
    else if( ( (size_t) print_count >= message_size )
416
14.7k
          || ( error_string[ print_count ] != (system_character_t) 0 ) )
417
12.0k
    {
418
12.0k
      next_message_size = (size_t) ( print_count + 1 );
419
12.0k
      print_count       = -1;
420
12.0k
    }
421
14.7k
    else
422
14.7k
    {
423
14.7k
      error_string_size = (size_t) print_count + 1;
424
14.7k
    }
425
26.7k
    if( message_size >= LIBCERROR_MESSAGE_MAXIMUM_SIZE )
426
0
    {
427
0
      break;
428
0
    }
429
26.7k
  }
430
26.7k
  while( print_count <= -1 );
431
432
14.7k
  if( message_size >= LIBCERROR_MESSAGE_MAXIMUM_SIZE )
433
0
  {
434
0
    error_string[ LIBCERROR_MESSAGE_MAXIMUM_SIZE - 4 ] = (system_character_t) '.';
435
0
    error_string[ LIBCERROR_MESSAGE_MAXIMUM_SIZE - 3 ] = (system_character_t) '.';
436
0
    error_string[ LIBCERROR_MESSAGE_MAXIMUM_SIZE - 2 ] = (system_character_t) '.';
437
0
    error_string[ LIBCERROR_MESSAGE_MAXIMUM_SIZE - 1 ] = 0;
438
0
    error_string_size                                  = (size_t) LIBCERROR_MESSAGE_MAXIMUM_SIZE;
439
0
  }
440
14.7k
  message_index = internal_error->number_of_messages - 1;
441
442
14.7k
  internal_error->messages[ message_index ] = error_string;
443
14.7k
  internal_error->sizes[ message_index ]    = error_string_size;
444
445
#if defined( HAVE_WIDE_SYSTEM_CHARACTER )
446
  memory_free(
447
   system_format_string );
448
449
  system_format_string = NULL;
450
#endif
451
14.7k
  return;
452
453
0
on_error:
454
#if defined( HAVE_WIDE_SYSTEM_CHARACTER )
455
  if( system_format_string != NULL )
456
  {
457
    memory_free(
458
     system_format_string );
459
  }
460
#endif
461
0
  return;
462
14.7k
}
463
464
#undef VARARGS
465
#undef VASTART
466
#undef VAEND
467
468
/* Determines if an error equals a certain error code of a domain
469
 * Returns 1 if error matches or 0 if not
470
 */
471
int libcerror_error_matches(
472
     libcerror_error_t *error,
473
     int error_domain,
474
     int error_code )
475
0
{
476
0
  if( error == NULL )
477
0
  {
478
0
    return( 0 );
479
0
  }
480
0
  if( ( ( (libcerror_internal_error_t *) error )->domain == error_domain )
481
0
   && ( ( (libcerror_internal_error_t *) error )->code == error_code ) )
482
0
  {
483
0
    return( 1 );
484
0
  }
485
0
  return( 0 );
486
0
}
487
488
/* Prints a descriptive string of the error to the stream
489
 * Returns the number of printed characters if successful or -1 on error
490
 */
491
int libcerror_error_fprint(
492
     libcerror_error_t *error,
493
     FILE *stream )
494
365
{
495
365
  libcerror_internal_error_t *internal_error = NULL;
496
365
  system_character_t *error_string           = NULL;
497
365
  int message_index                          = 0;
498
365
  int print_count                            = 0;
499
500
#if defined( WINAPI ) || defined( __MINGW32__ )
501
  const char *format_string                  = "%" PRIs_SYSTEM "\r\n";
502
#else
503
365
  const char *format_string                  = "%" PRIs_SYSTEM "\n";
504
365
#endif
505
506
365
  if( error == NULL )
507
0
  {
508
0
    return( -1 );
509
0
  }
510
365
  internal_error = (libcerror_internal_error_t *) error;
511
512
365
  if( internal_error->messages == NULL )
513
0
  {
514
0
    return( -1 );
515
0
  }
516
365
  if( stream == NULL )
517
205
  {
518
205
    return( -1 );
519
205
  }
520
160
  message_index = internal_error->number_of_messages - 1;
521
160
  error_string  = internal_error->messages[ message_index ];
522
523
160
  if( error_string != NULL )
524
160
  {
525
160
    print_count = fprintf(
526
160
                   stream,
527
160
                   format_string,
528
160
                   error_string );
529
530
160
    if( print_count <= -1 )
531
0
    {
532
0
      return( -1 );
533
0
    }
534
160
  }
535
160
  return( print_count );
536
160
}
537
538
/* Prints a descriptive string of the error to the string
539
 * The end-of-string character is not included in the return value
540
 * Returns the number of printed characters if successful or -1 on error
541
 */
542
int libcerror_error_sprint(
543
     libcerror_error_t *error,
544
     char *string,
545
     size_t size )
546
1.65k
{
547
1.65k
  libcerror_internal_error_t *internal_error = NULL;
548
1.65k
  system_character_t *error_string           = NULL;
549
1.65k
  size_t message_index                       = 0;
550
1.65k
  size_t print_count                         = 0;
551
552
1.65k
#if !defined( HAVE_WIDE_SYSTEM_CHARACTER )
553
1.65k
  size_t error_string_size                   = 0;
554
1.65k
#endif
555
556
1.65k
  if( error == NULL )
557
0
  {
558
0
    return( -1 );
559
0
  }
560
1.65k
  internal_error = (libcerror_internal_error_t *) error;
561
562
1.65k
  if( internal_error->messages == NULL )
563
0
  {
564
0
    return( -1 );
565
0
  }
566
1.65k
  if( internal_error->sizes == NULL )
567
0
  {
568
0
    return( -1 );
569
0
  }
570
1.65k
  if( string == NULL )
571
0
  {
572
0
    return( -1 );
573
0
  }
574
1.65k
#if INT_MAX < SSIZE_MAX
575
1.65k
  if( size > (size_t) INT_MAX )
576
#else
577
  if( size > (size_t) SSIZE_MAX )
578
#endif
579
0
  {
580
0
    return( -1 );
581
0
  }
582
1.65k
  message_index = internal_error->number_of_messages - 1;
583
1.65k
  error_string  = internal_error->messages[ message_index ];
584
585
1.65k
  if( error_string != NULL )
586
1.65k
  {
587
#if defined( HAVE_WIDE_SYSTEM_CHARACTER )
588
#if defined( _MSC_VER )
589
    if( wcstombs_s(
590
         &print_count,
591
         string,
592
         size,
593
         error_string,
594
         _TRUNCATE ) != 0 )
595
    {
596
      return( -1 );
597
    }
598
#else
599
    print_count = wcstombs(
600
                   string,
601
                   error_string,
602
                   size );
603
604
    if( print_count == (size_t) -1 )
605
    {
606
      return( -1 );
607
    }
608
#endif /* defined( _MSC_VER ) */
609
610
    if( print_count >= size )
611
    {
612
      return( -1 );
613
    }
614
#else
615
1.65k
    error_string_size = internal_error->sizes[ message_index ];
616
617
1.65k
    if( size < ( error_string_size + 1 ) )
618
0
    {
619
0
      return( -1 );
620
0
    }
621
1.65k
    if( narrow_string_copy(
622
1.65k
         string,
623
1.65k
         error_string,
624
1.65k
         error_string_size ) == NULL )
625
0
    {
626
0
      return( -1 );
627
0
    }
628
1.65k
    print_count = error_string_size;
629
630
1.65k
#endif /* defined( HAVE_WIDE_SYSTEM_CHARACTER ) */
631
1.65k
  }
632
1.65k
  if( print_count > (size_t) INT_MAX )
633
0
  {
634
0
    return( -1 );
635
0
  }
636
1.65k
  return( (int) print_count );
637
1.65k
}
638
639
/* Prints a backtrace of the error to the stream
640
 * Returns the number of printed characters if successful or -1 on error
641
 */
642
int libcerror_error_backtrace_fprint(
643
     libcerror_error_t *error,
644
     FILE *stream )
645
365
{
646
365
  libcerror_internal_error_t *internal_error = NULL;
647
365
  system_character_t *error_string           = NULL;
648
365
  int message_index                          = 0;
649
365
  int print_count                            = 0;
650
365
  int total_print_count                      = 0;
651
652
#if defined( WINAPI ) || defined( __MINGW32__ )
653
  const char *format_string                  = "%" PRIs_SYSTEM "\r\n";
654
#else
655
365
  const char *format_string                  = "%" PRIs_SYSTEM "\n";
656
365
#endif
657
658
365
  if( error == NULL )
659
0
  {
660
0
    return( -1 );
661
0
  }
662
365
  internal_error = (libcerror_internal_error_t *) error;
663
664
365
  if( internal_error->messages == NULL )
665
0
  {
666
0
    return( -1 );
667
0
  }
668
365
  if( stream == NULL )
669
205
  {
670
205
    return( -1 );
671
205
  }
672
160
  for( message_index = 0;
673
320
       message_index < internal_error->number_of_messages;
674
160
       message_index++ )
675
160
  {
676
160
    error_string = internal_error->messages[ message_index ];
677
678
160
    if( error_string != NULL )
679
160
    {
680
160
      print_count = fprintf(
681
160
                     stream,
682
160
                     format_string,
683
160
                     error_string );
684
685
160
      if( print_count <= -1 )
686
0
      {
687
0
        return( -1 );
688
0
      }
689
160
      total_print_count += print_count;
690
160
    }
691
160
  }
692
160
  return( total_print_count );
693
160
}
694
695
/* Prints a backtrace of the error to the string
696
 * The end-of-string character is not included in the return value
697
 * Returns the number of printed characters if successful or -1 on error
698
 */
699
int libcerror_error_backtrace_sprint(
700
     libcerror_error_t *error,
701
     char *string,
702
     size_t size )
703
1.65k
{
704
1.65k
  libcerror_internal_error_t *internal_error = NULL;
705
1.65k
  system_character_t *error_string           = NULL;
706
1.65k
  size_t string_index                        = 0;
707
1.65k
  int message_index                          = 0;
708
709
#if defined( HAVE_WIDE_SYSTEM_CHARACTER )
710
  size_t print_count                         = 0;
711
#else
712
1.65k
  size_t error_string_size                   = 0;
713
1.65k
#endif
714
715
1.65k
  if( error == NULL )
716
0
  {
717
0
    return( -1 );
718
0
  }
719
1.65k
  internal_error = (libcerror_internal_error_t *) error;
720
721
1.65k
  if( internal_error->messages == NULL )
722
0
  {
723
0
    return( -1 );
724
0
  }
725
1.65k
  if( internal_error->sizes == NULL )
726
0
  {
727
0
    return( -1 );
728
0
  }
729
1.65k
  if( string == NULL )
730
0
  {
731
0
    return( -1 );
732
0
  }
733
1.65k
#if INT_MAX < SSIZE_MAX
734
1.65k
  if( size > (size_t) INT_MAX )
735
#else
736
  if( size > (size_t) SSIZE_MAX )
737
#endif
738
0
  {
739
0
    return( -1 );
740
0
  }
741
1.65k
  for( message_index = 0;
742
10.1k
       message_index < internal_error->number_of_messages;
743
8.53k
       message_index++ )
744
8.57k
  {
745
8.57k
    error_string = internal_error->messages[ message_index ];
746
747
8.57k
    if( error_string != NULL )
748
8.57k
    {
749
8.57k
      if( string_index > 0 )
750
6.91k
      {
751
#if defined( WINAPI ) || defined( __MINGW32__ )
752
        if( ( string_index + 2 ) >= size )
753
        {
754
          return( -1 );
755
        }
756
        string[ string_index++ ] = (system_character_t) '\r';
757
#else
758
6.91k
        if( ( string_index + 1 ) >= size )
759
0
        {
760
0
          return( -1 );
761
0
        }
762
6.91k
#endif /* defined( WINAPI ) || defined( __MINGW32__ ) */
763
764
6.91k
        string[ string_index++ ] = (system_character_t) '\n';
765
6.91k
        string[ string_index ]   = (system_character_t) 0;
766
6.91k
      }
767
#if defined( HAVE_WIDE_SYSTEM_CHARACTER )
768
#if defined( _MSC_VER )
769
      if( wcstombs_s(
770
           &print_count,
771
           &( string[ string_index ] ),
772
           size - string_index,
773
           error_string,
774
           _TRUNCATE ) != 0 )
775
      {
776
        return( -1 );
777
      }
778
#else
779
      print_count = wcstombs(
780
                     &( string[ string_index ] ),
781
                     error_string,
782
                     size - string_index );
783
784
      if( print_count == (size_t) -1 )
785
      {
786
        return( -1 );
787
      }
788
#endif /*defined( _MSC_VER ) */
789
790
      string_index += print_count;
791
792
      if( string_index >= size )
793
      {
794
        return( -1 );
795
      }
796
      if( string[ string_index - 1 ] == 0 )
797
      {
798
        string_index--;
799
      }
800
#else
801
8.57k
      error_string_size = internal_error->sizes[ message_index ];
802
803
8.57k
      if( size < ( string_index + error_string_size + 1 ) )
804
40
      {
805
40
        return( -1 );
806
40
      }
807
8.53k
      if( narrow_string_copy(
808
8.53k
           &( string[ string_index ] ),
809
8.53k
           error_string,
810
8.53k
           error_string_size ) == NULL )
811
0
      {
812
0
        return( -1 );
813
0
      }
814
8.53k
      string_index += error_string_size - 1;
815
816
8.53k
#endif /* defined( HAVE_WIDE_SYSTEM_CHARACTER ) */
817
8.53k
    }
818
8.57k
  }
819
1.61k
  string_index++;
820
821
1.61k
  if( string_index > (size_t) INT_MAX )
822
0
  {
823
0
    return( -1 );
824
0
  }
825
1.61k
  return( (int) string_index );
826
1.61k
}
827