Skip to content

Commit 5dd7f08

Browse files
authored
improvement: improve printf format (#3773)
1. add __attribute__ format for sw_snprintf 2. change SessionId and TaskId to long
1 parent 3096a80 commit 5dd7f08

File tree

11 files changed

+25
-24
lines changed

11 files changed

+25
-24
lines changed

ext-src/swoole_http2_server.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,7 @@ int swoole_http2_server_parse(Http2Session *client, const char *buf) {
798798
if (client->deflater) {
799799
int ret = nghttp2_hd_deflate_change_table_size(client->deflater, value);
800800
if (ret != 0) {
801-
swWarn("nghttp2_hd_deflate_change_table_size() failed, errno=%s, errmsg=%s",
801+
swWarn("nghttp2_hd_deflate_change_table_size() failed, errno=%d, errmsg=%s",
802802
ret,
803803
nghttp2_strerror(ret));
804804
return SW_ERR;

ext-src/swoole_http_request.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ static int http_request_on_header_value(swoole_http_parser *parser, const char *
433433
swServer *serv = (swServer *) ctx->private_data;
434434
swConnection *conn = serv->get_connection_by_session_id(ctx->fd);
435435
if (!conn) {
436-
swoole_error_log(SW_LOG_NOTICE, SW_ERROR_SESSION_CLOSED, "session[%lld] is closed", ctx->fd);
436+
swoole_error_log(SW_LOG_NOTICE, SW_ERROR_SESSION_CLOSED, "session[%ld] is closed", ctx->fd);
437437
efree(header_name);
438438
return -1;
439439
}

ext-src/swoole_http_response.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,7 +1127,7 @@ static PHP_METHOD(swoole_http_response, ping) {
11271127
RETURN_FALSE;
11281128
}
11291129
if (UNEXPECTED(!ctx->http2)) {
1130-
php_swoole_fatal_error(E_WARNING, "fd[%" PRId64 "] is not a HTTP2 conncetion", ctx->fd);
1130+
php_swoole_fatal_error(E_WARNING, "fd[%ld] is not a HTTP2 conncetion", ctx->fd);
11311131
RETURN_FALSE;
11321132
}
11331133
SW_CHECK_RETURN(swoole_http2_server_ping(ctx));
@@ -1140,7 +1140,7 @@ static PHP_METHOD(swoole_http_response, goaway) {
11401140
RETURN_FALSE;
11411141
}
11421142
if (UNEXPECTED(!ctx->http2)) {
1143-
php_swoole_fatal_error(E_WARNING, "fd[%" PRId64 "] is not a HTTP2 conncetion", ctx->fd);
1143+
php_swoole_fatal_error(E_WARNING, "fd[%ld] is not a HTTP2 conncetion", ctx->fd);
11441144
RETURN_FALSE;
11451145
}
11461146
zend_long error_code = SW_HTTP2_ERROR_NO_ERROR;
@@ -1174,7 +1174,7 @@ static PHP_METHOD(swoole_http_response, push) {
11741174
RETURN_FALSE;
11751175
}
11761176
if (UNEXPECTED(!ctx->co_socket || !ctx->upgrade)) {
1177-
php_swoole_fatal_error(E_WARNING, "fd[%" PRId64 "] is not a websocket conncetion", ctx->fd);
1177+
php_swoole_fatal_error(E_WARNING, "fd[%ld] is not a websocket conncetion", ctx->fd);
11781178
RETURN_FALSE;
11791179
}
11801180

@@ -1225,7 +1225,7 @@ static PHP_METHOD(swoole_http_response, recv) {
12251225
RETURN_FALSE;
12261226
}
12271227
if (UNEXPECTED(!ctx->co_socket || !ctx->upgrade)) {
1228-
php_swoole_fatal_error(E_WARNING, "fd[%" PRId64 "] is not a websocket conncetion", ctx->fd);
1228+
php_swoole_fatal_error(E_WARNING, "fd[%ld] is not a websocket conncetion", ctx->fd);
12291229
RETURN_FALSE;
12301230
}
12311231

ext-src/swoole_redis_coro.cc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2750,7 +2750,7 @@ static PHP_METHOD(swoole_redis_coro, sRandMember) {
27502750
SW_REDIS_COMMAND_ARGV_FILL("SRANDMEMBER", 11);
27512751
SW_REDIS_COMMAND_ARGV_FILL(key, key_len);
27522752
if (argc == 3) {
2753-
buf_len = sw_snprintf(buf, sizeof(buf), "%ld", count);
2753+
buf_len = sw_snprintf(buf, sizeof(buf), "%" PRId64 "", count);
27542754
SW_REDIS_COMMAND_ARGV_FILL((char *) buf, buf_len);
27552755
}
27562756
redis_request(redis, argc, argv, argvlen, return_value);
@@ -3011,9 +3011,9 @@ static PHP_METHOD(swoole_redis_coro, zRange) {
30113011
SW_REDIS_COMMAND_ARGV_FILL(key, key_len)
30123012
char buf[32];
30133013
size_t buf_len;
3014-
buf_len = sw_snprintf(buf, sizeof(buf), "%ld", start);
3014+
buf_len = sw_snprintf(buf, sizeof(buf), "%" PRId64 "", start);
30153015
SW_REDIS_COMMAND_ARGV_FILL((char *) buf, buf_len)
3016-
buf_len = sw_snprintf(buf, sizeof(buf), "%ld", end);
3016+
buf_len = sw_snprintf(buf, sizeof(buf), "%" PRId64 "", end);
30173017
SW_REDIS_COMMAND_ARGV_FILL((char *) buf, buf_len)
30183018
if (ws) {
30193019
SW_REDIS_COMMAND_ARGV_FILL("WITHSCORES", 10)
@@ -3048,9 +3048,9 @@ static PHP_METHOD(swoole_redis_coro, zRevRange) {
30483048
SW_REDIS_COMMAND_ARGV_FILL(key, key_len)
30493049
char buf[32];
30503050
size_t buf_len;
3051-
buf_len = sw_snprintf(buf, sizeof(buf), "%ld", start);
3051+
buf_len = sw_snprintf(buf, sizeof(buf), "%" PRId64 "", start);
30523052
SW_REDIS_COMMAND_ARGV_FILL((char *) buf, buf_len)
3053-
buf_len = sw_snprintf(buf, sizeof(buf), "%ld", end);
3053+
buf_len = sw_snprintf(buf, sizeof(buf), "%" PRId64 "", end);
30543054
SW_REDIS_COMMAND_ARGV_FILL((char *) buf, buf_len)
30553055
if (ws) {
30563056
SW_REDIS_COMMAND_ARGV_FILL("WITHSCORES", 10)
@@ -3700,7 +3700,7 @@ static PHP_METHOD(swoole_redis_coro, zPopMin) {
37003700
SW_REDIS_COMMAND_ARGV_FILL("ZPOPMIN", 7);
37013701
SW_REDIS_COMMAND_ARGV_FILL(key, key_len);
37023702
if (argc == 3) {
3703-
buf_len = sw_snprintf(buf, sizeof(buf), "%ld", count);
3703+
buf_len = sw_snprintf(buf, sizeof(buf), "%" PRId64 "", count);
37043704
SW_REDIS_COMMAND_ARGV_FILL((char *) buf, buf_len);
37053705
}
37063706
redis_request(redis, argc, argv, argvlen, return_value);
@@ -3724,7 +3724,7 @@ static PHP_METHOD(swoole_redis_coro, zPopMax) {
37243724
SW_REDIS_COMMAND_ARGV_FILL("ZPOPMAX", 7);
37253725
SW_REDIS_COMMAND_ARGV_FILL(key, key_len);
37263726
if (argc == 3) {
3727-
buf_len = sw_snprintf(buf, sizeof(buf), "%ld", count);
3727+
buf_len = sw_snprintf(buf, sizeof(buf), "%" PRId64 "", count);
37283728
SW_REDIS_COMMAND_ARGV_FILL((char *) buf, buf_len);
37293729
}
37303730
redis_request(redis, argc, argv, argvlen, return_value);

ext-src/swoole_server.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1424,7 +1424,7 @@ static int php_swoole_onFinish(Server *serv, EventData *req) {
14241424
}
14251425
}
14261426
if (task_index < 0) {
1427-
php_swoole_fatal_error(E_WARNING, "task[%" PRId64 "] is invalid", task_id);
1427+
php_swoole_fatal_error(E_WARNING, "task[%ld] is invalid", task_id);
14281428
goto _fail;
14291429
}
14301430
(void) add_index_zval(result, task_index, zdata);

ext-src/swoole_websocket_server.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -729,15 +729,15 @@ void php_swoole_websocket_server_minit(int module_number) {
729729

730730
static sw_inline bool swoole_websocket_server_push(Server *serv, SessionId fd, String *buffer) {
731731
if (sw_unlikely(fd <= 0)) {
732-
php_swoole_fatal_error(E_WARNING, "fd[%" PRId64 "] is invalid", fd);
732+
php_swoole_fatal_error(E_WARNING, "fd[%ld] is invalid", fd);
733733
return false;
734734
}
735735

736736
Connection *conn = serv->get_connection_by_session_id(fd);
737737
if (!conn || conn->websocket_status < WEBSOCKET_STATUS_HANDSHAKE) {
738738
swoole_set_last_error(SW_ERROR_WEBSOCKET_UNCONNECTED);
739739
php_swoole_fatal_error(
740-
E_WARNING, "the connected client of connection[%" PRId64 "] is not a websocket client or closed", fd);
740+
E_WARNING, "the connected client of connection[%ld] is not a websocket client or closed", fd);
741741
return false;
742742
}
743743

include/swoole.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,8 @@ struct swMemoryPool;
234234
#endif
235235

236236
/** always return less than size, zero termination */
237-
size_t sw_snprintf(char *buf, size_t size, const char *format, ...);
237+
size_t sw_snprintf(char *buf, size_t size, const char *format, ...)
238+
__attribute__ ((format (printf, 3, 4)));
238239
size_t sw_vsnprintf(char *buf, size_t size, const char *format, va_list args);
239240

240241
#define sw_memset_zero(s, n) memset(s, '\0', n)
@@ -476,8 +477,8 @@ struct Event {
476477
network::Socket *socket;
477478
};
478479

479-
typedef int64_t SessionId;
480-
typedef int64_t TaskId;
480+
typedef long SessionId;
481+
typedef long TaskId;
481482

482483
struct DataHead {
483484
SessionId fd;

src/core/log.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ void Logger::put(int level, const char *content, size_t length) {
237237
if (date_with_microseconds) {
238238
auto now_us = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch()).count();
239239
l_data_str +=
240-
sw_snprintf(date_str + l_data_str, SW_LOG_DATE_STRLEN - l_data_str, "<.%ld>", now_us - now_sec * 1000000);
240+
sw_snprintf(date_str + l_data_str, SW_LOG_DATE_STRLEN - l_data_str, "<.%lld>", now_us - now_sec * 1000000);
241241
}
242242

243243
char process_flag = '@';

src/network/socket.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@ bool Socket::ssl_verify(bool allow_self_signed) {
730730
default:
731731
swoole_error_log(SW_LOG_NOTICE,
732732
SW_ERROR_SSL_VERIFY_FAILED,
733-
"can not verify peer from fd#%d with error#%d: %s",
733+
"can not verify peer from fd#%d with error#%ld: %s",
734734
fd,
735735
err,
736736
X509_verify_cert_error_string(err));

src/protocol/http.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ bool Server::select_static_handler(http_server::Request *request, Connection *co
183183

184184
// Use tcp_nopush to improve sending efficiency
185185
conn->socket->cork();
186-
186+
187187
// Send HTTP header
188188
send_to_connection(&response);
189189

@@ -709,7 +709,7 @@ void Server::destroy_http_request(Connection *conn) {
709709
static void protocol_status_error(Socket *socket, Connection *conn) {
710710
swoole_error_log(SW_LOG_WARNING,
711711
SW_ERROR_PROTOCOL_ERROR,
712-
"unexpected protocol status of session#%u<%s:%d>",
712+
"unexpected protocol status of session#%ld<%s:%d>",
713713
conn->session_id,
714714
conn->info.get_ip(),
715715
conn->info.get_port());

0 commit comments

Comments
 (0)