Skip to content

Commit 89e5db4

Browse files
committed
Format with clang_format
1 parent f070069 commit 89e5db4

1 file changed

Lines changed: 66 additions & 62 deletions

File tree

libc-top-half/musl/src/mman/memfd_create.c

Lines changed: 66 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -10,80 +10,84 @@
1010

1111
#define MEMFD_NAME_MAX 249
1212

13-
static int create_unlinked_temp(char *template_path, unsigned int flags)
14-
{
15-
int fd;
16-
if (flags & MFD_CLOEXEC) {
17-
fd = mkostemp(template_path, O_CLOEXEC);
18-
} else {
19-
fd = mkstemp(template_path);
20-
}
21-
if (fd < 0) return -1;
13+
static int create_unlinked_temp(char *template_path, unsigned int flags) {
14+
int fd;
15+
if (flags & MFD_CLOEXEC) {
16+
fd = mkostemp(template_path, O_CLOEXEC);
17+
} else {
18+
fd = mkstemp(template_path);
19+
}
20+
if (fd < 0)
21+
return -1;
2222

23-
if (unlink(template_path) != 0) {
24-
int saved_errno = errno;
25-
close(fd);
26-
errno = saved_errno;
27-
return -1;
28-
}
23+
if (unlink(template_path) != 0) {
24+
int saved_errno = errno;
25+
close(fd);
26+
errno = saved_errno;
27+
return -1;
28+
}
2929

30-
return fd;
30+
return fd;
3131
}
3232

3333
// Returns the length of the name without the null character.
34-
// If the returned length == dst_len, the src was too long. dst is not null terminated in this case.
35-
static int sanitize_name(char *dst, size_t dst_len, const char *src)
36-
{
37-
size_t i = 0;
34+
// If the returned length == dst_len, the src was too long. dst is not null
35+
// terminated in this case.
36+
static int sanitize_name(char *dst, size_t dst_len, const char *src) {
37+
size_t i = 0;
3838

39-
while (i < dst_len && src[i] != '\0') {
40-
unsigned char c = (unsigned char)src[i];
41-
if (isalnum(c) || c == '_' || c == '-' || c == '.') {
42-
dst[i] = (char)c;
43-
} else {
44-
dst[i] = '_';
45-
}
46-
i++;
47-
}
39+
while (i < dst_len && src[i] != '\0') {
40+
unsigned char c = (unsigned char)src[i];
41+
if (isalnum(c) || c == '_' || c == '-' || c == '.') {
42+
dst[i] = (char)c;
43+
} else {
44+
dst[i] = '_';
45+
}
46+
i++;
47+
}
4848

49-
return i;
49+
return i;
5050
}
5151

52+
int memfd_create(const char *name, unsigned int flags) {
53+
if (flags & ~(MFD_CLOEXEC | MFD_ALLOW_SEALING | MFD_HUGETLB)) {
54+
errno = EINVAL;
55+
return -1;
56+
}
5257

53-
int memfd_create(const char *name, unsigned int flags)
54-
{
55-
if (flags & ~(MFD_CLOEXEC | MFD_ALLOW_SEALING | MFD_HUGETLB)) {
56-
errno = EINVAL;
57-
return -1;
58-
}
58+
if (flags & (MFD_HUGETLB | MFD_ALLOW_SEALING)) {
59+
/* Seals are not currently supported by the WASIX fcntl subset. */
60+
/* hugetlbfs is not currently supported by the WASIX filesystem subset. */
61+
errno = ENOTSUP;
62+
return -1;
63+
}
5964

60-
if (flags & (MFD_HUGETLB | MFD_ALLOW_SEALING)) {
61-
/* Seals are not currently supported by the WASIX fcntl subset. */
62-
/* hugetlbfs is not currently supported by the WASIX filesystem subset. */
63-
errno = ENOTSUP;
64-
return -1;
65-
}
65+
if (name == NULL) {
66+
errno = EFAULT;
67+
return -1;
68+
}
6669

67-
if (name == NULL) {
68-
errno = EFAULT;
69-
return -1;
70-
}
70+
// Construct a string like "/tmp/.memfd-<name>-XXXXXX"
71+
char tmp_template[sizeof("/tmp/.memfd-") - 1 + MEMFD_NAME_MAX +
72+
sizeof("-XXXXXX") - 1 + 1];
73+
memcpy(tmp_template, "/tmp/.memfd-", sizeof("/tmp/.memfd-") - 1);
74+
int name_len = sanitize_name(tmp_template + sizeof("/tmp/.memfd-") - 1,
75+
MEMFD_NAME_MAX + 1, name);
76+
if (name_len > MEMFD_NAME_MAX) {
77+
errno = EINVAL;
78+
return -1;
79+
}
80+
memcpy(tmp_template + sizeof("/tmp/.memfd-") - 1 + name_len, "-XXXXXX",
81+
sizeof("-XXXXXX"));
7182

72-
// Construct a string like "/tmp/.memfd-<name>-XXXXXX"
73-
char tmp_template[sizeof("/tmp/.memfd-")-1 + MEMFD_NAME_MAX + sizeof("-XXXXXX")-1 + 1];
74-
memcpy(tmp_template, "/tmp/.memfd-", sizeof("/tmp/.memfd-") -1);
75-
int name_len = sanitize_name(tmp_template + sizeof("/tmp/.memfd-") - 1, MEMFD_NAME_MAX + 1, name);
76-
if (name_len > MEMFD_NAME_MAX) {
77-
errno = EINVAL;
78-
return -1;
79-
}
80-
memcpy(tmp_template + sizeof("/tmp/.memfd-") - 1 + name_len, "-XXXXXX", sizeof("-XXXXXX"));
83+
return create_unlinked_temp(tmp_template, flags);
84+
if (fd >= 0)
85+
return fd;
8186

82-
return create_unlinked_temp(tmp_template, flags);
83-
if (fd >= 0) return fd;
84-
85-
// If that failed, try ".memfd-<name>-XXXXXX" in the current working directory.
86-
// Restore the -XXXXXX placeholder because it got overwritten by the previous call to mkstemp.
87-
memcpy(tmp_template + sizeof("/tmp/.memfd-") - 1 + name_len, "-XXXXXX", sizeof("-XXXXXX"));
88-
return create_unlinked_temp(tmp_template + sizeof("/tmp/") - 1, flags);
87+
// If that failed, try ".memfd-<name>-XXXXXX" in the current working
88+
// directory. Restore the -XXXXXX placeholder because it got overwritten by
89+
// the previous call to mkstemp.
90+
memcpy(tmp_template + sizeof("/tmp/.memfd-") - 1 + name_len, "-XXXXXX",
91+
sizeof("-XXXXXX"));
92+
return create_unlinked_temp(tmp_template + sizeof("/tmp/") - 1, flags);
8993
}

0 commit comments

Comments
 (0)