Skip to content

Commit

Permalink
Fix/number of interfaces bug (#39)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
RincewindsHat authored Jul 10, 2024
1 parent f89e531 commit df6392e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ jobs:
build:
runs-on: ubuntu-latest
env:
CFLAGS: "-Wall"
CFLAGS: "-Wall -Werror"

steps:
- uses: actions/checkout@v4

- uses: actions/setup-python@v5
with:
python-version: '3.x'
Expand Down Expand Up @@ -41,18 +41,18 @@ jobs:
run: sleep 30s
shell: bash

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

clang-build:
runs-on: ubuntu-latest
env:
CC: clang
CFLAGS: "-Wall"
CFLAGS: "-Wall"

steps:
- uses: actions/checkout@v4

- uses: actions/setup-python@v5
with:
python-version: '3.x'
Expand Down Expand Up @@ -81,5 +81,5 @@ jobs:
run: sleep 30s
shell: bash

- name: check_interfaces
- name: check_interfaces with some cisco stuff
run: ./check_interfaces -h 127.0.0.1 --port=1611 -c cisco-c3560 -d
19 changes: 17 additions & 2 deletions check_interfaces.c
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,18 @@ int main(int argc, char *argv[]) {
vars = vars->next_variable;
}

// get the real list length we need
int real_count = 0;
for (netsnmp_variable_list *runner = response->variables; runner; runner = runner->next_variable) {
if (vars->type == ASN_OCTET_STR) {
real_count ++;
}
}

if (real_count > ifNumber) {
ifNumber = real_count;
}

interfaces = (struct ifStruct *)calloc((size_t)ifNumber,
sizeof(struct ifStruct));
oldperfdata = (struct ifStruct *)calloc(
Expand Down Expand Up @@ -365,15 +377,18 @@ int main(int argc, char *argv[]) {
if (vars->type == ASN_OCTET_STR) {
if (config.trimdescr && config.trimdescr < vars->val_len) {
interfaces[count].index =
(int)vars->name[(vars->name_length - 1)];
vars->name[(vars->name_length - 1)];

MEMCPY(interfaces[count].descr,
(vars->val.string) + config.trimdescr,
vars->val_len - config.trimdescr);

TERMSTR(interfaces[count].descr,
vars->val_len - config.trimdescr);
} else {
interfaces[count].index =
(int)vars->name[(vars->name_length - 1)];
vars->name[(vars->name_length - 1)];

MEMCPY(interfaces[count].descr, vars->val.string,
vars->val_len);
TERMSTR(interfaces[count].descr, vars->val_len);
Expand Down

0 comments on commit df6392e

Please sign in to comment.