@@ -52,9 +52,9 @@ struct snprint_param {
52
52
/** The destination buffer. */
53
53
char * dst ;
54
54
/** The size of the destination buffer. */
55
- int sz ;
55
+ uint32_t sz ;
56
56
/** Counter for written chars. */
57
- int wrtn ;
57
+ uint32_t wrtn ;
58
58
};
59
59
60
60
/** The characters to use for upper case hexadecimal conversion.
@@ -83,28 +83,22 @@ static const char lower_hex_digits[] = {
83
83
'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'x'
84
84
};
85
85
86
- static const char * get_int (const char * s , int * x )
86
+ static const char * get_param (const char * s , uint32_t * x )
87
87
{
88
- int negative = 0 ;
89
- * x = 0 ;
88
+ * x = 0U ;
90
89
91
- /* evaluate leading '-' for negative numbers */
90
+ /* ignore '-' for negative numbers, it will be handled in flags */
92
91
if (* s == '-' ) {
93
- negative = 1 ;
94
92
++ s ;
95
93
}
96
94
97
95
/* parse uint32_teger */
98
96
while ((* s >= '0' ) && (* s <= '9' )) {
99
- * x = (* x * 10 ) + (* s - '0' );
97
+ char delta = * s - '0' ;
98
+ * x = * x * 10U + (uint32_t )delta ;
100
99
s ++ ;
101
100
}
102
101
103
- /* apply sign to result */
104
- if (negative != 0 ) {
105
- * x = - * x ;
106
- }
107
-
108
102
return s ;
109
103
}
110
104
@@ -195,26 +189,21 @@ static int format_number(struct print_param *param)
195
189
/* effective width of the result */
196
190
uint32_t width ;
197
191
/* number of characters to insert for width (w) and precision (p) */
198
- uint32_t p , w ;
192
+ uint32_t p = 0U , w = 0U ;
199
193
/* the result */
200
- int res ;
194
+ int res = 0 ;
201
195
202
196
/* initialize variables */
203
- p = 0U ;
204
- w = 0U ;
205
- res = 0 ;
206
197
width = param -> vars .valuelen + param -> vars .prefixlen ;
207
198
208
199
/* calculate additional characters for precision */
209
- p = (uint32_t )(param -> vars .precision );
210
- if (p > width ) {
211
- p = p - width ;
200
+ if (param -> vars .precision > width ) {
201
+ p = param -> vars .precision - width ;
212
202
}
213
203
214
204
/* calculate additional characters for width */
215
- w = (uint32_t )(param -> vars .width );
216
- if (w > (width + p )) {
217
- w = w - (width + p );
205
+ if (param -> vars .width > (width + p )) {
206
+ w = param -> vars .width - (width + p );
218
207
}
219
208
220
209
/* handle case of right justification */
@@ -421,33 +410,25 @@ static int print_decimal(struct print_param *param, int64_t value)
421
410
static int print_string (struct print_param * param , const char * s )
422
411
{
423
412
/* the length of the string (-1) if unknown */
424
- int len ;
413
+ uint32_t len ;
425
414
/* the number of additional characters to insert to reach the required
426
415
* width
427
416
*/
428
- uint32_t w ;
417
+ uint32_t w = 0U ;
429
418
/* the last result of the emit function */
430
419
int res ;
431
420
432
- w = 0U ;
433
- len = -1 ;
434
-
435
- /* we need the length of the string if either width or precision is
436
- * given
437
- */
438
- if ((param -> vars .precision != 0 )|| (param -> vars .width != 0 )) {
439
- len = strnlen_s (s , PRINT_STRING_MAX_LEN );
440
- }
421
+ len = strnlen_s (s , PRINT_STRING_MAX_LEN );
441
422
442
423
/* precision gives the max. number of characters to emit. */
443
- if ((param -> vars .precision != 0 ) && (len > param -> vars .precision )) {
424
+ if ((param -> vars .precision != 0U ) && (len > param -> vars .precision )) {
444
425
len = param -> vars .precision ;
445
426
}
446
427
447
428
/* calculate the number of additional characters to get the required
448
429
* width
449
430
*/
450
- if (param -> vars .width > 0 && param -> vars .width > len ) {
431
+ if (param -> vars .width > 0U && param -> vars .width > len ) {
451
432
w = param -> vars .width - len ;
452
433
}
453
434
@@ -461,7 +442,6 @@ static int print_string(struct print_param *param, const char *s)
461
442
}
462
443
}
463
444
464
- /* emit the string, return early if an error occurred */
465
445
res = param -> emit (PRINT_CMD_COPY , s , len , param -> data );
466
446
if (res < 0 ) {
467
447
return res ;
@@ -527,14 +507,11 @@ int do_print(const char *fmt, struct print_param *param,
527
507
* - get the length modifier
528
508
*/
529
509
fmt = get_flags (fmt , & (param -> vars .flags ));
530
- fmt = get_int (fmt , & (param -> vars .width ));
510
+ fmt = get_param (fmt , & (param -> vars .width ));
531
511
532
512
if (* fmt == '.' ) {
533
513
fmt ++ ;
534
- fmt = get_int (fmt , & (param -> vars .precision ));
535
- if (param -> vars .precision < 0 ) {
536
- param -> vars .precision = 0 ;
537
- }
514
+ fmt = get_param (fmt , & (param -> vars .precision ));
538
515
}
539
516
540
517
fmt = get_length_modifier (fmt , & (param -> vars .flags ),
@@ -544,57 +521,61 @@ int do_print(const char *fmt, struct print_param *param,
544
521
545
522
/* a single '%'? => print out a single '%' */
546
523
if (ch == '%' ) {
547
- res = param -> emit (PRINT_CMD_COPY , & ch , 1 ,
524
+ res = param -> emit (PRINT_CMD_COPY , & ch , 1U ,
548
525
param -> data );
549
526
} else if ((ch == 'd' ) || (ch == 'i' )) {
550
527
/* decimal number */
551
- res = print_decimal ( param ,
552
- (( param -> vars . flags &
553
- PRINT_FLAG_LONG_LONG ) != 0U ) ?
554
- __builtin_va_arg (args ,
555
- long long )
556
- : ( long long )
557
- __builtin_va_arg (args ,
558
- int ));
528
+ if (( param -> vars . flags &
529
+ PRINT_FLAG_LONG_LONG ) != 0U ) {
530
+ res = print_decimal ( param ,
531
+ __builtin_va_arg (args , long ));
532
+ } else {
533
+ res = print_decimal ( param ,
534
+ __builtin_va_arg (args , int ));
535
+ }
559
536
}
560
537
/* unsigned decimal number */
561
538
else if (ch == 'u' ) {
562
539
param -> vars .flags |= PRINT_FLAG_UINT32 ;
563
- res = print_decimal (param ,
564
- ((param -> vars .flags &
565
- PRINT_FLAG_LONG_LONG ) != 0U ) ?
566
- __builtin_va_arg (args ,
567
- unsigned long long)
568
- : (unsigned long long )
569
- __builtin_va_arg (args ,
570
- unsigned int ));
540
+ if ((param -> vars .flags &
541
+ PRINT_FLAG_LONG_LONG ) != 0U ) {
542
+ res = print_decimal (param ,
543
+ (int64_t )__builtin_va_arg (args ,
544
+ uint64_t ));
545
+ } else {
546
+ res = print_decimal (param ,
547
+ (int64_t )__builtin_va_arg (args ,
548
+ uint32_t ));
549
+ }
571
550
}
572
551
/* octal number */
573
552
else if (ch == 'o' ) {
574
- res = print_pow2 ( param ,
575
- (( param -> vars . flags &
576
- PRINT_FLAG_LONG_LONG ) != 0U ) ?
553
+ if (( param -> vars . flags &
554
+ PRINT_FLAG_LONG_LONG ) != 0U ) {
555
+ res = print_pow2 ( param ,
577
556
__builtin_va_arg (args ,
578
- unsigned long long)
579
- : (unsigned long long )
557
+ uint64_t ), 3U );
558
+ } else {
559
+ res = print_pow2 (param ,
580
560
__builtin_va_arg (args ,
581
- uint32_t ),
582
- 3U );
561
+ uint32_t ), 3U );
562
+ }
583
563
}
584
564
/* hexadecimal number */
585
565
else if ((ch == 'X' ) || (ch == 'x' )) {
586
566
if (ch == 'X' ) {
587
567
param -> vars .flags |= PRINT_FLAG_UPPER ;
588
568
}
589
- res = print_pow2 ( param ,
590
- (( param -> vars . flags &
591
- PRINT_FLAG_LONG_LONG ) != 0U ) ?
569
+ if (( param -> vars . flags &
570
+ PRINT_FLAG_LONG_LONG ) != 0U ) {
571
+ res = print_pow2 ( param ,
592
572
__builtin_va_arg (args ,
593
- unsigned long long)
594
- : (unsigned long long )
573
+ uint64_t ), 4U );
574
+ } else {
575
+ res = print_pow2 (param ,
595
576
__builtin_va_arg (args ,
596
- uint32_t ),
597
- 4U );
577
+ uint32_t ), 4U );
578
+ }
598
579
}
599
580
/* string argument */
600
581
else if (ch == 's' ) {
@@ -635,28 +616,18 @@ int do_print(const char *fmt, struct print_param *param,
635
616
return res ;
636
617
}
637
618
638
- static int charmem (int cmd , const char * s , int sz , void * hnd )
619
+ static int charmem (int cmd , const char * s , uint32_t sz , void * hnd )
639
620
{
640
621
/* pointer to the snprint parameter list */
641
622
struct snprint_param * param = (struct snprint_param * ) hnd ;
642
623
/* pointer to the destination */
643
624
char * p = param -> dst + param -> wrtn ;
644
625
/* characters actually written */
645
- int n = 0 ;
626
+ uint32_t n = 0U ;
646
627
647
628
/* copy mode ? */
648
629
if (cmd == PRINT_CMD_COPY ) {
649
- if (sz < 0 ) {
650
- while ((* s ) != '\0' ) {
651
- if (n < (param -> sz - param -> wrtn )) {
652
- * p = * s ;
653
- }
654
- p ++ ;
655
- s ++ ;
656
- n ++ ;
657
- }
658
-
659
- } else if (sz > 0 ) {
630
+ if (sz > 0U ) {
660
631
while (((* s ) != '\0' ) && n < sz ) {
661
632
if (n < (param -> sz - param -> wrtn )) {
662
633
* p = * s ;
@@ -665,33 +636,26 @@ static int charmem(int cmd, const char *s, int sz, void *hnd)
665
636
s ++ ;
666
637
n ++ ;
667
638
}
668
- } else {
669
- /* sz == 0, no copy needed. */
670
639
}
671
640
672
641
param -> wrtn += n ;
673
- return n ;
674
642
}
675
643
/* fill mode */
676
644
else {
677
- n = (sz < ( param -> sz - param -> wrtn )) ? sz : 0 ;
645
+ n = (sz < param -> sz - param -> wrtn ) ? sz : 0U ;
678
646
param -> wrtn += sz ;
679
- (void )memset (p , * s , n );
647
+ (void )memset (p , ( uint8_t ) * s , n );
680
648
}
681
649
682
- return n ;
650
+ return ( int ) n ;
683
651
}
684
652
685
- int vsnprintf (char * dst , int32_t sz_arg , const char * fmt , va_list args )
653
+ int vsnprintf (char * dst , size_t sz , const char * fmt , va_list args )
686
654
{
687
- char c [1 ];
688
- /* the result of this function */
689
- int32_t sz = sz_arg ;
690
655
int res = 0 ;
691
656
692
- if (sz <= 0 || (dst == NULL )) {
693
- dst = c ;
694
- sz = 1 ;
657
+ if (sz == 0U || (dst == NULL )) {
658
+ return -1 ;
695
659
}
696
660
697
661
/* struct to store all necessary parameters */
@@ -722,7 +686,7 @@ int vsnprintf(char *dst, int32_t sz_arg, const char *fmt, va_list args)
722
686
}
723
687
724
688
/* return the number of chars which would be written */
725
- res = snparam .wrtn ;
689
+ res = ( int ) snparam .wrtn ;
726
690
727
691
/* done */
728
692
return res ;
@@ -738,7 +702,7 @@ int snprintf(char *dest, int sz, const char *fmt, ...)
738
702
va_start (args , fmt );
739
703
740
704
/* execute the printf() */
741
- res = vsnprintf (dest , sz , fmt , args );
705
+ res = vsnprintf (dest , ( size_t ) sz , fmt , args );
742
706
743
707
/* destroy parameter list */
744
708
va_end (args );
0 commit comments