Merge pull request #321 from weichslgartner/master

Fixed some undefined behavior and added CodeQL workflow
pull/366/head
Marc Kleine-Budde 2022-06-20 19:40:36 +02:00 committed by GitHub
commit 2b0ed067dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 115 additions and 8 deletions

View File

@ -0,0 +1,70 @@
# For most projects, this workflow file will not need changing; you simply need
# to commit it to your repository.
#
# You may wish to alter this file to override the set of languages analyzed,
# or to provide custom queries or build logic.
#
# ******** NOTE ********
# We have attempted to detect the languages in your repository. Please check
# the `language` matrix defined below to confirm you have the correct set of
# supported CodeQL languages.
#
name: "CodeQL"
on:
push:
branches: [ master ]
pull_request:
# The branches below must be a subset of the branches above
branches: [ master ]
schedule:
- cron: '29 21 * * 1'
jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write
strategy:
fail-fast: false
matrix:
language: [ 'cpp' ]
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ]
# Learn more about CodeQL language support at https://git.io/codeql-language-support
steps:
- name: Checkout repository
uses: actions/checkout@v2
# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v1
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.
# queries: ./path/to/local/query, your-org/your-repo/queries@main
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v1
# Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl
# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
# and modify them (or add more) to build your code if your project
# uses a compiled language
#- run: |
# make bootstrap
# make release
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1

View File

@ -50,6 +50,7 @@
#include <sys/time.h> #include <sys/time.h>
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
#include <limits.h>
#include <linux/can.h> #include <linux/can.h>
#include <linux/can/error.h> #include <linux/can/error.h>
@ -206,6 +207,13 @@ void eval_can(char* buf, struct timeval *date_tvp, char timestamps, char base, i
if (strlen(dir) != 2) /* "Rx" or "Tx" */ if (strlen(dir) != 2) /* "Rx" or "Tx" */
return; return;
/* check for signed integer overflow */
if (dplace == 4 && read_tv.tv_usec >= INT_MAX / 100)
return;
if (dplace == 5 && read_tv.tv_usec >= INT_MAX / 10)
return;
if (dir[0] == 'R') if (dir[0] == 'R')
extra_info = " R\n"; extra_info = " R\n";
else else
@ -269,6 +277,14 @@ void eval_canfd(char* buf, struct timeval *date_tvp, char timestamps, int dplace
if (strlen(dir) != 2) /* "Rx" or "Tx" */ if (strlen(dir) != 2) /* "Rx" or "Tx" */
return; return;
/* check for signed integer overflow */
if (dplace == 4 && read_tv.tv_usec >= INT_MAX / 100)
return;
/* check for signed integer overflow */
if (dplace == 5 && read_tv.tv_usec >= INT_MAX / 10)
return;
if (dir[0] == 'R') if (dir[0] == 'R')
extra_info = " R\n"; extra_info = " R\n";
else else

37
lib.c
View File

@ -159,7 +159,7 @@ int parse_canframe(char *cs, struct canfd_frame *cf) {
int i, idx, dlen, len; int i, idx, dlen, len;
int maxdlen = CAN_MAX_DLEN; int maxdlen = CAN_MAX_DLEN;
int ret = CAN_MTU; int ret = CAN_MTU;
unsigned char tmp; canid_t tmp;
len = strlen(cs); len = strlen(cs);
//printf("'%s' len %d\n", cs, len); //printf("'%s' len %d\n", cs, len);
@ -568,10 +568,20 @@ static int snprintf_error_data(char *buf, size_t len, uint8_t err,
for (i = 0; i < arr_len; i++) { for (i = 0; i < arr_len; i++) {
if (err & (1 << i)) { if (err & (1 << i)) {
if (count) int tmp_n = 0;
n += snprintf(buf + n, len - n, ","); if (count){
n += snprintf(buf + n, len - n, "%s", arr[i]); /* Fix for potential buffer overflow https://lgtm.com/rules/1505913226124/ */
count++; tmp_n = snprintf(buf + n, len - n, ",");
if (tmp_n < 0 || tmp_n >= len - n){
return n;
}
n += tmp_n;
}
tmp_n = snprintf(buf + n, len - n, "%s", arr[i]);
if (tmp_n < 0 || tmp_n >= len - n){
return n;
}
n += tmp_n;
} }
} }
@ -644,9 +654,20 @@ void snprintf_can_error_frame(char *buf, size_t len, const struct canfd_frame *c
for (i = 0; i < (int)ARRAY_SIZE(error_classes); i++) { for (i = 0; i < (int)ARRAY_SIZE(error_classes); i++) {
mask = 1 << i; mask = 1 << i;
if (class & mask) { if (class & mask) {
if (classes) int tmp_n = 0;
n += snprintf(buf + n, len - n, "%s", sep); if (classes){
n += snprintf(buf + n, len - n, "%s", error_classes[i]); /* Fix for potential buffer overflow https://lgtm.com/rules/1505913226124/ */
tmp_n = snprintf(buf + n, len - n, "%s", sep);
if (tmp_n < 0 || tmp_n >= len - n){
return;
}
n += tmp_n;
}
tmp_n = snprintf(buf + n, len - n, "%s", error_classes[i]);
if (tmp_n < 0 || tmp_n >= len - n){
return;
}
n += tmp_n;
if (mask == CAN_ERR_LOSTARB) if (mask == CAN_ERR_LOSTARB)
n += snprintf_error_lostarb(buf + n, len - n, n += snprintf_error_lostarb(buf + n, len - n,
cf); cf);