Skip to content

Commit

Permalink
Terminate string that is sent to strcmp. (#9517)
Browse files Browse the repository at this point in the history
  - A buffer read from `fread` is not null terminated. Make sure we null
    terminate it before we do string operations on it like `strcmp()`.

    This was preventing the determination of delimiters by reading the
    beginning of csv files for the pattern `"sep=x"` where x is the delimiter.

    If not explicitly specified, the default is of course `,`

  - Fixes #9487.
  • Loading branch information
mahge committed Oct 14, 2022
1 parent 057b86d commit a528d6d
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions OMCompiler/SimulationRuntime/c/util/read_csv.c
Expand Up @@ -252,13 +252,17 @@ struct csv_data* read_csv(const char *filename)
struct csv_data *res;
size_t offset = 0;
unsigned char delim = CSV_COMMA;
size_t len;

FILE *fin = omc_fopen(filename, "r");
if (!fin) {
return NULL;
}

/* determine delim */
omc_fread(buf, 1, 5, fin, 0);
len = omc_fread(buf, 1, 5, fin, 0);

This comment has been minimized.

Copy link
@hkiel

hkiel Oct 16, 2022

Member

The parallel line 215 has the same problem (read_csv_dataset_size())
By doing a grep on the sources I see a few more places where the return value of omc_fread() is not used. I didn't check if is about reading (and comparing) strings though.

// Terminate the string in the buffer to make sure strcmp works as expected.
buf[len] = '\0';
if (0 == strcmp(buf, "\"sep="))
{
omc_fread(&delim, 1, 1, fin, 0);
Expand All @@ -277,7 +281,7 @@ struct csv_data* read_csv(const char *filename)
csv_set_realloc_func(&p, realloc);
csv_set_free_func(&p, free);
do {
size_t len = omc_fread(buf, 1, buf_size, fin, 1);
len = omc_fread(buf, 1, buf_size, fin, 1);
if (len != buf_size && !feof(fin)) {
csv_free(&p);
fclose(fin);
Expand Down

0 comments on commit a528d6d

Please sign in to comment.