Skip to content

Commit 89fe03f

Browse files
cosmo0920edsiper
authored andcommitted
filter_kubernetes: Make rigid approach of growing memory
Signed-off-by: Hiroshi Hatake <hiroshi@chronosphere.io>
1 parent ad356f6 commit 89fe03f

1 file changed

Lines changed: 34 additions & 7 deletions

File tree

plugins/filter_kubernetes/kube_meta.c

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#define FLB_KUBE_META_INIT_CONTAINER_STATUSES_KEY_LEN \
4949
(sizeof(FLB_KUBE_META_INIT_CONTAINER_STATUSES_KEY) - 1)
5050
#define FLB_KUBE_TOKEN_BUF_SIZE 8192 /* 8KB */
51+
#define FLB_KUBE_TOKEN_MAX_SIZE (1024 * 1024) /* 1MB */
5152

5253
static int file_to_buffer(const char *path,
5354
char **out_buf, size_t *out_size)
@@ -100,6 +101,9 @@ static int get_token_with_command(const char *command,
100101
char buf[FLB_KUBE_TOKEN_BUF_SIZE];
101102
char *temp;
102103
char *res;
104+
size_t capacity = FLB_KUBE_TOKEN_BUF_SIZE;
105+
size_t required_size;
106+
size_t new_capacity;
103107
size_t size = 0;
104108
size_t len = 0;
105109

@@ -108,7 +112,7 @@ static int get_token_with_command(const char *command,
108112
return -1;
109113
}
110114

111-
res = flb_calloc(1, FLB_KUBE_TOKEN_BUF_SIZE);
115+
res = flb_calloc(1, capacity);
112116
if (!res) {
113117
flb_errno();
114118
pclose(fp);
@@ -117,21 +121,39 @@ static int get_token_with_command(const char *command,
117121

118122
while (fgets(buf, sizeof(buf), fp) != NULL) {
119123
len = strlen(buf);
120-
if (len >= FLB_KUBE_TOKEN_BUF_SIZE - 1) {
121-
temp = flb_realloc(res, (FLB_KUBE_TOKEN_BUF_SIZE + size) * 2);
124+
125+
if (len > FLB_KUBE_TOKEN_MAX_SIZE - size - 1) {
126+
flb_free(res);
127+
pclose(fp);
128+
return -1;
129+
}
130+
required_size = size + len + 1;
131+
132+
if (required_size > capacity) {
133+
new_capacity = capacity;
134+
135+
while (new_capacity < required_size) {
136+
new_capacity *= 2;
137+
}
138+
139+
temp = flb_realloc(res, new_capacity);
122140
if (temp == NULL) {
123141
flb_errno();
124142
flb_free(res);
125143
pclose(fp);
126144
return -1;
127145
}
146+
128147
res = temp;
148+
capacity = new_capacity;
129149
}
130-
strcpy(res + size, buf);
150+
151+
memcpy(res + size, buf, len);
131152
size += len;
153+
res[size] = '\0';
132154
}
133155

134-
if (strlen(res) < 1) {
156+
if (size < 1) {
135157
flb_free(res);
136158
pclose(fp);
137159
return -1;
@@ -140,7 +162,7 @@ static int get_token_with_command(const char *command,
140162
pclose(fp);
141163

142164
*out_buf = res;
143-
*out_size = strlen(res);
165+
*out_size = size;
144166

145167
return 0;
146168
}
@@ -169,8 +191,13 @@ static int get_http_auth_header(struct flb_kube *ctx)
169191
if (ret == -1) {
170192
flb_plg_warn(ctx->ins, "cannot open %s", FLB_KUBE_TOKEN);
171193
}
172-
flb_plg_info(ctx->ins, " token updated");
173194
}
195+
196+
if (ret == -1 || tk == NULL) {
197+
return -1;
198+
}
199+
200+
flb_plg_info(ctx->ins, " token updated");
174201
ctx->kube_token_create = time(NULL);
175202

176203
/* Token */

0 commit comments

Comments
 (0)