Safe API Framework 0.1.0 build 384e6ae
Layered API framework for safety-related applications (ERTMS RBC reference targeting CENELEC EN 50128 SIL 4)
Loading...
Searching...
No Matches
rte_cast.c
Go to the documentation of this file.
1
10
12
13rte_status_t rte_cast_i8_to_i16(int8_t in, int16_t *out)
14{
15 if (out == NULL)
16 {
18 }
19 *out = (int16_t)in;
20 return RTE_STATUS_OK;
21}
22
23rte_status_t rte_cast_i8_to_i32(int8_t in, int32_t *out)
24{
25 if (out == NULL)
26 {
28 }
29 *out = (int32_t)in;
30 return RTE_STATUS_OK;
31}
32
33rte_status_t rte_cast_i8_to_i64(int8_t in, int64_t *out)
34{
35 if (out == NULL)
36 {
38 }
39 *out = (int64_t)in;
40 return RTE_STATUS_OK;
41}
42
43rte_status_t rte_cast_i8_to_u8(int8_t in, uint8_t *out)
44{
45 if (out == NULL)
46 {
48 }
49 int64_t widened = (int64_t)in;
50 if (widened < 0)
51 {
53 }
54 *out = (uint8_t)in;
55 return RTE_STATUS_OK;
56}
57
58rte_status_t rte_cast_i8_to_u16(int8_t in, uint16_t *out)
59{
60 if (out == NULL)
61 {
63 }
64 int64_t widened = (int64_t)in;
65 if (widened < 0)
66 {
68 }
69 *out = (uint16_t)in;
70 return RTE_STATUS_OK;
71}
72
73rte_status_t rte_cast_i8_to_u32(int8_t in, uint32_t *out)
74{
75 if (out == NULL)
76 {
78 }
79 int64_t widened = (int64_t)in;
80 if (widened < 0)
81 {
83 }
84 *out = (uint32_t)in;
85 return RTE_STATUS_OK;
86}
87
88rte_status_t rte_cast_i8_to_u64(int8_t in, uint64_t *out)
89{
90 if (out == NULL)
91 {
93 }
94 int64_t widened = (int64_t)in;
95 if (widened < 0)
96 {
98 }
99 *out = (uint64_t)in;
100 return RTE_STATUS_OK;
101}
102
103rte_status_t rte_cast_i8_to_size(int8_t in, size_t *out)
104{
105 if (out == NULL)
106 {
108 }
109 int64_t widened = (int64_t)in;
110 if (widened < 0)
111 {
113 }
114#if SIZE_MAX < UINT64_MAX
115 if ((uint64_t)widened > (uint64_t)SIZE_MAX)
116 {
118 }
119#endif
120 *out = (size_t)in;
121 return RTE_STATUS_OK;
122}
123
124rte_status_t rte_cast_i16_to_i8(int16_t in, int8_t *out)
125{
126 if (out == NULL)
127 {
129 }
130 int64_t widened = (int64_t)in;
131 if (widened < (int64_t)INT8_MIN)
132 {
134 }
135 if (widened > (int64_t)INT8_MAX)
136 {
138 }
139 *out = (int8_t)in;
140 return RTE_STATUS_OK;
141}
142
143rte_status_t rte_cast_i16_to_i32(int16_t in, int32_t *out)
144{
145 if (out == NULL)
146 {
148 }
149 *out = (int32_t)in;
150 return RTE_STATUS_OK;
151}
152
153rte_status_t rte_cast_i16_to_i64(int16_t in, int64_t *out)
154{
155 if (out == NULL)
156 {
158 }
159 *out = (int64_t)in;
160 return RTE_STATUS_OK;
161}
162
163rte_status_t rte_cast_i16_to_u8(int16_t in, uint8_t *out)
164{
165 if (out == NULL)
166 {
168 }
169 int64_t widened = (int64_t)in;
170 if (widened < 0)
171 {
173 }
174 if ((uint64_t)widened > (uint64_t)UINT8_MAX)
175 {
177 }
178 *out = (uint8_t)in;
179 return RTE_STATUS_OK;
180}
181
182rte_status_t rte_cast_i16_to_u16(int16_t in, uint16_t *out)
183{
184 if (out == NULL)
185 {
187 }
188 int64_t widened = (int64_t)in;
189 if (widened < 0)
190 {
192 }
193 *out = (uint16_t)in;
194 return RTE_STATUS_OK;
195}
196
197rte_status_t rte_cast_i16_to_u32(int16_t in, uint32_t *out)
198{
199 if (out == NULL)
200 {
202 }
203 int64_t widened = (int64_t)in;
204 if (widened < 0)
205 {
207 }
208 *out = (uint32_t)in;
209 return RTE_STATUS_OK;
210}
211
212rte_status_t rte_cast_i16_to_u64(int16_t in, uint64_t *out)
213{
214 if (out == NULL)
215 {
217 }
218 int64_t widened = (int64_t)in;
219 if (widened < 0)
220 {
222 }
223 *out = (uint64_t)in;
224 return RTE_STATUS_OK;
225}
226
227rte_status_t rte_cast_i16_to_size(int16_t in, size_t *out)
228{
229 if (out == NULL)
230 {
232 }
233 int64_t widened = (int64_t)in;
234 if (widened < 0)
235 {
237 }
238#if SIZE_MAX < UINT64_MAX
239 if ((uint64_t)widened > (uint64_t)SIZE_MAX)
240 {
242 }
243#endif
244 *out = (size_t)in;
245 return RTE_STATUS_OK;
246}
247
248rte_status_t rte_cast_i32_to_i8(int32_t in, int8_t *out)
249{
250 if (out == NULL)
251 {
253 }
254 int64_t widened = (int64_t)in;
255 if (widened < (int64_t)INT8_MIN)
256 {
258 }
259 if (widened > (int64_t)INT8_MAX)
260 {
262 }
263 *out = (int8_t)in;
264 return RTE_STATUS_OK;
265}
266
267rte_status_t rte_cast_i32_to_i16(int32_t in, int16_t *out)
268{
269 if (out == NULL)
270 {
272 }
273 int64_t widened = (int64_t)in;
274 if (widened < (int64_t)INT16_MIN)
275 {
277 }
278 if (widened > (int64_t)INT16_MAX)
279 {
281 }
282 *out = (int16_t)in;
283 return RTE_STATUS_OK;
284}
285
286rte_status_t rte_cast_i32_to_i64(int32_t in, int64_t *out)
287{
288 if (out == NULL)
289 {
291 }
292 *out = (int64_t)in;
293 return RTE_STATUS_OK;
294}
295
296rte_status_t rte_cast_i32_to_u8(int32_t in, uint8_t *out)
297{
298 if (out == NULL)
299 {
301 }
302 int64_t widened = (int64_t)in;
303 if (widened < 0)
304 {
306 }
307 if ((uint64_t)widened > (uint64_t)UINT8_MAX)
308 {
310 }
311 *out = (uint8_t)in;
312 return RTE_STATUS_OK;
313}
314
315rte_status_t rte_cast_i32_to_u16(int32_t in, uint16_t *out)
316{
317 if (out == NULL)
318 {
320 }
321 int64_t widened = (int64_t)in;
322 if (widened < 0)
323 {
325 }
326 if ((uint64_t)widened > (uint64_t)UINT16_MAX)
327 {
329 }
330 *out = (uint16_t)in;
331 return RTE_STATUS_OK;
332}
333
334rte_status_t rte_cast_i32_to_u32(int32_t in, uint32_t *out)
335{
336 if (out == NULL)
337 {
339 }
340 int64_t widened = (int64_t)in;
341 if (widened < 0)
342 {
344 }
345 *out = (uint32_t)in;
346 return RTE_STATUS_OK;
347}
348
349rte_status_t rte_cast_i32_to_u64(int32_t in, uint64_t *out)
350{
351 if (out == NULL)
352 {
354 }
355 int64_t widened = (int64_t)in;
356 if (widened < 0)
357 {
359 }
360 *out = (uint64_t)in;
361 return RTE_STATUS_OK;
362}
363
364rte_status_t rte_cast_i32_to_size(int32_t in, size_t *out)
365{
366 if (out == NULL)
367 {
369 }
370 int64_t widened = (int64_t)in;
371 if (widened < 0)
372 {
374 }
375#if SIZE_MAX < UINT64_MAX
376 if ((uint64_t)widened > (uint64_t)SIZE_MAX)
377 {
379 }
380#endif
381 *out = (size_t)in;
382 return RTE_STATUS_OK;
383}
384
385rte_status_t rte_cast_i64_to_i8(int64_t in, int8_t *out)
386{
387 if (out == NULL)
388 {
390 }
391 int64_t widened = (int64_t)in;
392 if (widened < (int64_t)INT8_MIN)
393 {
395 }
396 if (widened > (int64_t)INT8_MAX)
397 {
399 }
400 *out = (int8_t)in;
401 return RTE_STATUS_OK;
402}
403
404rte_status_t rte_cast_i64_to_i16(int64_t in, int16_t *out)
405{
406 if (out == NULL)
407 {
409 }
410 int64_t widened = (int64_t)in;
411 if (widened < (int64_t)INT16_MIN)
412 {
414 }
415 if (widened > (int64_t)INT16_MAX)
416 {
418 }
419 *out = (int16_t)in;
420 return RTE_STATUS_OK;
421}
422
423rte_status_t rte_cast_i64_to_i32(int64_t in, int32_t *out)
424{
425 if (out == NULL)
426 {
428 }
429 int64_t widened = (int64_t)in;
430 if (widened < (int64_t)INT32_MIN)
431 {
433 }
434 if (widened > (int64_t)INT32_MAX)
435 {
437 }
438 *out = (int32_t)in;
439 return RTE_STATUS_OK;
440}
441
442rte_status_t rte_cast_i64_to_u8(int64_t in, uint8_t *out)
443{
444 if (out == NULL)
445 {
447 }
448 int64_t widened = (int64_t)in;
449 if (widened < 0)
450 {
452 }
453 if ((uint64_t)widened > (uint64_t)UINT8_MAX)
454 {
456 }
457 *out = (uint8_t)in;
458 return RTE_STATUS_OK;
459}
460
461rte_status_t rte_cast_i64_to_u16(int64_t in, uint16_t *out)
462{
463 if (out == NULL)
464 {
466 }
467 int64_t widened = (int64_t)in;
468 if (widened < 0)
469 {
471 }
472 if ((uint64_t)widened > (uint64_t)UINT16_MAX)
473 {
475 }
476 *out = (uint16_t)in;
477 return RTE_STATUS_OK;
478}
479
480rte_status_t rte_cast_i64_to_u32(int64_t in, uint32_t *out)
481{
482 if (out == NULL)
483 {
485 }
486 int64_t widened = (int64_t)in;
487 if (widened < 0)
488 {
490 }
491 if ((uint64_t)widened > (uint64_t)UINT32_MAX)
492 {
494 }
495 *out = (uint32_t)in;
496 return RTE_STATUS_OK;
497}
498
499rte_status_t rte_cast_i64_to_u64(int64_t in, uint64_t *out)
500{
501 if (out == NULL)
502 {
504 }
505 int64_t widened = (int64_t)in;
506 if (widened < 0)
507 {
509 }
510 *out = (uint64_t)in;
511 return RTE_STATUS_OK;
512}
513
514rte_status_t rte_cast_i64_to_size(int64_t in, size_t *out)
515{
516 if (out == NULL)
517 {
519 }
520 int64_t widened = (int64_t)in;
521 if (widened < 0)
522 {
524 }
525#if SIZE_MAX < UINT64_MAX
526 if ((uint64_t)widened > (uint64_t)SIZE_MAX)
527 {
529 }
530#endif
531 *out = (size_t)in;
532 return RTE_STATUS_OK;
533}
534
535rte_status_t rte_cast_u8_to_i8(uint8_t in, int8_t *out)
536{
537 if (out == NULL)
538 {
540 }
541 uint64_t widened = (uint64_t)in;
542 if (widened > (uint64_t)INT8_MAX)
543 {
545 }
546 *out = (int8_t)in;
547 return RTE_STATUS_OK;
548}
549
550rte_status_t rte_cast_u8_to_i16(uint8_t in, int16_t *out)
551{
552 if (out == NULL)
553 {
555 }
556 *out = (int16_t)in;
557 return RTE_STATUS_OK;
558}
559
560rte_status_t rte_cast_u8_to_i32(uint8_t in, int32_t *out)
561{
562 if (out == NULL)
563 {
565 }
566 *out = (int32_t)in;
567 return RTE_STATUS_OK;
568}
569
570rte_status_t rte_cast_u8_to_i64(uint8_t in, int64_t *out)
571{
572 if (out == NULL)
573 {
575 }
576 *out = (int64_t)in;
577 return RTE_STATUS_OK;
578}
579
580rte_status_t rte_cast_u8_to_u16(uint8_t in, uint16_t *out)
581{
582 if (out == NULL)
583 {
585 }
586 *out = (uint16_t)in;
587 return RTE_STATUS_OK;
588}
589
590rte_status_t rte_cast_u8_to_u32(uint8_t in, uint32_t *out)
591{
592 if (out == NULL)
593 {
595 }
596 *out = (uint32_t)in;
597 return RTE_STATUS_OK;
598}
599
600rte_status_t rte_cast_u8_to_u64(uint8_t in, uint64_t *out)
601{
602 if (out == NULL)
603 {
605 }
606 *out = (uint64_t)in;
607 return RTE_STATUS_OK;
608}
609
610rte_status_t rte_cast_u8_to_size(uint8_t in, size_t *out)
611{
612 if (out == NULL)
613 {
615 }
616#if SIZE_MAX < UINT64_MAX
617 {
618 uint64_t widened = (uint64_t)in;
619 if (widened > (uint64_t)SIZE_MAX)
620 {
622 }
623 }
624#endif
625 *out = (size_t)in;
626 return RTE_STATUS_OK;
627}
628
629rte_status_t rte_cast_u16_to_i8(uint16_t in, int8_t *out)
630{
631 if (out == NULL)
632 {
634 }
635 uint64_t widened = (uint64_t)in;
636 if (widened > (uint64_t)INT8_MAX)
637 {
639 }
640 *out = (int8_t)in;
641 return RTE_STATUS_OK;
642}
643
644rte_status_t rte_cast_u16_to_i16(uint16_t in, int16_t *out)
645{
646 if (out == NULL)
647 {
649 }
650 uint64_t widened = (uint64_t)in;
651 if (widened > (uint64_t)INT16_MAX)
652 {
654 }
655 *out = (int16_t)in;
656 return RTE_STATUS_OK;
657}
658
659rte_status_t rte_cast_u16_to_i32(uint16_t in, int32_t *out)
660{
661 if (out == NULL)
662 {
664 }
665 *out = (int32_t)in;
666 return RTE_STATUS_OK;
667}
668
669rte_status_t rte_cast_u16_to_i64(uint16_t in, int64_t *out)
670{
671 if (out == NULL)
672 {
674 }
675 *out = (int64_t)in;
676 return RTE_STATUS_OK;
677}
678
679rte_status_t rte_cast_u16_to_u8(uint16_t in, uint8_t *out)
680{
681 if (out == NULL)
682 {
684 }
685 uint64_t widened = (uint64_t)in;
686 if (widened > (uint64_t)UINT8_MAX)
687 {
689 }
690 *out = (uint8_t)in;
691 return RTE_STATUS_OK;
692}
693
694rte_status_t rte_cast_u16_to_u32(uint16_t in, uint32_t *out)
695{
696 if (out == NULL)
697 {
699 }
700 *out = (uint32_t)in;
701 return RTE_STATUS_OK;
702}
703
704rte_status_t rte_cast_u16_to_u64(uint16_t in, uint64_t *out)
705{
706 if (out == NULL)
707 {
709 }
710 *out = (uint64_t)in;
711 return RTE_STATUS_OK;
712}
713
714rte_status_t rte_cast_u16_to_size(uint16_t in, size_t *out)
715{
716 if (out == NULL)
717 {
719 }
720#if SIZE_MAX < UINT64_MAX
721 {
722 uint64_t widened = (uint64_t)in;
723 if (widened > (uint64_t)SIZE_MAX)
724 {
726 }
727 }
728#endif
729 *out = (size_t)in;
730 return RTE_STATUS_OK;
731}
732
733rte_status_t rte_cast_u32_to_i8(uint32_t in, int8_t *out)
734{
735 if (out == NULL)
736 {
738 }
739 uint64_t widened = (uint64_t)in;
740 if (widened > (uint64_t)INT8_MAX)
741 {
743 }
744 *out = (int8_t)in;
745 return RTE_STATUS_OK;
746}
747
748rte_status_t rte_cast_u32_to_i16(uint32_t in, int16_t *out)
749{
750 if (out == NULL)
751 {
753 }
754 uint64_t widened = (uint64_t)in;
755 if (widened > (uint64_t)INT16_MAX)
756 {
758 }
759 *out = (int16_t)in;
760 return RTE_STATUS_OK;
761}
762
763rte_status_t rte_cast_u32_to_i32(uint32_t in, int32_t *out)
764{
765 if (out == NULL)
766 {
768 }
769 uint64_t widened = (uint64_t)in;
770 if (widened > (uint64_t)INT32_MAX)
771 {
773 }
774 *out = (int32_t)in;
775 return RTE_STATUS_OK;
776}
777
778rte_status_t rte_cast_u32_to_i64(uint32_t in, int64_t *out)
779{
780 if (out == NULL)
781 {
783 }
784 *out = (int64_t)in;
785 return RTE_STATUS_OK;
786}
787
788rte_status_t rte_cast_u32_to_u8(uint32_t in, uint8_t *out)
789{
790 if (out == NULL)
791 {
793 }
794 uint64_t widened = (uint64_t)in;
795 if (widened > (uint64_t)UINT8_MAX)
796 {
798 }
799 *out = (uint8_t)in;
800 return RTE_STATUS_OK;
801}
802
803rte_status_t rte_cast_u32_to_u16(uint32_t in, uint16_t *out)
804{
805 if (out == NULL)
806 {
808 }
809 uint64_t widened = (uint64_t)in;
810 if (widened > (uint64_t)UINT16_MAX)
811 {
813 }
814 *out = (uint16_t)in;
815 return RTE_STATUS_OK;
816}
817
818rte_status_t rte_cast_u32_to_u64(uint32_t in, uint64_t *out)
819{
820 if (out == NULL)
821 {
823 }
824 *out = (uint64_t)in;
825 return RTE_STATUS_OK;
826}
827
828rte_status_t rte_cast_u32_to_size(uint32_t in, size_t *out)
829{
830 if (out == NULL)
831 {
833 }
834#if SIZE_MAX < UINT64_MAX
835 {
836 uint64_t widened = (uint64_t)in;
837 if (widened > (uint64_t)SIZE_MAX)
838 {
840 }
841 }
842#endif
843 *out = (size_t)in;
844 return RTE_STATUS_OK;
845}
846
847rte_status_t rte_cast_u64_to_i8(uint64_t in, int8_t *out)
848{
849 if (out == NULL)
850 {
852 }
853 uint64_t widened = (uint64_t)in;
854 if (widened > (uint64_t)INT8_MAX)
855 {
857 }
858 *out = (int8_t)in;
859 return RTE_STATUS_OK;
860}
861
862rte_status_t rte_cast_u64_to_i16(uint64_t in, int16_t *out)
863{
864 if (out == NULL)
865 {
867 }
868 uint64_t widened = (uint64_t)in;
869 if (widened > (uint64_t)INT16_MAX)
870 {
872 }
873 *out = (int16_t)in;
874 return RTE_STATUS_OK;
875}
876
877rte_status_t rte_cast_u64_to_i32(uint64_t in, int32_t *out)
878{
879 if (out == NULL)
880 {
882 }
883 uint64_t widened = (uint64_t)in;
884 if (widened > (uint64_t)INT32_MAX)
885 {
887 }
888 *out = (int32_t)in;
889 return RTE_STATUS_OK;
890}
891
892rte_status_t rte_cast_u64_to_i64(uint64_t in, int64_t *out)
893{
894 if (out == NULL)
895 {
897 }
898 uint64_t widened = (uint64_t)in;
899 if (widened > (uint64_t)INT64_MAX)
900 {
902 }
903 *out = (int64_t)in;
904 return RTE_STATUS_OK;
905}
906
907rte_status_t rte_cast_u64_to_u8(uint64_t in, uint8_t *out)
908{
909 if (out == NULL)
910 {
912 }
913 uint64_t widened = (uint64_t)in;
914 if (widened > (uint64_t)UINT8_MAX)
915 {
917 }
918 *out = (uint8_t)in;
919 return RTE_STATUS_OK;
920}
921
922rte_status_t rte_cast_u64_to_u16(uint64_t in, uint16_t *out)
923{
924 if (out == NULL)
925 {
927 }
928 uint64_t widened = (uint64_t)in;
929 if (widened > (uint64_t)UINT16_MAX)
930 {
932 }
933 *out = (uint16_t)in;
934 return RTE_STATUS_OK;
935}
936
937rte_status_t rte_cast_u64_to_u32(uint64_t in, uint32_t *out)
938{
939 if (out == NULL)
940 {
942 }
943 uint64_t widened = (uint64_t)in;
944 if (widened > (uint64_t)UINT32_MAX)
945 {
947 }
948 *out = (uint32_t)in;
949 return RTE_STATUS_OK;
950}
951
952rte_status_t rte_cast_u64_to_size(uint64_t in, size_t *out)
953{
954 if (out == NULL)
955 {
957 }
958#if SIZE_MAX < UINT64_MAX
959 {
960 uint64_t widened = (uint64_t)in;
961 if (widened > (uint64_t)SIZE_MAX)
962 {
964 }
965 }
966#endif
967 *out = (size_t)in;
968 return RTE_STATUS_OK;
969}
970
971rte_status_t rte_cast_size_to_i8(size_t in, int8_t *out)
972{
973 if (out == NULL)
974 {
976 }
977 /* size_t is assumed to fit within uint64_t on any supported target (ADR-003). */
978 uint64_t widened = (uint64_t)in;
979 if (widened > (uint64_t)INT8_MAX)
980 {
982 }
983 *out = (int8_t)in;
984 return RTE_STATUS_OK;
985}
986
987rte_status_t rte_cast_size_to_i16(size_t in, int16_t *out)
988{
989 if (out == NULL)
990 {
992 }
993 /* size_t is assumed to fit within uint64_t on any supported target (ADR-003). */
994 uint64_t widened = (uint64_t)in;
995 if (widened > (uint64_t)INT16_MAX)
996 {
998 }
999 *out = (int16_t)in;
1000 return RTE_STATUS_OK;
1001}
1002
1003rte_status_t rte_cast_size_to_i32(size_t in, int32_t *out)
1004{
1005 if (out == NULL)
1006 {
1008 }
1009 /* size_t is assumed to fit within uint64_t on any supported target (ADR-003). */
1010 uint64_t widened = (uint64_t)in;
1011 if (widened > (uint64_t)INT32_MAX)
1012 {
1014 }
1015 *out = (int32_t)in;
1016 return RTE_STATUS_OK;
1017}
1018
1019rte_status_t rte_cast_size_to_i64(size_t in, int64_t *out)
1020{
1021 if (out == NULL)
1022 {
1024 }
1025 /* size_t is assumed to fit within uint64_t on any supported target (ADR-003). */
1026 uint64_t widened = (uint64_t)in;
1027 if (widened > (uint64_t)INT64_MAX)
1028 {
1030 }
1031 *out = (int64_t)in;
1032 return RTE_STATUS_OK;
1033}
1034
1035rte_status_t rte_cast_size_to_u8(size_t in, uint8_t *out)
1036{
1037 if (out == NULL)
1038 {
1040 }
1041 /* size_t is assumed to fit within uint64_t on any supported target (ADR-003). */
1042 uint64_t widened = (uint64_t)in;
1043 if (widened > (uint64_t)UINT8_MAX)
1044 {
1046 }
1047 *out = (uint8_t)in;
1048 return RTE_STATUS_OK;
1049}
1050
1051rte_status_t rte_cast_size_to_u16(size_t in, uint16_t *out)
1052{
1053 if (out == NULL)
1054 {
1056 }
1057 /* size_t is assumed to fit within uint64_t on any supported target (ADR-003). */
1058 uint64_t widened = (uint64_t)in;
1059 if (widened > (uint64_t)UINT16_MAX)
1060 {
1062 }
1063 *out = (uint16_t)in;
1064 return RTE_STATUS_OK;
1065}
1066
1067rte_status_t rte_cast_size_to_u32(size_t in, uint32_t *out)
1068{
1069 if (out == NULL)
1070 {
1072 }
1073 /* size_t is assumed to fit within uint64_t on any supported target (ADR-003). */
1074 uint64_t widened = (uint64_t)in;
1075 if (widened > (uint64_t)UINT32_MAX)
1076 {
1078 }
1079 *out = (uint32_t)in;
1080 return RTE_STATUS_OK;
1081}
1082
1083rte_status_t rte_cast_size_to_u64(size_t in, uint64_t *out)
1084{
1085 if (out == NULL)
1086 {
1088 }
1089 /* size_t is assumed to fit within uint64_t on any supported target (ADR-003). */
1090 *out = (uint64_t)in;
1091 return RTE_STATUS_OK;
1092}
1093
1094rte_status_t rte_cast_checked_add_u32(uint32_t a, uint32_t b, uint32_t *out)
1095{
1096 if (out == NULL)
1097 {
1099 }
1100 /* a + b cannot overflow uint32_t arithmetic itself here because both
1101 * operands are first widened to uint64_t - the overflow check below
1102 * catches what would have wrapped had the addition stayed at uint32_t
1103 * width. */
1104 uint64_t widened = (uint64_t)a + (uint64_t)b;
1105 if (widened > (uint64_t)UINT32_MAX)
1106 {
1108 "rte_cast_checked_add_u32: addition overflowed uint32_t");
1110 }
1111 *out = (uint32_t)widened;
1112 return RTE_STATUS_OK;
1113}
1114
1115rte_status_t rte_cast_checked_sub_u32(uint32_t a, uint32_t b, uint32_t *out)
1116{
1117 if (out == NULL)
1118 {
1120 }
1121 if (b > a)
1122 {
1124 "rte_cast_checked_sub_u32: subtraction underflowed uint32_t");
1126 }
1127 *out = a - b;
1128 return RTE_STATUS_OK;
1129}
1130
1131rte_status_t rte_cast_checked_mul_u32(uint32_t a, uint32_t b, uint32_t *out)
1132{
1133 if (out == NULL)
1134 {
1136 }
1137 uint64_t widened = (uint64_t)a * (uint64_t)b;
1138 if (widened > (uint64_t)UINT32_MAX)
1139 {
1141 "rte_cast_checked_mul_u32: multiplication overflowed uint32_t");
1143 }
1144 *out = (uint32_t)widened;
1145 return RTE_STATUS_OK;
1146}
1147
1148rte_status_t rte_cast_checked_add_size(size_t a, size_t b, size_t *out)
1149{
1150 if (out == NULL)
1151 {
1153 }
1154 /* SIZE_MAX - a cannot underflow: a is already known <= SIZE_MAX, so
1155 * this subtraction is always well-defined and gives the largest b
1156 * that would not overflow. */
1157 if (b > (SIZE_MAX - a))
1158 {
1160 "rte_cast_checked_add_size: addition overflowed size_t");
1162 }
1163 *out = a + b;
1164 return RTE_STATUS_OK;
1165}
1166
1167rte_status_t rte_cast_checked_sub_size(size_t a, size_t b, size_t *out)
1168{
1169 if (out == NULL)
1170 {
1172 }
1173 if (b > a)
1174 {
1176 "rte_cast_checked_sub_size: subtraction underflowed size_t");
1178 }
1179 *out = a - b;
1180 return RTE_STATUS_OK;
1181}
1182
1183rte_status_t rte_cast_checked_mul_size(size_t a, size_t b, size_t *out)
1184{
1185 if (out == NULL)
1186 {
1188 }
1189 if ((a != 0U) && (b > (SIZE_MAX / a)))
1190 {
1192 "rte_cast_checked_mul_size: multiplication overflowed size_t");
1194 }
1195 *out = a * b;
1196 return RTE_STATUS_OK;
1197}
1198
1199rte_status_t rte_cast_bounds_check(size_t index, size_t count)
1200{
1201 if (index >= count)
1202 {
1204 "rte_cast_bounds_check: index out of range");
1206 }
1207 return RTE_STATUS_OK;
1208}
rte_status_t rte_cast_u64_to_u8(uint64_t in, uint8_t *out)
Checked cast from uint64_t to uint8_t.
Definition rte_cast.c:907
rte_status_t rte_cast_i16_to_u32(int16_t in, uint32_t *out)
Checked cast from int16_t to uint32_t.
Definition rte_cast.c:197
rte_status_t rte_cast_u64_to_i64(uint64_t in, int64_t *out)
Checked cast from uint64_t to int64_t.
Definition rte_cast.c:892
rte_status_t rte_cast_size_to_u8(size_t in, uint8_t *out)
Checked cast from size_t to uint8_t.
Definition rte_cast.c:1035
rte_status_t rte_cast_u32_to_u8(uint32_t in, uint8_t *out)
Checked cast from uint32_t to uint8_t.
Definition rte_cast.c:788
rte_status_t rte_cast_u8_to_u64(uint8_t in, uint64_t *out)
Checked cast from uint8_t to uint64_t.
Definition rte_cast.c:600
rte_status_t rte_cast_u8_to_i16(uint8_t in, int16_t *out)
Checked cast from uint8_t to int16_t.
Definition rte_cast.c:550
rte_status_t rte_cast_i16_to_size(int16_t in, size_t *out)
Checked cast from int16_t to size_t.
Definition rte_cast.c:227
rte_status_t rte_cast_checked_mul_u32(uint32_t a, uint32_t b, uint32_t *out)
Checked multiplication: *out = a * b, only if the result fits uint32_t.
Definition rte_cast.c:1131
rte_status_t rte_cast_u8_to_i32(uint8_t in, int32_t *out)
Checked cast from uint8_t to int32_t.
Definition rte_cast.c:560
rte_status_t rte_cast_u16_to_i16(uint16_t in, int16_t *out)
Checked cast from uint16_t to int16_t.
Definition rte_cast.c:644
rte_status_t rte_cast_i32_to_u8(int32_t in, uint8_t *out)
Checked cast from int32_t to uint8_t.
Definition rte_cast.c:296
rte_status_t rte_cast_bounds_check(size_t index, size_t count)
Checked array/index bounds check: is index a valid index into an array of count elements?
Definition rte_cast.c:1199
rte_status_t rte_cast_i32_to_i8(int32_t in, int8_t *out)
Checked cast from int32_t to int8_t.
Definition rte_cast.c:248
rte_status_t rte_cast_i32_to_u64(int32_t in, uint64_t *out)
Checked cast from int32_t to uint64_t.
Definition rte_cast.c:349
rte_status_t rte_cast_size_to_u64(size_t in, uint64_t *out)
Checked cast from size_t to uint64_t.
Definition rte_cast.c:1083
rte_status_t rte_cast_u16_to_u8(uint16_t in, uint8_t *out)
Checked cast from uint16_t to uint8_t.
Definition rte_cast.c:679
rte_status_t rte_cast_u64_to_u32(uint64_t in, uint32_t *out)
Checked cast from uint64_t to uint32_t.
Definition rte_cast.c:937
rte_status_t rte_cast_checked_sub_size(size_t a, size_t b, size_t *out)
Checked subtraction: *out = a - b, only if b <= a (no underflow).
Definition rte_cast.c:1167
rte_status_t rte_cast_u8_to_u32(uint8_t in, uint32_t *out)
Checked cast from uint8_t to uint32_t.
Definition rte_cast.c:590
rte_status_t rte_cast_u16_to_i32(uint16_t in, int32_t *out)
Checked cast from uint16_t to int32_t.
Definition rte_cast.c:659
rte_status_t rte_cast_i64_to_u16(int64_t in, uint16_t *out)
Checked cast from int64_t to uint16_t.
Definition rte_cast.c:461
rte_status_t rte_cast_i64_to_u64(int64_t in, uint64_t *out)
Checked cast from int64_t to uint64_t.
Definition rte_cast.c:499
rte_status_t rte_cast_i32_to_i64(int32_t in, int64_t *out)
Checked cast from int32_t to int64_t.
Definition rte_cast.c:286
rte_status_t rte_cast_i8_to_u8(int8_t in, uint8_t *out)
Checked cast from int8_t to uint8_t.
Definition rte_cast.c:43
rte_status_t rte_cast_i64_to_i16(int64_t in, int16_t *out)
Checked cast from int64_t to int16_t.
Definition rte_cast.c:404
rte_status_t rte_cast_u8_to_i8(uint8_t in, int8_t *out)
Checked cast from uint8_t to int8_t.
Definition rte_cast.c:535
rte_status_t rte_cast_u64_to_size(uint64_t in, size_t *out)
Checked cast from uint64_t to size_t.
Definition rte_cast.c:952
rte_status_t rte_cast_i8_to_i32(int8_t in, int32_t *out)
Checked cast from int8_t to int32_t.
Definition rte_cast.c:23
rte_status_t rte_cast_size_to_i8(size_t in, int8_t *out)
Checked cast from size_t to int8_t.
Definition rte_cast.c:971
rte_status_t rte_cast_i32_to_size(int32_t in, size_t *out)
Checked cast from int32_t to size_t.
Definition rte_cast.c:364
rte_status_t rte_cast_i16_to_i64(int16_t in, int64_t *out)
Checked cast from int16_t to int64_t.
Definition rte_cast.c:153
rte_status_t rte_cast_i64_to_u8(int64_t in, uint8_t *out)
Checked cast from int64_t to uint8_t.
Definition rte_cast.c:442
rte_status_t rte_cast_i16_to_u8(int16_t in, uint8_t *out)
Checked cast from int16_t to uint8_t.
Definition rte_cast.c:163
rte_status_t rte_cast_u32_to_i16(uint32_t in, int16_t *out)
Checked cast from uint32_t to int16_t.
Definition rte_cast.c:748
rte_status_t rte_cast_i8_to_u16(int8_t in, uint16_t *out)
Checked cast from int8_t to uint16_t.
Definition rte_cast.c:58
rte_status_t rte_cast_i32_to_u32(int32_t in, uint32_t *out)
Checked cast from int32_t to uint32_t.
Definition rte_cast.c:334
rte_status_t rte_cast_u32_to_u64(uint32_t in, uint64_t *out)
Checked cast from uint32_t to uint64_t.
Definition rte_cast.c:818
rte_status_t rte_cast_u8_to_i64(uint8_t in, int64_t *out)
Checked cast from uint8_t to int64_t.
Definition rte_cast.c:570
rte_status_t rte_cast_checked_sub_u32(uint32_t a, uint32_t b, uint32_t *out)
Checked subtraction: *out = a - b, only if b <= a (no underflow).
Definition rte_cast.c:1115
rte_status_t rte_cast_u8_to_u16(uint8_t in, uint16_t *out)
Checked cast from uint8_t to uint16_t.
Definition rte_cast.c:580
rte_status_t rte_cast_i8_to_u32(int8_t in, uint32_t *out)
Checked cast from int8_t to uint32_t.
Definition rte_cast.c:73
rte_status_t rte_cast_u16_to_size(uint16_t in, size_t *out)
Checked cast from uint16_t to size_t.
Definition rte_cast.c:714
rte_status_t rte_cast_i64_to_i8(int64_t in, int8_t *out)
Checked cast from int64_t to int8_t.
Definition rte_cast.c:385
rte_status_t rte_cast_i64_to_size(int64_t in, size_t *out)
Checked cast from int64_t to size_t.
Definition rte_cast.c:514
rte_status_t rte_cast_checked_add_u32(uint32_t a, uint32_t b, uint32_t *out)
Checked addition: *out = a + b, only if the result fits uint32_t.
Definition rte_cast.c:1094
rte_status_t rte_cast_size_to_i64(size_t in, int64_t *out)
Checked cast from size_t to int64_t.
Definition rte_cast.c:1019
rte_status_t rte_cast_u16_to_u32(uint16_t in, uint32_t *out)
Checked cast from uint16_t to uint32_t.
Definition rte_cast.c:694
rte_status_t rte_cast_u16_to_u64(uint16_t in, uint64_t *out)
Checked cast from uint16_t to uint64_t.
Definition rte_cast.c:704
rte_status_t rte_cast_u16_to_i8(uint16_t in, int8_t *out)
Checked cast from uint16_t to int8_t.
Definition rte_cast.c:629
rte_status_t rte_cast_i8_to_u64(int8_t in, uint64_t *out)
Checked cast from int8_t to uint64_t.
Definition rte_cast.c:88
rte_status_t rte_cast_u64_to_i8(uint64_t in, int8_t *out)
Checked cast from uint64_t to int8_t.
Definition rte_cast.c:847
rte_status_t rte_cast_size_to_u16(size_t in, uint16_t *out)
Checked cast from size_t to uint16_t.
Definition rte_cast.c:1051
rte_status_t rte_cast_i64_to_u32(int64_t in, uint32_t *out)
Checked cast from int64_t to uint32_t.
Definition rte_cast.c:480
rte_status_t rte_cast_u8_to_size(uint8_t in, size_t *out)
Checked cast from uint8_t to size_t.
Definition rte_cast.c:610
rte_status_t rte_cast_i32_to_i16(int32_t in, int16_t *out)
Checked cast from int32_t to int16_t.
Definition rte_cast.c:267
rte_status_t rte_cast_i16_to_i32(int16_t in, int32_t *out)
Checked cast from int16_t to int32_t.
Definition rte_cast.c:143
rte_status_t rte_cast_u64_to_i16(uint64_t in, int16_t *out)
Checked cast from uint64_t to int16_t.
Definition rte_cast.c:862
rte_status_t rte_cast_size_to_i16(size_t in, int16_t *out)
Checked cast from size_t to int16_t.
Definition rte_cast.c:987
rte_status_t rte_cast_i16_to_u16(int16_t in, uint16_t *out)
Checked cast from int16_t to uint16_t.
Definition rte_cast.c:182
rte_status_t rte_cast_checked_mul_size(size_t a, size_t b, size_t *out)
Checked multiplication: *out = a * b, only if the result fits size_t.
Definition rte_cast.c:1183
rte_status_t rte_cast_u32_to_i64(uint32_t in, int64_t *out)
Checked cast from uint32_t to int64_t.
Definition rte_cast.c:778
rte_status_t rte_cast_u16_to_i64(uint16_t in, int64_t *out)
Checked cast from uint16_t to int64_t.
Definition rte_cast.c:669
rte_status_t rte_cast_u32_to_i8(uint32_t in, int8_t *out)
Checked cast from uint32_t to int8_t.
Definition rte_cast.c:733
rte_status_t rte_cast_i8_to_i16(int8_t in, int16_t *out)
Checked cast from int8_t to int16_t.
Definition rte_cast.c:13
rte_status_t rte_cast_u32_to_size(uint32_t in, size_t *out)
Checked cast from uint32_t to size_t.
Definition rte_cast.c:828
rte_status_t rte_cast_size_to_u32(size_t in, uint32_t *out)
Checked cast from size_t to uint32_t.
Definition rte_cast.c:1067
rte_status_t rte_cast_i64_to_i32(int64_t in, int32_t *out)
Checked cast from int64_t to int32_t.
Definition rte_cast.c:423
rte_status_t rte_cast_i16_to_u64(int16_t in, uint64_t *out)
Checked cast from int16_t to uint64_t.
Definition rte_cast.c:212
rte_status_t rte_cast_i8_to_size(int8_t in, size_t *out)
Checked cast from int8_t to size_t.
Definition rte_cast.c:103
rte_status_t rte_cast_u64_to_u16(uint64_t in, uint16_t *out)
Checked cast from uint64_t to uint16_t.
Definition rte_cast.c:922
rte_status_t rte_cast_u32_to_i32(uint32_t in, int32_t *out)
Checked cast from uint32_t to int32_t.
Definition rte_cast.c:763
rte_status_t rte_cast_i32_to_u16(int32_t in, uint16_t *out)
Checked cast from int32_t to uint16_t.
Definition rte_cast.c:315
rte_status_t rte_cast_u32_to_u16(uint32_t in, uint16_t *out)
Checked cast from uint32_t to uint16_t.
Definition rte_cast.c:803
rte_status_t rte_cast_checked_add_size(size_t a, size_t b, size_t *out)
Checked addition: *out = a + b, only if the result fits size_t.
Definition rte_cast.c:1148
rte_status_t rte_cast_i16_to_i8(int16_t in, int8_t *out)
Checked cast from int16_t to int8_t.
Definition rte_cast.c:124
rte_status_t rte_cast_u64_to_i32(uint64_t in, int32_t *out)
Checked cast from uint64_t to int32_t.
Definition rte_cast.c:877
rte_status_t rte_cast_size_to_i32(size_t in, int32_t *out)
Checked cast from size_t to int32_t.
Definition rte_cast.c:1003
rte_status_t rte_cast_i8_to_i64(int8_t in, int64_t *out)
Checked cast from int8_t to int64_t.
Definition rte_cast.c:33
void rte_safety_violation_report(rte_safety_violation_kind_t kind, const char *file, int32_t line, const char *message)
Reports one violation to the registered handler, if any.
@ RTE_SAFETY_VIOLATION_OUT_OF_RANGE
@ RTE_SAFETY_VIOLATION_OVERFLOW
rte_status_t
Common result/status codes.
Definition rte_status.h:27
@ RTE_STATUS_OK
Definition rte_status.h:28
@ RTE_STATUS_VALUE_OUT_OF_RANGE
Definition rte_status.h:39
@ RTE_STATUS_INVALID_PARAM
Definition rte_status.h:29
Checked integer casting between all fixed-width types and size_t (ADR-003). Every conversion in the c...
Opt-in notification hook for the three new safety primitives (safe pointer, checked integer arithmeti...