Skip to content

Commit

Permalink
libutil/stdlog: use size_t not int
Browse files Browse the repository at this point in the history
Problem: several functions in the internal stdlog class use int
types where size_t would be more appropriate.

Switch to size_t.
Update unit test.
Update users.
  • Loading branch information
garlick committed Dec 11, 2024
1 parent af5e55d commit 174a1d4
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 44 deletions.
5 changes: 3 additions & 2 deletions src/broker/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,8 @@ static void log_fp (FILE *fp, int flags, const char *buf, int len)
{
struct stdlog_header hdr;
const char *msg;
int msglen, severity;
size_t msglen;
int severity;
uint32_t nodeid;

if (fp) {
Expand All @@ -453,7 +454,7 @@ static void log_fp (FILE *fp, int flags, const char *buf, int len)
hdr.appname,
stdlog_severity_to_string (severity),
nodeid,
msglen,
(int)msglen,
msg);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/broker/shutdown.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ static int forward_logbuf (flux_t *h,
{
struct stdlog_header hdr;
const char *txt;
int txtlen;
size_t txtlen;
char buf[FLUX_MAX_LOGBUF];
int loglevel;

Expand All @@ -86,7 +86,7 @@ static int forward_logbuf (flux_t *h,
hdr.appname,
stdlog_severity_to_string (STDLOG_SEVERITY (hdr.pri)),
strtoul (hdr.hostname, NULL, 10),
txtlen,
(int)txtlen,
txt) >= sizeof (buf))
return 0;
return flux_respond_pack (h, request, "{s:s}", "log", buf);
Expand Down
5 changes: 3 additions & 2 deletions src/broker/test/overlay.c
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,8 @@ void diag_logger (const char *buf, int len, void *arg)
{
struct stdlog_header hdr;
const char *msg;
int msglen, severity;
size_t msglen;
int severity;
char *s;

if (stdlog_decode (buf, len, &hdr, NULL, NULL, &msg, &msglen) < 0)
Expand All @@ -692,7 +693,7 @@ void diag_logger (const char *buf, int len, void *arg)
if (asprintf (&s,
"%s: %.*s\n",
stdlog_severity_to_string (severity),
msglen,
(int)msglen,
msg) < 0)
BAIL_OUT ("asprintf failed");
diag (s);
Expand Down
5 changes: 3 additions & 2 deletions src/broker/test/runat.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,8 @@ void diag_logger (const char *buf, int len, void *arg)
{
struct stdlog_header hdr;
const char *msg;
int msglen, severity;
size_t msglen;
int severity;
char *s;

if (stdlog_decode (buf, len, &hdr, NULL, NULL, &msg, &msglen) < 0)
Expand All @@ -254,7 +255,7 @@ void diag_logger (const char *buf, int len, void *arg)
if (asprintf (&s,
"%s: %.*s\n",
stdlog_severity_to_string (severity),
msglen,
(int)msglen,
msg) < 0)
BAIL_OUT ("asprintf failed");
diag (s);
Expand Down
5 changes: 3 additions & 2 deletions src/cmd/builtin/dmesg.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@ void dmesg_print (struct dmesg_ctx *ctx,
{
struct stdlog_header hdr;
const char *msg;
int msglen, severity;
size_t msglen;
int severity;
uint32_t nodeid;

if (stdlog_decode (buf, len, &hdr, NULL, NULL, &msg, &msglen) < 0)
Expand All @@ -225,7 +226,7 @@ void dmesg_print (struct dmesg_ctx *ctx,
nodeid,
dmesg_color_reset (ctx),
severity_color (ctx, severity),
msglen,
(int)msglen,
msg,
dmesg_color_reset (ctx));
}
Expand Down
34 changes: 17 additions & 17 deletions src/common/libutil/stdlog.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ static int next_str (char **p, char **result)
}

static int next_structured_data (const char *buf,
int len,
int *offp,
size_t len,
size_t *offp,
const char **sp,
int *slenp)
size_t *slenp)
{
int off = *offp;
int this = *offp;
size_t off = *offp;
size_t this = *offp;
int level = 0;

while (off < len) {
Expand All @@ -82,17 +82,17 @@ static int next_structured_data (const char *buf,
}

int stdlog_decode (const char *buf,
int len,
size_t len,
struct stdlog_header *hdr,
const char **sdp,
int *sdlenp,
size_t *sdlenp,
const char **msgp,
int *msglenp)
size_t *msglenp)
{
int hdr_len = STDLOG_MAX_HEADER;
size_t hdr_len = STDLOG_MAX_HEADER;
char *p = &hdr->buf[0];
const char *sd;
int off, sdlen;
size_t off, sdlen;

if (hdr_len > len)
hdr_len = len;
Expand Down Expand Up @@ -133,14 +133,14 @@ int stdlog_decode (const char *buf,
return 0;
}

char *stdlog_split_message (const char *buf, int *len, const char *sep)
char *stdlog_split_message (const char *buf, size_t *len, const char *sep)
{
struct stdlog_header hdr;
const char *msg;
int msglen;
int off;
size_t msglen;
size_t off;
char *xtra;
int xtra_len;
size_t xtra_len;

if (stdlog_decode (buf, *len, &hdr, NULL, NULL, &msg, &msglen) < 0)
return NULL;
Expand All @@ -166,7 +166,7 @@ char *stdlog_split_message (const char *buf, int *len, const char *sep)
}

int stdlog_vencodef (char *buf,
int len,
size_t len,
struct stdlog_header *hdr,
const char *sd,
const char *fmt,
Expand Down Expand Up @@ -209,7 +209,7 @@ int stdlog_vencodef (char *buf,
}

int stdlog_encodef (char *buf,
int len,
size_t len,
struct stdlog_header *hdr,
const char *sd,
const char *fmt,
Expand All @@ -225,7 +225,7 @@ int stdlog_encodef (char *buf,
}

int stdlog_encode (char *buf,
int len,
size_t len,
struct stdlog_header *hdr,
const char *sd,
const char *msg)
Expand Down
14 changes: 7 additions & 7 deletions src/common/libutil/stdlog.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,28 +44,28 @@ struct stdlog_header {
};

int stdlog_decode (const char *buf,
int len,
size_t len,
struct stdlog_header *hdr,
const char **sd,
int *sdlen,
size_t *sdlen,
const char **msg,
int *msglen);
size_t *msglen);

int stdlog_encode (char *buf,
int len,
size_t len,
struct stdlog_header *hdr,
const char *sd,
const char *msg);

int stdlog_vencodef (char *buf,
int len,
size_t len,
struct stdlog_header *hdr,
const char *sd,
const char *fmt,
va_list ap);

int stdlog_encodef (char *buf,
int len,
size_t len,
struct stdlog_header *hdr,
const char *sd,
const char *fmt,
Expand All @@ -77,7 +77,7 @@ int stdlog_encodef (char *buf,
* Returns NULL if no 'sep' chars or on alloc failure.
* If non-NULL, caller must free returned value.
*/
char *stdlog_split_message (const char *buf, int *len, const char *sep);
char *stdlog_split_message (const char *buf, size_t *len, const char *sep);

void stdlog_init (struct stdlog_header *hdr);

Expand Down
25 changes: 15 additions & 10 deletions src/common/libutil/test/stdlog.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,19 @@ void test_split (void)
char buf[2048];
char *xtra;
struct stdlog_header hdr;
int len;
int msglen;
int n;
size_t len;
size_t msglen;
const char *msg;

stdlog_init (&hdr);

/* orig=foo\nbar\nbaz */
len = stdlog_encode (buf, sizeof (buf), &hdr, STDLOG_NILVALUE,
n = stdlog_encode (buf, sizeof (buf), &hdr, STDLOG_NILVALUE,
"foo\nbar\nbaz");
ok (len >= 0,
ok (n >= 0,
"stdlog_encode encoded foo\\nbar\\nbaz");
len = n;
xtra = stdlog_split_message (buf, &len, "\r\n");
ok (xtra != NULL && streq (xtra, "bar\nbaz"),
"stdlog_split_message got bar\\nbaz");
Expand All @@ -67,9 +69,10 @@ void test_split (void)
"and truncated orig message to foo");

/* xtra=bar\nbaz */
len = stdlog_encode (buf, sizeof (buf), &hdr, STDLOG_NILVALUE, xtra);
ok (len >= 0,
n = stdlog_encode (buf, sizeof (buf), &hdr, STDLOG_NILVALUE, xtra);
ok (n >= 0,
"stdlog_encode encoded bar\\nbaz");
len = n;
free (xtra);
xtra = stdlog_split_message (buf, &len, "\r\n");
ok (xtra != NULL && streq (xtra, "baz"),
Expand All @@ -80,9 +83,10 @@ void test_split (void)
"and truncated orig message to bar");

/* xtra=baz */
len = stdlog_encode (buf, sizeof (buf), &hdr, STDLOG_NILVALUE, xtra);
ok (len >= 0,
n = stdlog_encode (buf, sizeof (buf), &hdr, STDLOG_NILVALUE, xtra);
ok (n >= 0,
"stdlog_encode encoded baz");
len = n;
free (xtra);
xtra = stdlog_split_message (buf, &len, "\r\n");
ok (xtra == NULL,
Expand All @@ -93,9 +97,10 @@ int main(int argc, char** argv)
{
char buf[2048];
struct stdlog_header hdr, cln;
int n, len;
int n;
size_t len;
const char *sd, *msg;
int sdlen, msglen;
size_t sdlen, msglen;

plan (NO_PLAN);

Expand Down

0 comments on commit 174a1d4

Please sign in to comment.