From 8bd18714ed3436212b49514c79c85a09ba43751f Mon Sep 17 00:00:00 2001 From: Troels Henriksen Date: Sat, 31 Aug 2024 22:44:44 +0200 Subject: [PATCH] Make tuning file loader more robust. --- CHANGELOG.md | 2 ++ rts/c/tuning.h | 8 +++++++- src/Futhark/CodeGen/Backends/GenericC/CLI.hs | 2 +- src/Futhark/CodeGen/Backends/GenericC/Server.hs | 2 +- 4 files changed, 11 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 67ba89ca9e..30dd461e49 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. * `auto output` didn't work if the `.fut` file did not have any path components. +* Improved detection of malformed tuning files. + ## [0.25.20] ### Added diff --git a/rts/c/tuning.h b/rts/c/tuning.h index 99f8f8f08c..e1e0f6266b 100644 --- a/rts/c/tuning.h +++ b/rts/c/tuning.h @@ -29,7 +29,13 @@ static char* load_tuning_file(const char *fname, char *eql = strstr(line, "="); if (eql) { *eql = 0; - int value = atoi(eql+1); + char *endptr; + int value = strtol(eql+1, &endptr, 10); + if (*endptr) { + snprintf(line, max_line_len, "Invalid line %d (must be of form 'name=int').", + lineno); + return line; + } if (set_tuning_param(cfg, line, (size_t)value) != 0) { char* err = (char*) malloc(max_line_len + 50); snprintf(err, max_line_len + 50, "Unknown name '%s' on line %d.", line, lineno); diff --git a/src/Futhark/CodeGen/Backends/GenericC/CLI.hs b/src/Futhark/CodeGen/Backends/GenericC/CLI.hs index 257cabd4e6..c19bdec2e0 100644 --- a/src/Futhark/CodeGen/Backends/GenericC/CLI.hs +++ b/src/Futhark/CodeGen/Backends/GenericC/CLI.hs @@ -141,7 +141,7 @@ genericOptions = char *ret = load_tuning_file(optarg, cfg, (int(*)(void*, const char*, size_t)) futhark_context_config_set_tuning_param); if (ret != NULL) { - futhark_panic(1, "When loading tuning from '%s': %s\n", optarg, ret); + futhark_panic(1, "When loading tuning file '%s': %s\n", optarg, ret); }}|] }, Option diff --git a/src/Futhark/CodeGen/Backends/GenericC/Server.hs b/src/Futhark/CodeGen/Backends/GenericC/Server.hs index ff3ec83fc1..18ecb35502 100644 --- a/src/Futhark/CodeGen/Backends/GenericC/Server.hs +++ b/src/Futhark/CodeGen/Backends/GenericC/Server.hs @@ -99,7 +99,7 @@ genericOptions = char *ret = load_tuning_file(optarg, cfg, (int(*)(void*, const char*, size_t)) futhark_context_config_set_tuning_param); if (ret != NULL) { - futhark_panic(1, "When loading tuning from '%s': %s\n", optarg, ret); + futhark_panic(1, "When loading tuning file '%s': %s\n", optarg, ret); }}|] }, Option