Skip to content

Commit b901c05

Browse files
committed
Refactor conversion function
Use a size_t instead of int because it tracks the size of the string Cast to int to maintain the C stdlib API
1 parent 2bf68b3 commit b901c05

File tree

1 file changed

+19
-25
lines changed

1 file changed

+19
-25
lines changed

main/snprintf.c

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -568,11 +568,11 @@ typedef struct buf_area buffy;
568568
/*
569569
* Do format conversion placing the output in buffer
570570
*/
571-
static int format_converter(register buffy * odp, const char *fmt, va_list ap) /* {{{ */
571+
static size_t format_converter(register buffy * odp, const char *fmt, va_list ap) /* {{{ */
572572
{
573573
char *sp;
574574
char *bep;
575-
int cc = 0;
575+
size_t cc = 0;
576576
size_t i;
577577

578578
char *s = NULL;
@@ -1172,10 +1172,10 @@ static int format_converter(register buffy * odp, const char *fmt, va_list ap) /
11721172
/*
11731173
* This is the general purpose conversion function.
11741174
*/
1175-
static void strx_printv(int *ccp, char *buf, size_t len, const char *format, va_list ap) /* {{{ */
1175+
static size_t strx_printv(char *buf, size_t len, const char *format, va_list ap) /* {{{ */
11761176
{
11771177
buffy od;
1178-
int cc;
1178+
size_t cc;
11791179

11801180
/*
11811181
* First initialize the descriptor
@@ -1197,59 +1197,53 @@ static void strx_printv(int *ccp, char *buf, size_t len, const char *format, va_
11971197
if (len != 0 && od.nextb <= od.buf_end) {
11981198
*(od.nextb) = '\0';
11991199
}
1200-
if (ccp) {
1201-
*ccp = cc;
1202-
}
1200+
return cc;
12031201
}
12041202
/* }}} */
12051203

12061204
PHPAPI int ap_php_slprintf(char *buf, size_t len, const char *format,...) /* {{{ */
12071205
{
1208-
int cc;
1206+
size_t cc;
12091207
va_list ap;
12101208

12111209
va_start(ap, format);
1212-
strx_printv(&cc, buf, len, format, ap);
1210+
cc = strx_printv(buf, len, format, ap);
12131211
va_end(ap);
1214-
if ((size_t)cc >= len) {
1215-
cc = (int)len -1;
1212+
if (cc >= len) {
1213+
cc = len -1;
12161214
buf[cc] = '\0';
12171215
}
1218-
return cc;
1216+
return (int) cc;
12191217
}
12201218
/* }}} */
12211219

12221220
PHPAPI int ap_php_vslprintf(char *buf, size_t len, const char *format, va_list ap) /* {{{ */
12231221
{
1224-
int cc;
1225-
1226-
strx_printv(&cc, buf, len, format, ap);
1227-
if ((size_t)cc >= len) {
1228-
cc = (int)len -1;
1222+
size_t cc = strx_printv(buf, len, format, ap);
1223+
if (cc >= len) {
1224+
cc = len -1;
12291225
buf[cc] = '\0';
12301226
}
1231-
return cc;
1227+
return (int) cc;
12321228
}
12331229
/* }}} */
12341230

12351231
PHPAPI int ap_php_snprintf(char *buf, size_t len, const char *format,...) /* {{{ */
12361232
{
1237-
int cc;
1233+
size_t cc;
12381234
va_list ap;
12391235

12401236
va_start(ap, format);
1241-
strx_printv(&cc, buf, len, format, ap);
1237+
cc = strx_printv(buf, len, format, ap);
12421238
va_end(ap);
1243-
return (cc);
1239+
return (int) cc;
12441240
}
12451241
/* }}} */
12461242

12471243
PHPAPI int ap_php_vsnprintf(char *buf, size_t len, const char *format, va_list ap) /* {{{ */
12481244
{
1249-
int cc;
1250-
1251-
strx_printv(&cc, buf, len, format, ap);
1252-
return (cc);
1245+
size_t cc = strx_printv(buf, len, format, ap);
1246+
return (int) cc;
12531247
}
12541248
/* }}} */
12551249

0 commit comments

Comments
 (0)