From 4d241042e81435b20de3207423f6626b5ac35605 Mon Sep 17 00:00:00 2001 From: Manuel <71137295+mverch67@users.noreply.github.com> Date: Fri, 22 May 2026 09:30:35 +0200 Subject: [PATCH 1/2] check wifi status --- source/graphics/map/URLService.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/source/graphics/map/URLService.cpp b/source/graphics/map/URLService.cpp index 0ca96af1..a7ddef97 100644 --- a/source/graphics/map/URLService.cpp +++ b/source/graphics/map/URLService.cpp @@ -6,6 +6,8 @@ #ifdef ARDUINO_ARCH_ESP32 +#include "WiFi.h" + // from ConvertPNG.c extern "C" { bool decodeImgGrey(const void *data, size_t size, lv_img_dsc_t **img); @@ -23,6 +25,11 @@ bool URLService::load(const char *name, void *img) ~HttpEndGuard() { client.end(); } } httpGuard{http}; + if (WiFi.status() != WL_CONNECTED) { + ILOG_DEBUG("URLService::load skipped (WiFi not connected)"); + return false; + } + struct LvFreeGuard { uint8_t *&ptr; ~LvFreeGuard() { lv_free(ptr); } @@ -30,6 +37,11 @@ bool URLService::load(const char *name, void *img) // transform filename to provider url std::string url = TileProvider::url(name); + if (url.empty()) { + ILOG_ERROR("empty URL for tile %s", name ? name : "(null)"); + return false; + } + http.begin(url.c_str()); int httpCode = http.GET(); if (httpCode != HTTP_CODE_OK) { From 936946aedbe49223852dd22be7a042e7e12b3290 Mon Sep 17 00:00:00 2001 From: Manuel <71137295+mverch67@users.noreply.github.com> Date: Fri, 22 May 2026 09:31:23 +0200 Subject: [PATCH 2/2] harden MapTile settings sizes and check bounds --- include/graphics/map/MapTileSettings.h | 30 ++++++++++++++++++++----- source/graphics/map/MapTileSettings.cpp | 10 ++++----- source/graphics/map/TileProvider.cpp | 15 ++++++++++++- 3 files changed, 44 insertions(+), 11 deletions(-) diff --git a/include/graphics/map/MapTileSettings.h b/include/graphics/map/MapTileSettings.h index e2cdd5cc..617ff9c4 100644 --- a/include/graphics/map/MapTileSettings.h +++ b/include/graphics/map/MapTileSettings.h @@ -9,6 +9,10 @@ class MapTileSettings { public: + static constexpr size_t PREFIX_SIZE = 10; + static constexpr size_t TILE_STYLE_SIZE = 20; + static constexpr size_t TILE_FORMAT_SIZE = 10; + MapTileSettings() = default; static uint8_t getDefaultZoom(void) { return zoomDefault; } static void setDefaultZoom(uint8_t zoom) { zoomDefault = zoom; } @@ -28,19 +32,21 @@ class MapTileSettings static void setDefaultLon(float lon) { defaultLon = lon; } static const char *getPrefix(void) { return prefix; } - static void setPrefix(const char *p) { strcpy(prefix, p); } + static void setPrefix(const char *p) { copyBounded(prefix, PREFIX_SIZE, p); } static const char *getTileStyle(void) { return tileStyle; } static void setTileStyle(const char *p) { - strcpy(tileStyle, p); + copyBounded(tileStyle, TILE_STYLE_SIZE, p); size_t len = strlen(tileStyle); - if (len > 0 && tileStyle[len - 1] != '/') - strcat(tileStyle, "/"); + if (len > 0 && tileStyle[len - 1] != '/' && len + 1 < TILE_STYLE_SIZE) { + tileStyle[len] = '/'; + tileStyle[len + 1] = '\0'; + } } static const char *getTileFormat(void) { return tileFormat; } - static void setTileFormat(const char *p) { strcpy(tileFormat, p); } + static void setTileFormat(const char *p) { copyBounded(tileFormat, TILE_FORMAT_SIZE, p); } static uint16_t getTileProvider(void) { return tileProviderId; } static void setTileProvider(uint16_t id) { tileProviderId = id; } @@ -55,6 +61,20 @@ class MapTileSettings static void setSaveOK(bool ok) { save = ok; } private: + static void copyBounded(char *dst, size_t dstSize, const char *src) + { + if (!dst || dstSize == 0) { + return; + } + if (!src) { + dst[0] = '\0'; + return; + } + + strncpy(dst, src, dstSize - 1); + dst[dstSize - 1] = '\0'; + } + static uint8_t zoomLevel; static uint8_t zoomDefault; static uint16_t tileSize; diff --git a/source/graphics/map/MapTileSettings.cpp b/source/graphics/map/MapTileSettings.cpp index 8201fa5c..ab883feb 100644 --- a/source/graphics/map/MapTileSettings.cpp +++ b/source/graphics/map/MapTileSettings.cpp @@ -9,11 +9,11 @@ uint16_t MapTileSettings::tileProviderId = 0; // default url index to load uint32_t MapTileSettings::cacheSize = 50 * 1024; // LV_FS_CACHE_FROM_BUFFER float MapTileSettings::defaultLat = 51.5003646652f; // @theBigBentern float MapTileSettings::defaultLon = -0.1214328476f; -char MapTileSettings::prefix[10] = "/maps"; // default map tile directory -char MapTileSettings::tileStyle[20] = ""; // { osm/, atlas/, atlas-mobile/, ...} -char MapTileSettings::tileFormat[10] = "png"; // use jpg or png -bool MapTileSettings::debug = false; // draw tile frame and info -bool MapTileSettings::save = false; // ok to save tile back to SD card +char MapTileSettings::prefix[MapTileSettings::PREFIX_SIZE] = "/maps"; // default map tile directory +char MapTileSettings::tileStyle[MapTileSettings::TILE_STYLE_SIZE] = ""; // { osm/, atlas/, atlas-mobile/, ...} +char MapTileSettings::tileFormat[MapTileSettings::TILE_FORMAT_SIZE] = "png"; // use jpg or png +bool MapTileSettings::debug = false; // draw tile frame and info +bool MapTileSettings::save = false; // ok to save tile back to SD card #ifdef MAP_TILES_GREY bool MapTileSettings::colorTiles = false; #else diff --git a/source/graphics/map/TileProvider.cpp b/source/graphics/map/TileProvider.cpp index 92fa42f7..acc1e462 100644 --- a/source/graphics/map/TileProvider.cpp +++ b/source/graphics/map/TileProvider.cpp @@ -21,7 +21,20 @@ std::string TileProvider::url(const char *filename) std::string TileProvider::url(int z, int x, int y) { std::string provider, url; - std::tie(provider, url) = urlTemplates[MapTileSettings::getTileProvider()]; + if (urlTemplates.empty()) { + ILOG_ERROR("no URL templates available"); + return ""; + } + + size_t selected = MapTileSettings::getTileProvider(); + if (selected >= urlTemplates.size()) { + ILOG_WARN("tile provider index out of range: %u (max %u)", (unsigned int)selected, + (unsigned int)(urlTemplates.size() - 1)); + selected = 0; + MapTileSettings::setTileProvider(0); + } + + std::tie(provider, url) = urlTemplates[selected]; size_t pos; while ((pos = url.find("{z}")) != std::string::npos) url.replace(pos, 3, std::to_string(z));