Skip to content

Commit cf4600d

Browse files
committed
Fix the filename size
1 parent cf85b1c commit cf4600d

File tree

1 file changed

+40
-37
lines changed

1 file changed

+40
-37
lines changed

plugins/healthchecks/healthchecks.cc

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ typedef struct HCFileData_t {
6464
typedef struct HCFileInfo_t {
6565
char fname[MAX_PATH_LEN]; /* Filename */
6666
char *basename; /* The "basename" of the file */
67-
unsigned basename_len; /* The length of the basename */
67+
unsigned basename_len = 0; /* The length of the basename */
6868
char path[PATH_NAME_MAX]; /* URL path for this HC */
6969
int p_len; /* Length of path */
7070
const char *ok; /* Header for an OK result */
@@ -137,7 +137,7 @@ setup_watchers(int fd)
137137
while (conf) {
138138
conf->wd = inotify_add_watch(fd, conf->fname, IN_DELETE_SELF | IN_CLOSE_WRITE | IN_ATTRIB);
139139
TSDebug(PLUGIN_NAME, "Setting up a watcher for %s", conf->fname);
140-
strncpy(fname, conf->fname, MAX_PATH_LEN);
140+
strncpy(fname, conf->fname, MAX_PATH_LEN - 1);
141141
char *dname = dirname(fname);
142142
/* Make sure to only watch each directory once */
143143
if (!(dir = find_direntry(dname, head_dir))) {
@@ -322,56 +322,59 @@ parse_configs(const char *fname)
322322
++str;
323323
}
324324
strncpy(finfo->path, str, PATH_NAME_MAX - 1);
325-
finfo->p_len = strlen(finfo->path);
325+
finfo->path[PATH_NAME_MAX - 1] = '\0';
326+
finfo->p_len = strlen(finfo->path);
326327
break;
327328
case 1:
328329
strncpy(finfo->fname, str, MAX_PATH_LEN - 1);
329-
finfo->basename = strrchr(finfo->fname, '/');
330+
finfo->fname[MAX_PATH_LEN - 1] = '\0';
331+
finfo->basename = strrchr(finfo->fname, '/');
330332
if (finfo->basename) {
331333
++(finfo->basename);
334+
finfo->basename_len = strlen(finfo->basename);
332335
}
333-
finfo->basename_len = strlen(finfo->basename);
334-
break;
335-
case 2:
336-
mime = str;
337-
break;
338-
case 3:
339-
ok = str;
340-
break;
341-
case 4:
342-
miss = str;
343-
break;
344336
}
345-
++state;
337+
break;
338+
case 2:
339+
mime = str;
340+
break;
341+
case 3:
342+
ok = str;
343+
break;
344+
case 4:
345+
miss = str;
346+
break;
346347
}
347-
str = strtok_r(nullptr, SEPARATORS, &save);
348+
++state;
348349
}
350+
str = strtok_r(nullptr, SEPARATORS, &save);
351+
}
349352

350-
/* Fill in the info if everything was ok */
351-
if (state > 4) {
352-
TSDebug(PLUGIN_NAME, "Parsed: %s %s %s %s %s", finfo->path, finfo->fname, mime, ok, miss);
353-
finfo->ok = gen_header(ok, mime, &finfo->o_len);
354-
finfo->miss = gen_header(miss, mime, &finfo->m_len);
355-
finfo->data = tsapi::malloc<HCFileData>();
356-
memset(finfo->data, 0, sizeof(HCFileData));
357-
reload_status_file(finfo, finfo->data);
358-
359-
/* Add it the linked list */
360-
TSDebug(PLUGIN_NAME, "Adding path=%s to linked list", finfo->path);
361-
if (nullptr == head_finfo) {
362-
head_finfo = finfo;
363-
} else {
364-
prev_finfo->_next = finfo;
365-
}
366-
prev_finfo = finfo;
353+
/* Fill in the info if everything was ok */
354+
if (state > 4) {
355+
TSDebug(PLUGIN_NAME, "Parsed: %s %s %s %s %s", finfo->path, finfo->fname, mime, ok, miss);
356+
finfo->ok = gen_header(ok, mime, &finfo->o_len);
357+
finfo->miss = gen_header(miss, mime, &finfo->m_len);
358+
finfo->data = tsapi::malloc<HCFileData>();
359+
memset(finfo->data, 0, sizeof(HCFileData));
360+
reload_status_file(finfo, finfo->data);
361+
362+
/* Add it the linked list */
363+
TSDebug(PLUGIN_NAME, "Adding path=%s to linked list", finfo->path);
364+
if (nullptr == head_finfo) {
365+
head_finfo = finfo;
367366
} else {
368-
TSfree(finfo);
367+
prev_finfo->_next = finfo;
369368
}
369+
prev_finfo = finfo;
370+
} else {
371+
TSfree(finfo);
370372
}
371373
}
372-
fclose(fd);
374+
}
375+
fclose(fd);
373376

374-
return head_finfo;
377+
return head_finfo;
375378
}
376379

377380
/* Cleanup after intercept has completed */

0 commit comments

Comments
 (0)