Skip to content

Commit 65a6056

Browse files
committed
ndctl: clamp dimm formats
Static analysis warns about unbounded values of 'formats' being passed to calloc. Clamp to the known allowed values. This also updates the max() macro to avoid 'variable shadowed' warnings. Signed-off-by: Dan Williams <[email protected]>
1 parent ef230a1 commit 65a6056

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

ccan/minmax/minmax.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@
3232

3333
#define max(a, b) \
3434
({ \
35-
typeof(a) _a = (a); \
36-
typeof(b) _b = (b); \
37-
MINMAX_ASSERT_COMPATIBLE(typeof(_a), typeof(_b)); \
38-
_a > _b ? _a : _b; \
35+
typeof(a) __a = (a); \
36+
typeof(b) __b = (b); \
37+
MINMAX_ASSERT_COMPATIBLE(typeof(__a), typeof(__b)); \
38+
__a > __b ? __a : __b; \
3939
})
4040

4141
#define clamp(v, f, c) (max(min((v), (c)), (f)))

ndctl/lib/libndctl.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1170,7 +1170,7 @@ static void *add_dimm(void *parent, int id, const char *dimm_base)
11701170
if (sysfs_read_attr(ctx, path, buf) < 0)
11711171
formats = 1;
11721172
else
1173-
formats = strtoul(buf, NULL, 0);
1173+
formats = clamp(strtoul(buf, NULL, 0), 1UL, 2UL);
11741174

11751175
dimm = calloc(1, sizeof(*dimm) + sizeof(int) * formats);
11761176
if (!dimm)

0 commit comments

Comments
 (0)