Skip to content

Commit 88cf4fc

Browse files
Cynerdxiaoxiang781216
authored andcommitted
libs/libc: add limits checks
NuttX defines constants for the sizes of types just as constants directly in the code. The real values depend on compiler and target platform. It is possible that value declared by NuttX header is wrong compared to the compiler configuration. This is more likely in case of floating points. Not all compilers supported by NuttX provide required info through defines and thus it is more generic to specify limits as constants, but in case compiler provides them then it is a good idea to compare. This commit adds such set of comparisons for limits.h and float.h constants. The comparisons were tested with GCC and Clang. They will be ignored in case compiler doesn't provide them. Signed-off-by: Karel Kočí <kkoci@elektroline.cz>
1 parent 2d8b9de commit 88cf4fc

2 files changed

Lines changed: 197 additions & 0 deletions

File tree

libs/libc/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ include wctype/Make.defs
7373
include wqueue/Make.defs
7474
include fdt/Make.defs
7575

76+
CSRCS += limits_check.c
77+
7678
# Use double delim to fix windows native build and give an error:
7779
# makefile:132: *** target mode do not include“%”. stop.
7880
#

libs/libc/limits_check.c

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
/****************************************************************************
2+
* libs/libc/limits_check.c
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Licensed to the Apache Software Foundation (ASF) under one or more
7+
* contributor license agreements. See the NOTICE file distributed with
8+
* this work for additional information regarding copyright ownership. The
9+
* ASF licenses this file to you under the Apache License, Version 2.0 (the
10+
* "License"); you may not use this file except in compliance with the
11+
* License. You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18+
* License for the specific language governing permissions and limitations
19+
* under the License.
20+
*
21+
****************************************************************************/
22+
23+
/* NuttX prefers defining limits per architecture directly as constants
24+
* instead of using compiler provided definitions. That is to support
25+
* compilers that do not provide such definitions. On the other hand in case
26+
* compiler provides these definitions we should not deviate from them. This
27+
* file thus contains only checks of NuttX's defined limits against compiler
28+
* definitions if available.
29+
*/
30+
31+
/****************************************************************************
32+
* Included Files
33+
****************************************************************************/
34+
35+
#include <nuttx/config.h>
36+
37+
#include <assert.h>
38+
39+
#include <limits.h>
40+
#include <float.h>
41+
42+
/****************************************************************************
43+
* Pre-processor Prototypes
44+
****************************************************************************/
45+
46+
#ifdef static_assert
47+
# define static_assert_equal(COMPILER_DEF, DEF) \
48+
static_assert(COMPILER_DEF == DEF, "Compiler definition mismatch for " #DEF)
49+
#else
50+
# define DEFINITION_ASSERT(COMPILER_DEF, DEF)
51+
#endif
52+
53+
#ifdef __CHAR_BIT__
54+
static_assert_equal(__CHAR_BIT__, CHAR_BIT);
55+
#endif
56+
57+
#ifdef __SCHAR_MAX__
58+
static_assert_equal(__SCHAR_MAX__, SCHAR_MAX);
59+
#endif
60+
61+
#ifdef __SHRT_MAX__
62+
static_assert_equal(__SHRT_MAX__, SHRT_MAX);
63+
#endif
64+
65+
#ifdef __LONG_MAX__
66+
static_assert_equal(__LONG_MAX__, LONG_MAX);
67+
#endif
68+
69+
#ifdef __LONG_LONG_MAX__
70+
static_assert_equal(__LONG_LONG_MAX__, LLONG_MAX);
71+
#endif
72+
73+
#ifdef __UINTPTR_MAX__
74+
static_assert_equal(__UINTPTR_MAX__, UPTR_MAX);
75+
#endif
76+
77+
#ifdef __WCHAR_MAX__
78+
static_assert_equal(__WCHAR_MAX__, WCHAR_MAX);
79+
#endif
80+
81+
#ifdef __FLT_MANT_DIG__
82+
static_assert_equal(__FLT_MANT_DIG__, FLT_MANT_DIG);
83+
#endif
84+
85+
#ifdef __DBL_MANT_DIG__
86+
static_assert_equal(__DBL_MANT_DIG__, DBL_MANT_DIG);
87+
#endif
88+
89+
#ifdef __LDBL_MANT_DIG__
90+
static_assert_equal(__LDBL_MANT_DIG__, LDBL_MANT_DIG);
91+
#endif
92+
93+
#ifdef __FLT_DIG__
94+
static_assert_equal(__FLT_DIG__, FLT_DIG);
95+
#endif
96+
97+
#ifdef __DBL_DIG__
98+
static_assert_equal(__DBL_DIG__, DBL_DIG);
99+
#endif
100+
101+
#ifdef __LDBL_DIG__
102+
static_assert_equal(__LDBL_DIG__, LDBL_DIG);
103+
#endif
104+
105+
#ifdef __FLT_MIN_EXP__
106+
static_assert_equal(__FLT_MIN_EXP__, FLT_MIN_EXP);
107+
#endif
108+
109+
#ifdef __DBL_MIN_EXP__
110+
static_assert_equal(__DBL_MIN_EXP__, DBL_MIN_EXP);
111+
#endif
112+
113+
#ifdef __LDBL_MIN_EXP__
114+
static_assert_equal(__LDBL_MIN_EXP__, LDBL_MIN_EXP);
115+
#endif
116+
117+
#ifdef __FLT_MIN_10_EXP__
118+
static_assert_equal(__FLT_MIN_10_EXP__, FLT_MIN_10_EXP);
119+
#endif
120+
121+
#ifdef __DBL_MIN_10_EXP__
122+
static_assert_equal(__DBL_MIN_10_EXP__, DBL_MIN_10_EXP);
123+
#endif
124+
125+
#ifdef __LDBL_MIN_10_EXP__
126+
static_assert_equal(__LDBL_MIN_10_EXP__, LDBL_MIN_10_EXP);
127+
#endif
128+
129+
#ifdef __FLT_MAX_EXP__
130+
static_assert_equal(__FLT_MAX_EXP__, FLT_MAX_EXP);
131+
#endif
132+
133+
#ifdef __DBL_MAX_EXP__
134+
static_assert_equal(__DBL_MAX_EXP__, DBL_MAX_EXP);
135+
#endif
136+
137+
#ifdef __LDBL_MAX_EXP__
138+
static_assert_equal(__LDBL_MAX_EXP__, LDBL_MAX_EXP);
139+
#endif
140+
141+
#ifdef __FLT_MAX_10_EXP__
142+
static_assert_equal(__FLT_MAX_10_EXP__, FLT_MAX_10_EXP);
143+
#endif
144+
145+
#ifdef __DBL_MAX_10_EXP__
146+
static_assert_equal(__DBL_MAX_10_EXP__, DBL_MAX_10_EXP);
147+
#endif
148+
149+
#ifdef __LDBL_MAX_10_EXP__
150+
static_assert_equal(__LDBL_MAX_10_EXP__, LDBL_MAX_10_EXP);
151+
#endif
152+
153+
#ifdef __FLT_MAX__
154+
static_assert_equal(__FLT_MAX__, FLT_MAX);
155+
#endif
156+
157+
#ifdef __DBL_MAX__
158+
static_assert_equal(__DBL_MAX__, DBL_MAX);
159+
#endif
160+
161+
#ifdef __LDBL_MAX__
162+
static_assert_equal(__LDBL_MAX__, LDBL_MAX);
163+
#endif
164+
165+
#ifdef __FLT_EPSILON__
166+
static_assert_equal(__FLT_EPSILON__, FLT_EPSILON);
167+
#endif
168+
169+
#ifdef __DBL_EPSILON__
170+
static_assert_equal(__DBL_EPSILON__, DBL_EPSILON);
171+
#endif
172+
173+
#ifdef __LDBL_EPSILON__
174+
static_assert_equal(__LDBL_EPSILON__, LDBL_EPSILON);
175+
#endif
176+
177+
#ifdef __FLT_MIN__
178+
static_assert_equal(__FLT_MIN__, FLT_MIN);
179+
#endif
180+
181+
#ifdef __DBL_MIN__
182+
static_assert_equal(__DBL_MIN__, DBL_MIN);
183+
#endif
184+
185+
#ifdef __LDBL_MIN__
186+
static_assert_equal(__LDBL_MIN__, LDBL_MIN);
187+
#endif
188+
189+
/****************************************************************************
190+
* Private Functions
191+
****************************************************************************/
192+
193+
/****************************************************************************
194+
* Public Data
195+
****************************************************************************/

0 commit comments

Comments
 (0)