Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libc/src/time/strftime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ LLVM_LIBC_FUNCTION(size_t, strftime,
int ret = strftime_core::strftime_main(&writer, format, timeptr);
if (buffsz > 0) // if the buffsz is 0 the buffer may be a null pointer.
wb.buff[wb.buff_cur] = '\0';
return (ret < 0 || static_cast<size_t>(ret) > buffsz) ? 0 : ret;
return (ret < 0 || static_cast<size_t>(ret) >= buffsz) ? 0 : ret;
}

} // namespace LIBC_NAMESPACE_DECL
2 changes: 1 addition & 1 deletion libc/src/time/strftime_l.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ LLVM_LIBC_FUNCTION(size_t, strftime_l,
int ret = strftime_core::strftime_main(&writer, format, timeptr);
if (buffsz > 0) // if the buffsz is 0 the buffer may be a null pointer.
wb.buff[wb.buff_cur] = '\0';
return (ret < 0 || static_cast<size_t>(ret) > buffsz) ? 0 : ret;
return (ret < 0 || static_cast<size_t>(ret) >= buffsz) ? 0 : ret;
}

} // namespace LIBC_NAMESPACE_DECL
20 changes: 20 additions & 0 deletions libc/test/src/time/strftime_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2326,3 +2326,23 @@ TEST(LlvmLibcStrftimeTest, TimeFormatFullDateTime) {
// size_t written = 0;
// SimplePaddedNum spn;
// }

TEST(LlvmLibcStrftimeTest, BufferTooSmall) {
struct tm time;
char buffer[1];

time.tm_year = get_adjusted_year(2025);
time.tm_mon = 10;
time.tm_mday = 24;

size_t written =
LIBC_NAMESPACE::strftime(buffer, sizeof(buffer), "%F", &time);
EXPECT_EQ(written, size_t{0});

char buffer2[10];

// The string "2025-11-24" is 10 chars,
// so strftime needs 10 + 1 bytes to write the string and the null terminator.
written = LIBC_NAMESPACE::strftime(buffer, sizeof(buffer2), "%F", &time);
EXPECT_EQ(written, size_t{0});
}
Loading