Skip to content

Commit 87c048a

Browse files
author
taras
committed
Cleanup comments, remove unnecessary constant
1 parent e55da54 commit 87c048a

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

src/picologging/formatter.cxx

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
#include "formatstyle.hxx"
77
#include "logrecord.hxx"
88

9-
constexpr const size_t MAX_FORMATED_ASCTIME_SIZE = 64;
10-
constexpr const size_t MAX_DATEFMT_WITH_MICROSECONDS_SIZE = 64;
9+
// Size of the temporary buffer on stack to format asctime.
10+
// 64 - is big enough.
11+
// For example: "2024-07-23 03:27:04.982856" - is just 29 bytes
12+
constexpr const size_t MAX_FORMATTED_ASCTIME_SIZE = 64;
1113

1214
PyObject* Formatter_new(PyTypeObject* type, PyObject* args, PyObject* kwds)
1315
{
@@ -77,10 +79,11 @@ int Formatter_init(Formatter *self, PyObject *args, PyObject *kwds){
7779
std::string_view dateFmtSV = self->dateFmtStr;
7880
self->dateFmtStrSize = dateFmtSV.size();
7981

80-
// We use temporary buffer on stack later to format %f before using standard strftime
81-
// This protects against buffer overflow
82-
// Breaching this will simply disable formatting of %f
83-
if (self->dateFmtStrSize < MAX_DATEFMT_WITH_MICROSECONDS_SIZE - 8)
82+
// Later we use temporary buffer allocated on stack to format %f before using standard strftime
83+
// This check protects against buffer overflow. If dateFmt is too large for the buffer
84+
// (bigger than in this check) then %f formatting will be disabled thus dateFmt will be passed
85+
// directly to strftime
86+
if (self->dateFmtStrSize <= MAX_FORMATTED_ASCTIME_SIZE - 4)
8487
self->dateFmtMicrosendsPos = dateFmtSV.find("%f");
8588
} else {
8689
self->dateFmtStr = nullptr;
@@ -110,15 +113,13 @@ PyObject* Formatter_format(Formatter *self, PyObject *record){
110113
std::time_t created = static_cast<std::time_t>(createdInt);
111114
std::tm *ct = localtime(&created);
112115

113-
// 64 - is big enough to fit any formatted asctime
114-
// For example: "2024-07-23 03:27:04.982856" - is just 29 bytes
115-
char buf[MAX_FORMATED_ASCTIME_SIZE];
116+
char buf[MAX_FORMATTED_ASCTIME_SIZE + 1];
116117

117118
if (self->dateFmt != Py_None){
118119
size_t len;
119120

120121
if (self->dateFmtMicrosendsPos != std::string_view::npos){
121-
char formatStrBuf[MAX_DATEFMT_WITH_MICROSECONDS_SIZE];
122+
char formatStrBuf[MAX_FORMATTED_ASCTIME_SIZE + 1];
122123
// Copy everything before %f
123124
memcpy(formatStrBuf, self->dateFmtStr, self->dateFmtMicrosendsPos);
124125
// Format microseconds

src/picologging/formatter.hxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ typedef struct {
1212
PyObject *dateFmt;
1313
PyObject *style;
1414
bool usesTime;
15-
size_t dateFmtMicrosendsPos; // If %f specified in dateFmt then points to '%' character, otherwise std::string_view::npos
15+
size_t dateFmtMicrosendsPos; // If %f is specified in dateFmt then points to '%' character, otherwise std::string_view::npos
1616
size_t dateFmtStrSize; // Size of the dateFmt without null terminator
1717
const char* dateFmtStr; // C-string, null terminated
1818
PyObject *_const_line_break;

0 commit comments

Comments
 (0)