Skip to content

Commit da33c75

Browse files
u5surfclaude
andcommitted
Fix GitHub Copilot suggestions: replace magic numbers and add bounds checking
- Replace hardcoded constants 3 and 4 with named constants - Add NGX_HTTP_VHOST_TRAFFIC_STATUS_UPSTREAM_PREFIX_LEN and NGX_HTTP_VHOST_TRAFFIC_STATUS_UPSTREAM_KEY_LEN - Add bounds checking before accessing vtsn->data to prevent buffer overflow - Fix indentation and syntax issues in ngx_http_vhost_traffic_status_display_ug_host function 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 94648d0 commit da33c75

File tree

2 files changed

+28
-22
lines changed

2 files changed

+28
-22
lines changed

src/ngx_http_vhost_traffic_status_display_json.c

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "ngx_http_upstream_check_module.h"
1515
#endif
1616

17+
1718
#if (nginx_version > 1027003) && defined(NGX_HTTP_UPSTREAM_MODIFY) && !defined(NGX_HTTP_UPSTREAM_CHECK)
1819
static u_char *
1920
ngx_http_vhost_traffic_status_display_ug_host(
@@ -1047,31 +1048,33 @@ ngx_http_vhost_traffic_status_display_ug_host(
10471048
if (node != sentinel) {
10481049
vtsn = (ngx_http_vhost_traffic_status_node_t *) &node->color;
10491050
if (vtsn->stat_upstream.type == NGX_HTTP_VHOST_TRAFFIC_STATUS_UPSTREAM_UG) {
1050-
rc = ngx_memn2cmp(host.data, vtsn->data+3, host.len, (size_t) host.len);
1051-
if (rc == 0) {
1052-
usn.name.data = vtsn->data + 3 + host.len + 1;
1053-
usn.name.len = vtsn->len - host.len - 4;
1054-
usn.weight = 0;
1055-
usn.max_fails = 0;
1056-
usn.fail_timeout = 0;
1057-
usn.backup = 0;
1058-
usn.down = 0;
1059-
while (peers != NULL) {
1060-
ngx_http_upstream_rr_peers_rlock(peers);
1061-
for (peer = peers->peer; peer; peer = peer->next) {
1062-
rc = ngx_memn2cmp(peer->name.data, usn.name.data, peer->name.len, (size_t) usn.name.len);
1063-
if (rc == 0) {
1064-
usn.weight = peer->weight;
1065-
usn.max_fails = peer->max_fails;
1066-
usn.fail_timeout = peer->fail_timeout;
1067-
usn.backup = 0;
1068-
usn.down = (peer->fails >= peer->max_fails || peer->down);
1051+
if (vtsn->len >= NGX_HTTP_VHOST_TRAFFIC_STATUS_UPSTREAM_KEY_LEN + host.len) {
1052+
rc = ngx_memn2cmp(host.data, vtsn->data + NGX_HTTP_VHOST_TRAFFIC_STATUS_UPSTREAM_PREFIX_LEN, host.len, (size_t) host.len);
1053+
if (rc == 0) {
1054+
usn.name.data = vtsn->data + NGX_HTTP_VHOST_TRAFFIC_STATUS_UPSTREAM_PREFIX_LEN + host.len + 1;
1055+
usn.name.len = vtsn->len - host.len - NGX_HTTP_VHOST_TRAFFIC_STATUS_UPSTREAM_KEY_LEN;
1056+
usn.weight = 0;
1057+
usn.max_fails = 0;
1058+
usn.fail_timeout = 0;
1059+
usn.backup = 0;
1060+
usn.down = 0;
1061+
while (peers != NULL) {
1062+
ngx_http_upstream_rr_peers_rlock(peers);
1063+
for (peer = peers->peer; peer; peer = peer->next) {
1064+
rc = ngx_memn2cmp(peer->name.data, usn.name.data, peer->name.len, (size_t) usn.name.len);
1065+
if (rc == 0) {
1066+
usn.weight = peer->weight;
1067+
usn.max_fails = peer->max_fails;
1068+
usn.fail_timeout = peer->fail_timeout;
1069+
usn.backup = 0;
1070+
usn.down = (peer->fails >= peer->max_fails || peer->down);
1071+
}
10691072
}
1073+
ngx_http_upstream_rr_peers_unlock(peers);
1074+
peers = peers->next;
10701075
}
1071-
ngx_http_upstream_rr_peers_unlock(peers);
1072-
peers = peers->next;
1076+
buf = ngx_http_vhost_traffic_status_display_set_upstream_node(r, buf, &usn, vtsn);
10731077
}
1074-
buf = ngx_http_vhost_traffic_status_display_set_upstream_node(r, buf, &usn, vtsn);
10751078
}
10761079
}
10771080
buf = ngx_http_vhost_traffic_status_display_ug_host(r, host, node->left, sentinel, base_peers, buf);

src/ngx_http_vhost_traffic_status_display_json.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,9 @@
180180
"},"
181181

182182
#if (NGX_HTTP_CACHE)
183+
#define NGX_HTTP_VHOST_TRAFFIC_STATUS_UPSTREAM_PREFIX_LEN 3
184+
#define NGX_HTTP_VHOST_TRAFFIC_STATUS_UPSTREAM_KEY_LEN 4
185+
183186
#define NGX_HTTP_VHOST_TRAFFIC_STATUS_JSON_FMT_CACHE_S "\"cacheZones\":{"
184187
#define NGX_HTTP_VHOST_TRAFFIC_STATUS_JSON_FMT_CACHE "\"%V\":{" \
185188
"\"maxSize\":%uA," \

0 commit comments

Comments
 (0)