Skip to content

Commit df6392e

Browse files
Fix/number of interfaces bug (#39)
* Try (harder?) to detect the real number of check_interfaces It seems like detecting the number of interfaces a device has is harder than one might think. Apparently those things lie HARD about that. If you then try to allocate memory for the result based on that, but try to write MORE results there, you get a segfault sooner or later. This patch iterates through the answer of the device and just counts them, so this is hopefully the safe way to do this. * Remove suspicious type cast
1 parent f89e531 commit df6392e

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

.github/workflows/build.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ jobs:
88
build:
99
runs-on: ubuntu-latest
1010
env:
11-
CFLAGS: "-Wall"
11+
CFLAGS: "-Wall -Werror"
1212

1313
steps:
1414
- uses: actions/checkout@v4
15-
15+
1616
- uses: actions/setup-python@v5
1717
with:
1818
python-version: '3.x'
@@ -41,18 +41,18 @@ jobs:
4141
run: sleep 30s
4242
shell: bash
4343

44-
- name: check_interfaces
44+
- name: check_interfaces with some cisco stuff
4545
run: ./check_interfaces -h 127.0.0.1 --port=1611 -c cisco-c3560 -d
4646

4747
clang-build:
4848
runs-on: ubuntu-latest
4949
env:
5050
CC: clang
51-
CFLAGS: "-Wall"
51+
CFLAGS: "-Wall"
5252

5353
steps:
5454
- uses: actions/checkout@v4
55-
55+
5656
- uses: actions/setup-python@v5
5757
with:
5858
python-version: '3.x'
@@ -81,5 +81,5 @@ jobs:
8181
run: sleep 30s
8282
shell: bash
8383

84-
- name: check_interfaces
84+
- name: check_interfaces with some cisco stuff
8585
run: ./check_interfaces -h 127.0.0.1 --port=1611 -c cisco-c3560 -d

check_interfaces.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,18 @@ int main(int argc, char *argv[]) {
324324
vars = vars->next_variable;
325325
}
326326

327+
// get the real list length we need
328+
int real_count = 0;
329+
for (netsnmp_variable_list *runner = response->variables; runner; runner = runner->next_variable) {
330+
if (vars->type == ASN_OCTET_STR) {
331+
real_count ++;
332+
}
333+
}
334+
335+
if (real_count > ifNumber) {
336+
ifNumber = real_count;
337+
}
338+
327339
interfaces = (struct ifStruct *)calloc((size_t)ifNumber,
328340
sizeof(struct ifStruct));
329341
oldperfdata = (struct ifStruct *)calloc(
@@ -365,15 +377,18 @@ int main(int argc, char *argv[]) {
365377
if (vars->type == ASN_OCTET_STR) {
366378
if (config.trimdescr && config.trimdescr < vars->val_len) {
367379
interfaces[count].index =
368-
(int)vars->name[(vars->name_length - 1)];
380+
vars->name[(vars->name_length - 1)];
381+
369382
MEMCPY(interfaces[count].descr,
370383
(vars->val.string) + config.trimdescr,
371384
vars->val_len - config.trimdescr);
385+
372386
TERMSTR(interfaces[count].descr,
373387
vars->val_len - config.trimdescr);
374388
} else {
375389
interfaces[count].index =
376-
(int)vars->name[(vars->name_length - 1)];
390+
vars->name[(vars->name_length - 1)];
391+
377392
MEMCPY(interfaces[count].descr, vars->val.string,
378393
vars->val_len);
379394
TERMSTR(interfaces[count].descr, vars->val_len);

0 commit comments

Comments
 (0)