Skip to content

Commit 56d6968

Browse files
committed
Baremetal stuff
1 parent e035a15 commit 56d6968

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
lines changed

libc/src/stdio/baremetal/printf.cpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,24 @@ LLVM_LIBC_FUNCTION(int, printf, (const char *__restrict format, ...)) {
4242
buffer, BUFF_SIZE, &stdout_write_hook, nullptr);
4343
printf_core::Writer<printf_core::WriteMode::FLUSH_TO_STREAM> writer(wb);
4444

45-
int retval = printf_core::printf_main(&writer, format, args);
45+
auto retval = printf_core::printf_main(&writer, format, args);
46+
if (retval.has_error()) {
47+
libc_errno = retval.error;
48+
return -1;
49+
}
4650

4751
int flushval = wb.overflow_write("");
48-
if (flushval != printf_core::WRITE_OK)
49-
retval = flushval;
50-
51-
return retval;
52+
if (flushval != printf_core::WRITE_OK) {
53+
libc_errno = -flushval;
54+
return -1;
55+
}
56+
57+
if (retval.value >= cpp::numeric_limits<int>::max()) {
58+
libc_errno = EOVERFLOW;
59+
return -1;
60+
}
61+
62+
return static_cast<int>(retval.value);
5263
}
5364

5465
} // namespace LIBC_NAMESPACE_DECL

libc/src/stdio/baremetal/vprintf.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,25 @@ LLVM_LIBC_FUNCTION(int, vprintf,
4040
buffer, BUFF_SIZE, &stdout_write_hook, nullptr);
4141
printf_core::Writer<printf_core::WriteMode::FLUSH_TO_STREAM> writer(wb);
4242

43-
int retval = printf_core::printf_main(&writer, format, args); // TODO baremetal stuff
43+
auto retval = printf_core::printf_main(&writer, format, args);
44+
if (retval.has_error()) {
45+
libc_errno = retval.error;
46+
return -1;
47+
}
4448

4549
int flushval = wb.overflow_write("");
46-
if (flushval != printf_core::WRITE_OK)
47-
retval = flushval;
50+
if (flushval != printf_core::WRITE_OK) {
51+
libc_errno = -flushval;
52+
return -1;
53+
54+
}
4855

49-
return retval;
56+
if (retval.value >= cpp::numeric_limits<int>::max()) {
57+
libc_errno = EOVERFLOW;
58+
return -1;
59+
}
60+
61+
return static_cast<int>(retval.value);
5062
}
5163

5264
} // namespace LIBC_NAMESPACE_DECL

libc/src/stdio/printf_core/vfprintf_internal.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ LIBC_INLINE PrintfResult vfprintf_internal(::FILE *__restrict stream,
7878
Writer writer(wb);
7979
internal::flockfile(stream);
8080
auto retval = printf_main(&writer, format, args);
81+
if (retval.has_error()) {
82+
internal::funlockfile(stream);
83+
return retval;
84+
}
8185
int flushval = wb.overflow_write("");
8286
if (flushval != WRITE_OK)
8387
retval.error = -flushval;

0 commit comments

Comments
 (0)