Skip to content

Commit 1ec3911

Browse files
ccdingAlex Elder
authored andcommitted
libceph: fix undefined behavior when using snprintf()
The variable "str" is used as both the source and destination in function snprintf(), which is undefined behavior based on C11. The original description in C11 is: "If copying takes place between objects that overlap, the behavior is undefined." And, the function of ceph_osdmap_state_str() is to return the osdmap state, so it should return "doesn't exist" when all the conditions are not satisfied. I fix it in this patch. [elder@inktank.com: shortened the commit message] Signed-off-by: Cong Ding <dinggnu@gmail.com> Reviewed-by: Alex Elder <elder@inktank.com>
1 parent c043064 commit 1ec3911

1 file changed

Lines changed: 10 additions & 18 deletions

File tree

‎net/ceph/osdmap.c‎

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,18 @@
1313

1414
char *ceph_osdmap_state_str(char *str, int len, int state)
1515
{
16-
int flag = 0;
17-
1816
if (!len)
19-
goto done;
20-
21-
*str = '\0';
22-
if (state) {
23-
if (state & CEPH_OSD_EXISTS) {
24-
snprintf(str, len, "exists");
25-
flag = 1;
26-
}
27-
if (state & CEPH_OSD_UP) {
28-
snprintf(str, len, "%s%s%s", str, (flag ? ", " : ""),
29-
"up");
30-
flag = 1;
31-
}
32-
} else {
17+
return str;
18+
19+
if ((state & CEPH_OSD_EXISTS) && (state & CEPH_OSD_UP))
20+
snprintf(str, len, "exists, up");
21+
else if (state & CEPH_OSD_EXISTS)
22+
snprintf(str, len, "exists");
23+
else if (state & CEPH_OSD_UP)
24+
snprintf(str, len, "up");
25+
else
3326
snprintf(str, len, "doesn't exist");
34-
}
35-
done:
27+
3628
return str;
3729
}
3830

0 commit comments

Comments
 (0)