mirror of
https://github.com/esphome/esphome.git
synced 2026-10-14 18:22:48 +00:00
Compare commits
41
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
efbee480dc | ||
|
|
1e72cdb8a0 | ||
|
|
f117264eda | ||
|
|
6ea7651a5c | ||
|
|
d253f6af4e | ||
|
|
bbfdf02903 | ||
|
|
20ff795229 | ||
|
|
c9fbc2f046 | ||
|
|
7c420ec6ae | ||
|
|
feb139fd20 | ||
|
|
5af8b8a972 | ||
|
|
39fd5b6a87 | ||
|
|
1167d95312 | ||
|
|
9828037ff1 | ||
|
|
c3546f8320 | ||
|
|
9d400ab787 | ||
|
|
4f0fec3e50 | ||
|
|
f73dc0e7ce | ||
|
|
cd214cc98b | ||
|
|
3d71e9050f | ||
|
|
55bf8c4e0c | ||
|
|
7e089621d1 | ||
|
|
1d1758c508 | ||
|
|
2606c88f3c | ||
|
|
5c0607fc6e | ||
|
|
2b3921b565 | ||
|
|
6020b76587 | ||
|
|
6f1d7d78f6 | ||
|
|
aef04162d9 | ||
|
|
e92530a0d5 | ||
|
|
3c8c511e12 | ||
|
|
c6062a73c3 | ||
|
|
bd193c0aff | ||
|
|
79754065cd | ||
|
|
0e800b2bd8 | ||
|
|
2c7b2e6cb1 | ||
|
|
ac4abd7f1f | ||
|
|
da70e10832 | ||
|
|
8aa1424ae4 | ||
|
|
7d8f4b60e1 | ||
|
|
d1943fe835 |
@@ -0,0 +1,126 @@
|
||||
"""Build-time scans of the user's C++ sources: lambdas, ``includes:`` files and
|
||||
external components.
|
||||
|
||||
The only scan today finds scanf calls with float conversions, which decides
|
||||
whether float scanf support (or, on ESP-IDF Bluetooth builds, the libc sscanf)
|
||||
must stay linked. Other source heuristics belong here too.
|
||||
"""
|
||||
|
||||
import logging
|
||||
from pathlib import Path
|
||||
import re
|
||||
|
||||
from esphome.config_helpers import iter_include_files
|
||||
from esphome.const import (
|
||||
CONF_ESPHOME,
|
||||
CONF_INCLUDES,
|
||||
CONF_INCLUDES_C,
|
||||
SOURCE_FILE_EXTENSIONS,
|
||||
)
|
||||
from esphome.core import Lambda
|
||||
from esphome.types import ConfigType
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
# The string literal handed to a scanf family call as its format: an optional
|
||||
# first argument (sscanf/fscanf; a literal or a call with its own commas is
|
||||
# fine) then the literal.
|
||||
_SCANF_FORMAT_RE = re.compile(
|
||||
r'scanf\s*\((?:(?:"(?:[^"\\]|\\.)*"|\([^()]*\)|[^,;"()])*,)?\s*"((?:[^"\\]|\\.)*)"'
|
||||
)
|
||||
# Standard scanf float conversions %f %F %e %E %g %G %a %A with optional
|
||||
# suppression, width and length; also the invalid %.2f users write by analogy
|
||||
# with printf.
|
||||
_SCANF_FLOAT_SPEC_RE = re.compile(r"%[*\d.]*[hlL]*[feEgGaAF]")
|
||||
|
||||
|
||||
def source_uses_scanf_float(src: str) -> bool:
|
||||
"""Heuristic: does C++ source call a scanf family function with a float conversion?"""
|
||||
if "scanf" not in src:
|
||||
return False
|
||||
src = Lambda.comment_remover(src)
|
||||
return any(
|
||||
_SCANF_FLOAT_SPEC_RE.search(match.group(1))
|
||||
for match in _SCANF_FORMAT_RE.finditer(src)
|
||||
)
|
||||
|
||||
|
||||
def lambdas_use_scanf_float(config: ConfigType) -> bool:
|
||||
"""Check if any lambda in the config uses scanf with a float format specifier."""
|
||||
stack: list = [config]
|
||||
while stack:
|
||||
obj = stack.pop()
|
||||
if isinstance(obj, Lambda):
|
||||
if source_uses_scanf_float(obj.value):
|
||||
return True
|
||||
elif isinstance(obj, dict):
|
||||
stack.extend(obj.values())
|
||||
elif isinstance(obj, list):
|
||||
stack.extend(obj)
|
||||
return False
|
||||
|
||||
|
||||
def includes_use_scanf_float(config: ConfigType) -> bool:
|
||||
"""Check if an ``includes:`` or ``includes_c:`` file uses scanf with a float format specifier."""
|
||||
esphome_config = config.get(CONF_ESPHOME, {})
|
||||
includes = esphome_config.get(CONF_INCLUDES, []) + esphome_config.get(
|
||||
CONF_INCLUDES_C, []
|
||||
)
|
||||
for path, _ in iter_include_files(includes):
|
||||
if path.suffix not in SOURCE_FILE_EXTENSIONS:
|
||||
continue
|
||||
if source_uses_scanf_float(path.read_text(encoding="utf-8", errors="replace")):
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def external_components_use_scanf_float(config: ConfigType) -> bool:
|
||||
"""Whether a component from outside the esphome package scans a float."""
|
||||
from esphome.config import iter_components
|
||||
from esphome.loader import CORE_COMPONENTS_PATH
|
||||
|
||||
package_root = CORE_COMPONENTS_PATH.parent
|
||||
for _, component in iter_components(config):
|
||||
package_dir = Path(component.module.__file__).resolve().parent
|
||||
if package_dir.is_relative_to(package_root):
|
||||
continue
|
||||
for resource in component.resources:
|
||||
src = (package_dir / resource.resource).read_text(
|
||||
encoding="utf-8", errors="replace"
|
||||
)
|
||||
if source_uses_scanf_float(src):
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def user_code_uses_scanf_float(config: ConfigType) -> bool:
|
||||
"""Whether a lambda, an ``includes:`` file or an external component scans a float."""
|
||||
return (
|
||||
lambdas_use_scanf_float(config)
|
||||
or includes_use_scanf_float(config)
|
||||
or external_components_use_scanf_float(config)
|
||||
)
|
||||
|
||||
|
||||
def keep_float_scanf(
|
||||
option: bool | None, config: ConfigType, flash_note: str, override_note: str
|
||||
) -> bool:
|
||||
"""Resolve a tri-state float-scanf option: unset means "only if user code scans a float"."""
|
||||
if option:
|
||||
return True
|
||||
found = user_code_uses_scanf_float(config)
|
||||
if option is None:
|
||||
if found:
|
||||
_LOGGER.warning(
|
||||
"Lambda, include or external component uses scanf with a float format "
|
||||
"specifier; keeping float scanf support (%s)",
|
||||
flash_note,
|
||||
)
|
||||
return found
|
||||
if found:
|
||||
_LOGGER.warning(
|
||||
"Float scanf support is disabled but a lambda, include or external "
|
||||
"component uses scanf with a float format specifier; %s",
|
||||
override_note,
|
||||
)
|
||||
return False
|
||||
@@ -10,11 +10,13 @@ import subprocess
|
||||
from typing import Any
|
||||
|
||||
from esphome import yaml_util
|
||||
from esphome.code_scan import keep_float_scanf
|
||||
import esphome.codegen as cg
|
||||
from esphome.components.const import (
|
||||
CONF_ENABLE_OTA_DOWNGRADE_PROTECTION,
|
||||
CONF_IGNORE_NOT_FOUND,
|
||||
)
|
||||
from esphome.config_helpers import filter_source_files_from_defines
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import (
|
||||
CONF_ADVANCED,
|
||||
@@ -202,6 +204,10 @@ PSRAM_XIP_VARIANTS = {
|
||||
# neither, so the engine is already in the image and the wrap saves nothing.
|
||||
ROM_VSNPRINTF_WITHOUT_VASPRINTF_VARIANTS = {VARIANT_ESP32C6}
|
||||
|
||||
# Variants whose ROM exports sscanf with the newlib format in use (C6 normal
|
||||
# format, C2 nano format by IDF default), so the sscanf wrap saves nothing.
|
||||
ROM_SSCANF_VARIANTS = {VARIANT_ESP32C2, VARIANT_ESP32C6}
|
||||
|
||||
# NVS encryption (HMAC peripheral scheme) is only available on variants that
|
||||
# expose the HMAC peripheral (SOC_HMAC_SUPPORTED in soc_caps.h). The original
|
||||
# ESP32 and ESP32-C2 do not have it. New variants with an HMAC peripheral
|
||||
@@ -1934,6 +1940,7 @@ CONF_RINGBUF_IN_IRAM = "ringbuf_in_iram"
|
||||
CONF_HEAP_IN_IRAM = "heap_in_iram"
|
||||
CONF_LOOP_TASK_STACK_SIZE = "loop_task_stack_size"
|
||||
CONF_USE_FULL_CERTIFICATE_BUNDLE = "use_full_certificate_bundle"
|
||||
CONF_ENABLE_FULL_SCANF = "enable_full_scanf"
|
||||
CONF_DISABLE_DEBUG_STUBS = "disable_debug_stubs"
|
||||
CONF_DISABLE_OCD_AWARE = "disable_ocd_aware"
|
||||
CONF_DISABLE_USB_SERIAL_JTAG_SECONDARY = "disable_usb_serial_jtag_secondary"
|
||||
@@ -2218,6 +2225,7 @@ FRAMEWORK_SCHEMA = cv.Schema(
|
||||
CONF_INCLUDE_BUILTIN_IDF_COMPONENTS, default=[]
|
||||
): cv.ensure_list(cv.string_strict),
|
||||
cv.Optional(CONF_ENABLE_FULL_PRINTF, default=False): cv.boolean,
|
||||
cv.Optional(CONF_ENABLE_FULL_SCANF): cv.boolean,
|
||||
cv.Optional(CONF_DISABLE_DEBUG_STUBS, default=True): cv.boolean,
|
||||
cv.Optional(CONF_DISABLE_OCD_AWARE, default=True): cv.boolean,
|
||||
cv.Optional(
|
||||
@@ -2519,6 +2527,32 @@ async def _set_libc_picolibc_newlib_compat() -> None:
|
||||
)
|
||||
|
||||
|
||||
def _add_wrap_stub(symbol: str, define: str) -> None:
|
||||
"""Emit a linker wrap for ``symbol`` served by a stub compiled under ``define``.
|
||||
|
||||
--undefined pulls the stub from libsrc.a, scanned before the IDF library that
|
||||
references the symbol.
|
||||
"""
|
||||
cg.add_define(define)
|
||||
cg.add_build_flag(f"-Wl,--wrap={symbol}")
|
||||
cg.add_build_flag(f"-Wl,--undefined=__wrap_{symbol}")
|
||||
|
||||
|
||||
@coroutine_with_priority(CoroPriority.FINAL)
|
||||
async def _add_sscanf_stub(enable_full_scanf: bool | None) -> None:
|
||||
"""Wrap sscanf when bluedroid is the only reason newlib's scanf engine links.
|
||||
|
||||
FINAL priority so every request_bluetooth() call has happened.
|
||||
"""
|
||||
if get_esp32_variant() in ROM_SSCANF_VARIANTS or not _network_sdkconfig().bluetooth:
|
||||
return
|
||||
if keep_float_scanf(
|
||||
enable_full_scanf, CORE.config, "~13KB flash", "that call will abort the device"
|
||||
):
|
||||
return
|
||||
_add_wrap_stub("sscanf", "USE_ESP32_SSCANF_STUB")
|
||||
|
||||
|
||||
@coroutine_with_priority(CoroPriority.FINAL)
|
||||
async def _apply_nvs_cache_in_psram(explicit: bool) -> None:
|
||||
"""Keep the NVS cache in PSRAM unless NVS encryption is on, however it was enabled."""
|
||||
@@ -2959,9 +2993,8 @@ async def to_code(config):
|
||||
# saves nothing while costing ~170 B of shim. IDF 5.x defaults to
|
||||
# newlib on every variant; IDF 6.0+ switches to picolibc on every
|
||||
# variant.
|
||||
if conf[CONF_ADVANCED][CONF_ENABLE_FULL_PRINTF] or idf_version() >= cv.Version(
|
||||
6, 0, 0
|
||||
):
|
||||
uses_newlib = idf_version() < cv.Version(6, 0, 0)
|
||||
if conf[CONF_ADVANCED][CONF_ENABLE_FULL_PRINTF] or not uses_newlib:
|
||||
cg.add_define("USE_FULL_PRINTF")
|
||||
else:
|
||||
for symbol in ("vprintf", "printf", "fprintf", "vfprintf"):
|
||||
@@ -2969,14 +3002,14 @@ async def to_code(config):
|
||||
# esp_http_client calls vasprintf, which on the ESP32-C6 is the only
|
||||
# reference to newlib's full printf engine (~20 KB: _svfprintf_r,
|
||||
# _dtoa_r and their helpers); every other caller resolves to the
|
||||
# ROM. See vasprintf_stubs.cpp. The --undefined flag is needed
|
||||
# because libsrc.a is scanned before the IDF libraries that
|
||||
# reference the symbol, so the stub would otherwise never be pulled
|
||||
# from the archive.
|
||||
# ROM. See vasprintf_stubs.cpp.
|
||||
if variant in ROM_VSNPRINTF_WITHOUT_VASPRINTF_VARIANTS:
|
||||
cg.add_define("USE_ESP32_VASPRINTF_STUB")
|
||||
cg.add_build_flag("-Wl,--wrap=vasprintf")
|
||||
cg.add_build_flag("-Wl,--undefined=__wrap_vasprintf")
|
||||
_add_wrap_stub("vasprintf", "USE_ESP32_VASPRINTF_STUB")
|
||||
if uses_newlib:
|
||||
# bluedroid's sscanf calls; see sscanf_stubs.cpp
|
||||
CORE.add_job(
|
||||
_add_sscanf_stub, conf[CONF_ADVANCED].get(CONF_ENABLE_FULL_SCANF)
|
||||
)
|
||||
else:
|
||||
cg.add_build_flag("-DUSE_ARDUINO")
|
||||
cg.add_build_flag("-DUSE_ESP32_FRAMEWORK_ARDUINO")
|
||||
@@ -3966,3 +3999,12 @@ def process_stacktrace(config, line, backtrace_state):
|
||||
_decode_pc(config, addr.group())
|
||||
|
||||
return backtrace_state
|
||||
|
||||
|
||||
# Each stub only compiles under its define, so keep the sources out of other builds
|
||||
FILTER_SOURCE_FILES = filter_source_files_from_defines(
|
||||
{
|
||||
"sscanf_stubs.cpp": "USE_ESP32_SSCANF_STUB",
|
||||
"vasprintf_stubs.cpp": "USE_ESP32_VASPRINTF_STUB",
|
||||
}
|
||||
)
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Linker wrap for sscanf on ESP-IDF with newlib.
|
||||
*
|
||||
* bluedroid's two "%02x" parses are the only sscanf callers in an ESPHome
|
||||
* image and link newlib's whole scanf engine (~13 KB with _strtod_l). Routing
|
||||
* them through core/sscanf_no_float.h drops it; floats would pull _strtod_l
|
||||
* back, so those abort naming enable_full_scanf. See _add_sscanf_stub in
|
||||
* esp32/__init__.py for when this is emitted.
|
||||
*/
|
||||
|
||||
#include "esphome/core/defines.h"
|
||||
|
||||
#if defined(USE_ESP_IDF) && defined(USE_ESP32_SSCANF_STUB)
|
||||
|
||||
#include <cstdarg>
|
||||
#include <cstdio>
|
||||
|
||||
#include "esp_system.h"
|
||||
#include "esphome/core/sscanf_no_float.h"
|
||||
|
||||
namespace esphome::esp32 {}
|
||||
|
||||
// NOLINTBEGIN(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp,readability-identifier-naming)
|
||||
extern "C" {
|
||||
|
||||
int __wrap_sscanf(const char *str, const char *fmt, ...) {
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
int result = esphome::vsscanf_no_float(str, fmt, ap);
|
||||
va_end(ap);
|
||||
if (result == esphome::SSCANF_UNSUPPORTED) {
|
||||
char msg[160];
|
||||
snprintf(msg, sizeof(msg),
|
||||
"sscanf: unsupported conversion in \"%s\"; set enable_full_scanf: true in esp32 framework", fmt);
|
||||
esp_system_abort(msg);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
} // extern "C"
|
||||
// NOLINTEND(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp,readability-identifier-naming)
|
||||
|
||||
#endif // USE_ESP_IDF && USE_ESP32_SSCANF_STUB
|
||||
@@ -6,6 +6,7 @@ import subprocess
|
||||
from typing import Any
|
||||
|
||||
from esphome.build_helpers.native import native_backend
|
||||
from esphome.code_scan import keep_float_scanf
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import (
|
||||
@@ -25,13 +26,7 @@ from esphome.const import (
|
||||
ThreadModel,
|
||||
Toolchain,
|
||||
)
|
||||
from esphome.core import (
|
||||
CORE,
|
||||
CoroPriority,
|
||||
EsphomeError,
|
||||
Lambda,
|
||||
coroutine_with_priority,
|
||||
)
|
||||
from esphome.core import CORE, CoroPriority, EsphomeError, coroutine_with_priority
|
||||
from esphome.core.config import BOARD_MAX_LENGTH
|
||||
from esphome.helpers import IS_MACOS, copy_file_if_changed
|
||||
from esphome.platformio.toolchain import copy_ccache_script
|
||||
@@ -61,15 +56,6 @@ from .const import (
|
||||
from .gpio import PinInitialState, add_pin_initial_states_array
|
||||
|
||||
CONF_ENABLE_SCANF_FLOAT = "enable_scanf_float"
|
||||
# Heuristically matches scanf/sscanf calls with float format specifiers.
|
||||
# Standard scanf float conversions: %f %F %e %E %g %G %a %A
|
||||
# With optional modifiers: %*f (suppression), %8f (width), %lf %Lf (length)
|
||||
# Also matches non-standard patterns like %.2f as a heuristic — these are
|
||||
# invalid in scanf but users may write them by analogy with printf.
|
||||
# Uses [^;]*? to stay within a single statement, preventing false positives
|
||||
# from e.g. sscanf(buf, "%d", &x); printf("%f", val);
|
||||
_SCANF_FLOAT_RE = re.compile(r"scanf\s*\([^;]*?%[*\d.]*[hlL]*[feEgGaAF]")
|
||||
|
||||
CODEOWNERS = ["@esphome/core"]
|
||||
DOMAIN = "esp8266"
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@@ -77,26 +63,6 @@ AUTO_LOAD = ["preferences"]
|
||||
IS_TARGET_PLATFORM = True
|
||||
|
||||
|
||||
def lambdas_use_scanf_float(config: ConfigType) -> bool:
|
||||
"""Check if any lambda in the config uses scanf with a float format specifier.
|
||||
|
||||
Comments are stripped before matching to avoid false positives from
|
||||
commented-out code. The cost of a false positive is only ~8KB flash.
|
||||
"""
|
||||
stack: list = [config]
|
||||
while stack:
|
||||
obj = stack.pop()
|
||||
if isinstance(obj, Lambda):
|
||||
src = obj.comment_remover(obj.value)
|
||||
if _SCANF_FLOAT_RE.search(src):
|
||||
return True
|
||||
elif isinstance(obj, dict):
|
||||
stack.extend(obj.values())
|
||||
elif isinstance(obj, list):
|
||||
stack.extend(obj)
|
||||
return False
|
||||
|
||||
|
||||
def set_core_data(config: ConfigType) -> ConfigType:
|
||||
CORE.data[KEY_ESP8266] = {}
|
||||
CORE.data[KEY_CORE][KEY_TARGET_PLATFORM] = PLATFORM_ESP8266
|
||||
@@ -417,13 +383,12 @@ async def to_code(config: ConfigType) -> None:
|
||||
cg.add_define(ThreadModel.SINGLE)
|
||||
cg.add_define("USE_ESP8266_CRASH_HANDLER")
|
||||
|
||||
enable_scanf_float = config.get(CONF_ENABLE_SCANF_FLOAT)
|
||||
if enable_scanf_float is None and lambdas_use_scanf_float(CORE.config):
|
||||
enable_scanf_float = True
|
||||
_LOGGER.warning(
|
||||
"Lambda uses scanf with a float format specifier; "
|
||||
"enabling scanf float support (~8KB flash)"
|
||||
)
|
||||
enable_scanf_float = keep_float_scanf(
|
||||
config.get(CONF_ENABLE_SCANF_FLOAT),
|
||||
CORE.config,
|
||||
"~8KB flash",
|
||||
"that call will not parse",
|
||||
)
|
||||
|
||||
# The native generator reads the same decision (KEY_SCANF_FLOAT)
|
||||
CORE.data[KEY_ESP8266][KEY_SCANF_FLOAT] = bool(enable_scanf_float)
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
from collections.abc import Callable, Collection
|
||||
from collections.abc import Callable, Collection, Iterator
|
||||
import logging
|
||||
from pathlib import Path
|
||||
|
||||
from esphome.const import (
|
||||
CONF_LEVEL,
|
||||
@@ -9,8 +11,11 @@ from esphome.const import (
|
||||
PlatformFramework,
|
||||
)
|
||||
from esphome.core import CORE
|
||||
from esphome.helpers import walk_files
|
||||
from esphome.util import OrderedDict
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
# Pre-build lookup map from (platform, framework) tuples to PlatformFramework enum
|
||||
_PLATFORM_FRAMEWORK_LOOKUP = {
|
||||
(pf.value[0].value, pf.value[1].value): pf for pf in PlatformFramework
|
||||
@@ -191,3 +196,16 @@ def get_logger_level() -> str:
|
||||
|
||||
logger_config = CORE.config[CONF_LOGGER]
|
||||
return logger_config.get(CONF_LEVEL, "DEBUG")
|
||||
|
||||
|
||||
def iter_include_files(includes: list[str]) -> Iterator[tuple[Path, Path]]:
|
||||
"""Yield ``(path, basename)`` per local ``includes:`` file; directory members keep the directory prefix."""
|
||||
for include in includes:
|
||||
if include.startswith("<") and include.endswith(">"):
|
||||
continue
|
||||
path = CORE.relative_config_path(include)
|
||||
if path.is_dir():
|
||||
for file in walk_files(path):
|
||||
yield file, file.relative_to(path.parent)
|
||||
else:
|
||||
yield path, Path(path.name)
|
||||
|
||||
+5
-13
@@ -10,6 +10,7 @@ import esphome.codegen as cg
|
||||
from esphome.config_helpers import (
|
||||
filter_source_files_from_defines,
|
||||
filter_source_files_from_platform,
|
||||
iter_include_files,
|
||||
)
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import (
|
||||
@@ -48,6 +49,7 @@ from esphome.const import (
|
||||
PLATFORM_ESP32,
|
||||
PLATFORM_HOST,
|
||||
PLATFORM_RP2,
|
||||
SOURCE_FILE_EXTENSIONS,
|
||||
PlatformFramework,
|
||||
__version__ as ESPHOME_VERSION,
|
||||
)
|
||||
@@ -64,7 +66,6 @@ from esphome.helpers import (
|
||||
fnv1a_32bit_hash,
|
||||
get_str_env,
|
||||
get_usable_cpu_count,
|
||||
walk_files,
|
||||
)
|
||||
from esphome.types import ConfigType
|
||||
|
||||
@@ -120,7 +121,7 @@ ProjectUpdateTrigger = cg.esphome_ns.class_(
|
||||
Device = cg.esphome_ns.class_("Device")
|
||||
Area = cg.esphome_ns.class_("Area")
|
||||
|
||||
VALID_INCLUDE_EXTS = {".h", ".hpp", ".tcc", ".ino", ".cpp", ".c"}
|
||||
VALID_INCLUDE_EXTS = SOURCE_FILE_EXTENSIONS
|
||||
|
||||
|
||||
def validate_hostname(config):
|
||||
@@ -578,17 +579,8 @@ async def add_arduino_global_workaround():
|
||||
@coroutine_with_priority(CoroPriority.FINAL)
|
||||
async def add_includes(includes: list[str], is_c_header: bool = False) -> None:
|
||||
# Add includes at the very end, so that the included files can access global variables
|
||||
for include in includes:
|
||||
path = CORE.relative_config_path(include)
|
||||
if path.is_dir():
|
||||
# Directory, copy tree
|
||||
for p in walk_files(path):
|
||||
basename = p.relative_to(path.parent)
|
||||
include_file(p, basename, is_c_header)
|
||||
else:
|
||||
# Copy file
|
||||
basename = Path(path.name)
|
||||
include_file(path, basename, is_c_header)
|
||||
for path, basename in iter_include_files(includes):
|
||||
include_file(path, basename, is_c_header)
|
||||
|
||||
|
||||
def _add_library_str(lib: str) -> None:
|
||||
|
||||
@@ -364,6 +364,7 @@
|
||||
#ifdef USE_ESP32
|
||||
#define USE_ESP32_CRASH_HANDLER
|
||||
#define USE_ESP32_VASPRINTF_STUB
|
||||
#define USE_ESP32_SSCANF_STUB
|
||||
#define USE_MQTT_IDF_ENQUEUE
|
||||
#define USE_ESPHOME_TASK_LOG_BUFFER
|
||||
#define ESPHOME_TASK_LOG_BUFFER_SIZE 768
|
||||
|
||||
@@ -0,0 +1,277 @@
|
||||
#pragma once
|
||||
|
||||
// sscanf without floating-point conversions, backing the linker wrap in
|
||||
// components/esp32/sscanf_stubs.cpp. Integer, character, string and scanset
|
||||
// conversions behave like libc; anything else returns SSCANF_UNSUPPORTED.
|
||||
// Header only so the host unit tests can cover it.
|
||||
|
||||
#include <cstdarg>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
|
||||
#include "esphome/core/helpers.h"
|
||||
|
||||
namespace esphome {
|
||||
|
||||
/// Returned by vsscanf_no_float() for a conversion it does not implement.
|
||||
static constexpr int SSCANF_UNSUPPORTED = -2;
|
||||
|
||||
namespace sscanf_no_float_internal {
|
||||
|
||||
enum class SscanfLength : uint8_t {
|
||||
SSCANF_LENGTH_NONE,
|
||||
SSCANF_LENGTH_HH,
|
||||
SSCANF_LENGTH_H,
|
||||
SSCANF_LENGTH_L,
|
||||
SSCANF_LENGTH_LL,
|
||||
SSCANF_LENGTH_Z,
|
||||
SSCANF_LENGTH_T,
|
||||
SSCANF_LENGTH_PTR, // %p
|
||||
};
|
||||
|
||||
inline bool is_space(char c) { return c == ' ' || (c >= '\t' && c <= '\r'); }
|
||||
|
||||
// The unsigned bit pattern is what the signed conversions want as well
|
||||
inline void store_int(void *dest, SscanfLength length, uint64_t value) {
|
||||
switch (length) {
|
||||
case SscanfLength::SSCANF_LENGTH_HH:
|
||||
*static_cast<uint8_t *>(dest) = static_cast<uint8_t>(value);
|
||||
break;
|
||||
case SscanfLength::SSCANF_LENGTH_H:
|
||||
*static_cast<uint16_t *>(dest) = static_cast<uint16_t>(value);
|
||||
break;
|
||||
case SscanfLength::SSCANF_LENGTH_L:
|
||||
// %l is defined in terms of long, whatever its width on the target
|
||||
*static_cast<unsigned long *>(dest) = static_cast<unsigned long>(value); // NOLINT(google-runtime-int)
|
||||
break;
|
||||
case SscanfLength::SSCANF_LENGTH_LL:
|
||||
*static_cast<uint64_t *>(dest) = value;
|
||||
break;
|
||||
case SscanfLength::SSCANF_LENGTH_Z:
|
||||
*static_cast<size_t *>(dest) = static_cast<size_t>(value);
|
||||
break;
|
||||
case SscanfLength::SSCANF_LENGTH_T:
|
||||
*static_cast<ptrdiff_t *>(dest) = static_cast<ptrdiff_t>(value);
|
||||
break;
|
||||
case SscanfLength::SSCANF_LENGTH_PTR:
|
||||
*static_cast<uintptr_t *>(dest) = static_cast<uintptr_t>(value);
|
||||
break;
|
||||
default:
|
||||
*static_cast<unsigned *>(dest) = static_cast<unsigned>(value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
struct Scanset {
|
||||
uint8_t bits[32]{};
|
||||
bool negate{false};
|
||||
|
||||
bool contains(char c) const {
|
||||
auto u = static_cast<uint8_t>(c);
|
||||
return ((this->bits[u >> 3] >> (u & 7)) & 1u) != this->negate;
|
||||
}
|
||||
};
|
||||
|
||||
// Parses "[set]" from just after the '[' and returns the position after ']'
|
||||
inline const char *parse_scanset(const char *f, Scanset &set) {
|
||||
if (*f == '^') {
|
||||
set.negate = true;
|
||||
f++;
|
||||
}
|
||||
// A ']' right after '[' or '[^' is a member, not the terminator.
|
||||
bool first = true;
|
||||
for (; *f != '\0' && (first || *f != ']'); f++) {
|
||||
first = false;
|
||||
auto lo = static_cast<uint8_t>(*f);
|
||||
auto hi = lo;
|
||||
if (f[1] == '-' && f[2] != ']' && f[2] != '\0') {
|
||||
hi = static_cast<uint8_t>(f[2]);
|
||||
f += 2;
|
||||
}
|
||||
for (unsigned c = lo; c <= hi; c++)
|
||||
set.bits[c >> 3] |= static_cast<uint8_t>(1u << (c & 7));
|
||||
}
|
||||
return *f == ']' ? f + 1 : f;
|
||||
}
|
||||
|
||||
// Radix of an integer conversion: 0 detects from the prefix (%i), 0xFF is unsupported
|
||||
inline unsigned int_base(char conv) {
|
||||
switch (conv) {
|
||||
case 'd':
|
||||
case 'u':
|
||||
return 10;
|
||||
case 'i':
|
||||
return 0;
|
||||
case 'o':
|
||||
return 8;
|
||||
case 'x':
|
||||
case 'X':
|
||||
case 'p':
|
||||
return 16;
|
||||
default:
|
||||
return 0xFF;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace sscanf_no_float_internal
|
||||
|
||||
/// sscanf semantics for the integer, character, string and scanset
|
||||
/// conversions; returns SSCANF_UNSUPPORTED on any other conversion.
|
||||
inline int vsscanf_no_float(const char *str, const char *fmt, va_list ap) { // NOLINT(readability-non-const-parameter)
|
||||
using namespace sscanf_no_float_internal;
|
||||
const char *in = str;
|
||||
int assigned = 0;
|
||||
bool input_failure = false;
|
||||
for (const char *f = fmt; *f != '\0'; f++) {
|
||||
if (is_space(*f)) {
|
||||
while (is_space(*in))
|
||||
in++;
|
||||
continue;
|
||||
}
|
||||
bool literal = *f != '%';
|
||||
if (!literal && f[1] == '%') {
|
||||
// "%%" matches one '%' with no whitespace skip, like newlib
|
||||
f++;
|
||||
literal = true;
|
||||
}
|
||||
if (literal) {
|
||||
if (*in != *f) {
|
||||
input_failure = *in == '\0';
|
||||
break;
|
||||
}
|
||||
in++;
|
||||
continue;
|
||||
}
|
||||
f++;
|
||||
bool suppress = false;
|
||||
if (*f == '*') {
|
||||
suppress = true;
|
||||
f++;
|
||||
}
|
||||
size_t width = 0;
|
||||
while (*f >= '0' && *f <= '9') {
|
||||
width = width * 10 + static_cast<size_t>(*f - '0');
|
||||
f++;
|
||||
}
|
||||
if (width == 0)
|
||||
width = SIZE_MAX;
|
||||
auto length = SscanfLength::SSCANF_LENGTH_NONE;
|
||||
switch (*f) {
|
||||
case 'h':
|
||||
f++;
|
||||
if (*f == 'h') {
|
||||
length = SscanfLength::SSCANF_LENGTH_HH;
|
||||
f++;
|
||||
} else {
|
||||
length = SscanfLength::SSCANF_LENGTH_H;
|
||||
}
|
||||
break;
|
||||
case 'l':
|
||||
f++;
|
||||
if (*f == 'l') {
|
||||
length = SscanfLength::SSCANF_LENGTH_LL;
|
||||
f++;
|
||||
} else {
|
||||
length = SscanfLength::SSCANF_LENGTH_L;
|
||||
}
|
||||
break;
|
||||
case 'j':
|
||||
length = SscanfLength::SSCANF_LENGTH_LL;
|
||||
f++;
|
||||
break;
|
||||
case 'z':
|
||||
length = SscanfLength::SSCANF_LENGTH_Z;
|
||||
f++;
|
||||
break;
|
||||
case 't':
|
||||
length = SscanfLength::SSCANF_LENGTH_T;
|
||||
f++;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
const char conv = *f;
|
||||
if (conv == '\0')
|
||||
break;
|
||||
if (conv == 'n') {
|
||||
if (!suppress)
|
||||
store_int(va_arg(ap, void *), length, static_cast<uint64_t>(in - str));
|
||||
continue;
|
||||
}
|
||||
const bool string_conv = conv == 'c' || conv == 's' || conv == '[';
|
||||
if (string_conv && length != SscanfLength::SSCANF_LENGTH_NONE)
|
||||
return SSCANF_UNSUPPORTED; // wide characters
|
||||
if (conv == 'p')
|
||||
length = SscanfLength::SSCANF_LENGTH_PTR;
|
||||
unsigned base = string_conv ? 0 : int_base(conv);
|
||||
if (base == 0xFF)
|
||||
return SSCANF_UNSUPPORTED;
|
||||
if (conv != 'c' && conv != '[') {
|
||||
while (is_space(*in))
|
||||
in++;
|
||||
}
|
||||
if (*in == '\0') {
|
||||
input_failure = true;
|
||||
break;
|
||||
}
|
||||
if (!string_conv) {
|
||||
const char *p = in;
|
||||
size_t n = 0;
|
||||
bool negative = false;
|
||||
if (*p == '+' || *p == '-') {
|
||||
negative = *p == '-';
|
||||
p++;
|
||||
n++;
|
||||
}
|
||||
if ((base == 0 || base == 16) && p[0] == '0' && (p[1] == 'x' || p[1] == 'X') && n + 2 < width &&
|
||||
parse_hex_char(p[2]) != INVALID_HEX_CHAR) {
|
||||
p += 2;
|
||||
n += 2;
|
||||
base = 16;
|
||||
}
|
||||
if (base == 0)
|
||||
base = *p == '0' ? 8 : 10;
|
||||
uint64_t value = 0;
|
||||
bool any_digit = false;
|
||||
for (uint8_t digit; n < width && (digit = parse_hex_char(*p)) < base; p++, n++) {
|
||||
value = value * base + digit;
|
||||
any_digit = true;
|
||||
}
|
||||
if (!any_digit)
|
||||
break;
|
||||
in = p;
|
||||
if (!suppress) {
|
||||
store_int(va_arg(ap, void *), length, negative ? 0 - value : value);
|
||||
assigned++;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
char *dest = suppress ? nullptr : va_arg(ap, char *);
|
||||
Scanset set;
|
||||
if (conv == '[')
|
||||
f = parse_scanset(f + 1, set) - 1; // the loop's f++ steps past the ']'
|
||||
// %c reads width characters (default 1); a short read still assigns, like newlib
|
||||
const size_t limit = conv == 'c' && width == SIZE_MAX ? 1 : width;
|
||||
size_t n = 0;
|
||||
while (n < limit && in[n] != '\0' && (conv == 'c' || (conv == '[' ? set.contains(in[n]) : !is_space(in[n])))) {
|
||||
if (dest != nullptr)
|
||||
dest[n] = in[n];
|
||||
n++;
|
||||
}
|
||||
if (conv != 'c') {
|
||||
if (n == 0)
|
||||
break;
|
||||
if (dest != nullptr)
|
||||
dest[n] = '\0';
|
||||
}
|
||||
in += n;
|
||||
if (!suppress)
|
||||
assigned++;
|
||||
}
|
||||
if (input_failure && assigned == 0)
|
||||
return EOF;
|
||||
return assigned;
|
||||
}
|
||||
|
||||
} // namespace esphome
|
||||
@@ -217,6 +217,8 @@ ESPHOME_H_EXCLUDE = {
|
||||
Path(ENTITY_TYPES_H_TARGET),
|
||||
# main.cpp includes it after defining esphome_controllers()
|
||||
Path("esphome/core/controller_dispatch.h"),
|
||||
# only the esp32 sscanf wrap includes it
|
||||
Path("esphome/core/sscanf_no_float.h"),
|
||||
# build machinery, not user API
|
||||
Path("esphome/core/pch_prefix.h"),
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
esphome:
|
||||
name: test
|
||||
|
||||
esp32:
|
||||
variant: esp32c6
|
||||
framework:
|
||||
type: esp-idf
|
||||
|
||||
esp32_ble_tracker:
|
||||
@@ -0,0 +1,11 @@
|
||||
esphome:
|
||||
name: test
|
||||
|
||||
esp32:
|
||||
board: esp32dev
|
||||
framework:
|
||||
type: esp-idf
|
||||
advanced:
|
||||
enable_full_printf: true
|
||||
|
||||
esp32_ble_tracker:
|
||||
@@ -0,0 +1,11 @@
|
||||
esphome:
|
||||
name: test
|
||||
|
||||
esp32:
|
||||
board: esp32dev
|
||||
framework:
|
||||
type: esp-idf
|
||||
advanced:
|
||||
enable_full_scanf: true
|
||||
|
||||
esp32_ble_tracker:
|
||||
@@ -0,0 +1,17 @@
|
||||
esphome:
|
||||
name: test
|
||||
|
||||
esp32:
|
||||
board: esp32dev
|
||||
framework:
|
||||
type: esp-idf
|
||||
|
||||
esp32_ble_tracker:
|
||||
|
||||
sensor:
|
||||
- platform: template
|
||||
name: Parsed
|
||||
lambda: |-
|
||||
float value = 0;
|
||||
sscanf("1.5", "%f", &value);
|
||||
return value;
|
||||
@@ -1881,28 +1881,51 @@ def test_mbedtls_tls_zigbee_requires_extras(
|
||||
assert mbedtls.tls_extras_required == _CCM_ECDSA_EXTRAS
|
||||
|
||||
|
||||
_VASPRINTF_STUB_FLAGS = {"-Wl,--wrap=vasprintf", "-Wl,--undefined=__wrap_vasprintf"}
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("config_file", "expected"),
|
||||
("config_file", "symbol", "expected"),
|
||||
[
|
||||
pytest.param("vasprintf_stub_c6.yaml", True, id="c6"),
|
||||
pytest.param("vasprintf_stub_c6_full_printf.yaml", False, id="c6_full_printf"),
|
||||
pytest.param("exclusion_reincludes.yaml", False, id="esp32"),
|
||||
# vasprintf: only where the ROM lacks vasprintf but has vsnprintf
|
||||
pytest.param("vasprintf_stub_c6.yaml", "vasprintf", True, id="vasprintf_c6"),
|
||||
pytest.param(
|
||||
"vasprintf_stub_c6_full_printf.yaml",
|
||||
"vasprintf",
|
||||
False,
|
||||
id="vasprintf_c6_full_printf",
|
||||
),
|
||||
pytest.param(
|
||||
"exclusion_reincludes.yaml", "vasprintf", False, id="vasprintf_esp32"
|
||||
),
|
||||
# sscanf: only for Bluetooth builds on variants whose ROM lacks sscanf,
|
||||
# and not when a lambda scans a float
|
||||
pytest.param(
|
||||
"exclusion_reincludes_wifi_ble.yaml", "sscanf", True, id="sscanf_esp32_ble"
|
||||
),
|
||||
pytest.param("sscanf_stub_ble_c6.yaml", "sscanf", False, id="sscanf_c6_rom"),
|
||||
pytest.param(
|
||||
"sscanf_stub_ble_full_scanf.yaml", "sscanf", False, id="sscanf_full_scanf"
|
||||
),
|
||||
pytest.param(
|
||||
"sscanf_stub_ble_lambda.yaml", "sscanf", False, id="sscanf_lambda_float"
|
||||
),
|
||||
pytest.param(
|
||||
"sscanf_stub_ble_full_printf.yaml", "sscanf", True, id="sscanf_full_printf"
|
||||
),
|
||||
pytest.param("exclusion_reincludes.yaml", "sscanf", False, id="sscanf_no_ble"),
|
||||
],
|
||||
)
|
||||
def test_vasprintf_stub_only_on_rom_vsnprintf_variants(
|
||||
def test_wrap_stub_emission(
|
||||
generate_main: Callable[[str | Path], str],
|
||||
component_config_path: Callable[[str], Path],
|
||||
config_file: str,
|
||||
symbol: str,
|
||||
expected: bool,
|
||||
) -> None:
|
||||
"""The vasprintf wrap is emitted only where the ROM lacks vasprintf but has vsnprintf."""
|
||||
"""A linker wrap stub is emitted as a define plus --wrap/--undefined flag pair."""
|
||||
generate_main(component_config_path(config_file))
|
||||
assert (CORE.build_flags >= _VASPRINTF_STUB_FLAGS) is expected
|
||||
flags = {f"-Wl,--wrap={symbol}", f"-Wl,--undefined=__wrap_{symbol}"}
|
||||
assert (CORE.build_flags >= flags) is expected
|
||||
defines = {define.name for define in CORE.defines}
|
||||
assert ("USE_ESP32_VASPRINTF_STUB" in defines) is expected
|
||||
assert (f"USE_ESP32_{symbol.upper()}_STUB" in defines) is expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
|
||||
@@ -0,0 +1,250 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <cstdarg>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
|
||||
#include "esphome/core/sscanf_no_float.h"
|
||||
|
||||
namespace esphome::core::testing {
|
||||
|
||||
static int sscanf_no_float(const char *str, const char *fmt, ...) {
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
int result = vsscanf_no_float(str, fmt, ap);
|
||||
va_end(ap);
|
||||
return result;
|
||||
}
|
||||
|
||||
// --- The two bluedroid call sites the wrap exists for ---
|
||||
|
||||
TEST(SscanfNoFloat, BluedroidHexByte) {
|
||||
unsigned int value = 0;
|
||||
EXPECT_EQ(sscanf_no_float("ab", "%02x", &value), 1);
|
||||
EXPECT_EQ(value, 0xabu);
|
||||
// Width stops after two digits even when more follow
|
||||
EXPECT_EQ(sscanf_no_float("ab12", "%02x", &value), 1);
|
||||
EXPECT_EQ(value, 0xabu);
|
||||
}
|
||||
|
||||
TEST(SscanfNoFloat, BluedroidAddress) {
|
||||
uint32_t a[6] = {0};
|
||||
EXPECT_EQ(sscanf_no_float("AA:bb:0C:dd:ee:FF", "%02x:%02x:%02x:%02x:%02x:%02x", a, a + 1, a + 2, a + 3, a + 4, a + 5),
|
||||
6);
|
||||
EXPECT_EQ(a[0], 0xaau);
|
||||
EXPECT_EQ(a[1], 0xbbu);
|
||||
EXPECT_EQ(a[2], 0x0cu);
|
||||
EXPECT_EQ(a[3], 0xddu);
|
||||
EXPECT_EQ(a[4], 0xeeu);
|
||||
EXPECT_EQ(a[5], 0xffu);
|
||||
// Short input assigns what matched, like libc
|
||||
EXPECT_EQ(sscanf_no_float("AA:bb:0C", "%02x:%02x:%02x:%02x:%02x:%02x", a, a + 1, a + 2, a + 3, a + 4, a + 5), 3);
|
||||
}
|
||||
|
||||
TEST(SscanfNoFloat, WidthWithByteLength) {
|
||||
uint8_t mac[2] = {0};
|
||||
EXPECT_EQ(sscanf_no_float("AA:bb", "%02hhx:%02hhx", mac, mac + 1), 2);
|
||||
EXPECT_EQ(mac[0], 0xaa);
|
||||
EXPECT_EQ(mac[1], 0xbb);
|
||||
// The width still bounds the digits when a length modifier is present
|
||||
EXPECT_EQ(sscanf_no_float("abcd", "%02hhx", mac), 1);
|
||||
EXPECT_EQ(mac[0], 0xab);
|
||||
}
|
||||
|
||||
// --- Formats bluedroid's hands-free client uses: %hu with %n ---
|
||||
|
||||
TEST(SscanfNoFloat, ShortWithPosition) {
|
||||
uint16_t h = 0, m = 0, s = 0;
|
||||
int n1 = 0, n2 = 0, n3 = 0;
|
||||
EXPECT_EQ(sscanf_no_float("6:30:15", "%hu%n:%hu%n:%hu%n", &h, &n1, &m, &n2, &s, &n3), 3);
|
||||
EXPECT_EQ(h, 6);
|
||||
EXPECT_EQ(m, 30);
|
||||
EXPECT_EQ(s, 15);
|
||||
EXPECT_EQ(n1, 1);
|
||||
EXPECT_EQ(n2, 4);
|
||||
EXPECT_EQ(n3, 7);
|
||||
EXPECT_EQ(sscanf_no_float("M3.2.0", "M%hu%n.%hu%n.%hu%n", &h, &n1, &m, &n2, &s, &n3), 3);
|
||||
EXPECT_EQ(sscanf_no_float("X3.2.0", "M%hu", &h), 0);
|
||||
}
|
||||
|
||||
// --- Integer conversions ---
|
||||
|
||||
TEST(SscanfNoFloat, SignedDecimalSkipsLeadingSpace) {
|
||||
int value = 0;
|
||||
EXPECT_EQ(sscanf_no_float(" -42 rest", "%d", &value), 1);
|
||||
EXPECT_EQ(value, -42);
|
||||
EXPECT_EQ(sscanf_no_float("+7", "%u", &value), 1);
|
||||
EXPECT_EQ(value, 7);
|
||||
}
|
||||
|
||||
TEST(SscanfNoFloat, AutoBase) {
|
||||
int value = 0;
|
||||
EXPECT_EQ(sscanf_no_float("0x1F", "%i", &value), 1);
|
||||
EXPECT_EQ(value, 31);
|
||||
EXPECT_EQ(sscanf_no_float("017", "%i", &value), 1);
|
||||
EXPECT_EQ(value, 15);
|
||||
EXPECT_EQ(sscanf_no_float("0", "%i", &value), 1);
|
||||
EXPECT_EQ(value, 0);
|
||||
EXPECT_EQ(sscanf_no_float("-0x10", "%i", &value), 1);
|
||||
EXPECT_EQ(value, -16);
|
||||
}
|
||||
|
||||
TEST(SscanfNoFloat, OctalAndHexPrefix) {
|
||||
unsigned int value = 0;
|
||||
EXPECT_EQ(sscanf_no_float("17", "%o", &value), 1);
|
||||
EXPECT_EQ(value, 15u);
|
||||
EXPECT_EQ(sscanf_no_float("+0x10", "%x", &value), 1);
|
||||
EXPECT_EQ(value, 16u);
|
||||
// "0x" with no digit after it: only the 0 is consumed
|
||||
EXPECT_EQ(sscanf_no_float("0xg", "%x", &value), 1);
|
||||
EXPECT_EQ(value, 0u);
|
||||
EXPECT_EQ(sscanf_no_float("zz", "%x", &value), 0);
|
||||
}
|
||||
|
||||
TEST(SscanfNoFloat, WidthSplitsDigits) {
|
||||
int a = 0;
|
||||
unsigned int b = 0;
|
||||
EXPECT_EQ(sscanf_no_float("12345", "%3d%d", &a, &b), 2);
|
||||
EXPECT_EQ(a, 123);
|
||||
EXPECT_EQ(b, 45u);
|
||||
EXPECT_EQ(sscanf_no_float("ff", "%1x", &b), 1);
|
||||
EXPECT_EQ(b, 15u);
|
||||
}
|
||||
|
||||
TEST(SscanfNoFloat, LengthModifiers) {
|
||||
unsigned char hh = 0;
|
||||
int16_t h = 0;
|
||||
long l = 0; // NOLINT(google-runtime-int) %ld is defined in terms of long
|
||||
uint64_t ll = 0;
|
||||
size_t z = 0;
|
||||
ptrdiff_t t = 0;
|
||||
EXPECT_EQ(sscanf_no_float("255", "%hhu", &hh), 1);
|
||||
EXPECT_EQ(hh, 255);
|
||||
EXPECT_EQ(sscanf_no_float("-2", "%hd", &h), 1);
|
||||
EXPECT_EQ(h, -2);
|
||||
EXPECT_EQ(sscanf_no_float("-1", "%ld", &l), 1);
|
||||
EXPECT_EQ(l, -1L);
|
||||
EXPECT_EQ(sscanf_no_float("18446744073709551615", "%llu", &ll), 1);
|
||||
EXPECT_EQ(ll, UINT64_MAX);
|
||||
EXPECT_EQ(sscanf_no_float("123", "%ju", &ll), 1);
|
||||
EXPECT_EQ(ll, 123u);
|
||||
EXPECT_EQ(sscanf_no_float("9", "%zu", &z), 1);
|
||||
EXPECT_EQ(z, 9u);
|
||||
EXPECT_EQ(sscanf_no_float("-9", "%td", &t), 1);
|
||||
EXPECT_EQ(t, -9);
|
||||
}
|
||||
|
||||
TEST(SscanfNoFloat, Pointer) {
|
||||
void *p = nullptr;
|
||||
EXPECT_EQ(sscanf_no_float("0x1234", "%p", &p), 1);
|
||||
EXPECT_EQ(reinterpret_cast<uintptr_t>(p), 0x1234u);
|
||||
EXPECT_EQ(sscanf_no_float("abcd rest", "%p", &p), 1);
|
||||
EXPECT_EQ(reinterpret_cast<uintptr_t>(p), 0xabcdu);
|
||||
}
|
||||
|
||||
TEST(SscanfNoFloat, Suppression) {
|
||||
int value = 0;
|
||||
EXPECT_EQ(sscanf_no_float("1 2 3", "%*d %d %*d", &value), 1);
|
||||
EXPECT_EQ(value, 2);
|
||||
}
|
||||
|
||||
// --- Character, string and scanset conversions ---
|
||||
|
||||
TEST(SscanfNoFloat, CharDoesNotSkipSpace) {
|
||||
char c[3] = {0};
|
||||
EXPECT_EQ(sscanf_no_float("xyz", "%c%c", c, c + 1), 2);
|
||||
EXPECT_EQ(c[0], 'x');
|
||||
EXPECT_EQ(c[1], 'y');
|
||||
EXPECT_EQ(sscanf_no_float(" x", "%c", c), 1);
|
||||
EXPECT_EQ(c[0], ' ');
|
||||
}
|
||||
|
||||
TEST(SscanfNoFloat, CharWidthShortRead) {
|
||||
char c[4] = {0};
|
||||
EXPECT_EQ(sscanf_no_float("abc", "%2c", c), 1);
|
||||
EXPECT_EQ(c[0], 'a');
|
||||
EXPECT_EQ(c[1], 'b');
|
||||
EXPECT_EQ(c[2], '\0');
|
||||
// Fewer characters than the width still count as one assignment, like newlib
|
||||
EXPECT_EQ(sscanf_no_float("ab", "%3c", c), 1);
|
||||
EXPECT_EQ(sscanf_no_float("", "%c", c), EOF);
|
||||
}
|
||||
|
||||
TEST(SscanfNoFloat, StringStopsAtSpaceAndWidth) {
|
||||
char buf[16] = {0};
|
||||
EXPECT_EQ(sscanf_no_float("hello world", "%5s", buf), 1);
|
||||
EXPECT_STREQ(buf, "hello");
|
||||
EXPECT_EQ(sscanf_no_float(" x", "%1s", buf), 1);
|
||||
EXPECT_STREQ(buf, "x");
|
||||
EXPECT_EQ(sscanf_no_float(" ", "%s", buf), EOF);
|
||||
}
|
||||
|
||||
TEST(SscanfNoFloat, Scansets) {
|
||||
char buf[16] = {0};
|
||||
char rest[16] = {0};
|
||||
EXPECT_EQ(sscanf_no_float("key=val", "%[^=]=%s", buf, rest), 2);
|
||||
EXPECT_STREQ(buf, "key");
|
||||
EXPECT_STREQ(rest, "val");
|
||||
// ']' first is a member; '-' last is literal
|
||||
EXPECT_EQ(sscanf_no_float("]]ab", "%[]]", buf), 1);
|
||||
EXPECT_STREQ(buf, "]]");
|
||||
EXPECT_EQ(sscanf_no_float("a-z]x", "%[a-]", buf), 1);
|
||||
EXPECT_STREQ(buf, "a-");
|
||||
EXPECT_EQ(sscanf_no_float("abc", "%[^]]", buf), 1);
|
||||
EXPECT_STREQ(buf, "abc");
|
||||
// No member matched is a matching failure
|
||||
EXPECT_EQ(sscanf_no_float("123", "%[a-z]", buf), 0);
|
||||
}
|
||||
|
||||
// --- Literals, whitespace and return value ---
|
||||
|
||||
TEST(SscanfNoFloat, LiteralsAndPercent) {
|
||||
int value = 0;
|
||||
EXPECT_EQ(sscanf_no_float("50%", "%d%%", &value), 1);
|
||||
EXPECT_EQ(value, 50);
|
||||
// Like newlib, %% does not skip whitespace; an explicit space in the format does
|
||||
EXPECT_EQ(sscanf_no_float("50 % 7", "%d%%%d", &value, &value), 1);
|
||||
EXPECT_EQ(sscanf_no_float("50 % 7", "%d %%%d", &value, &value), 2);
|
||||
EXPECT_EQ(value, 7);
|
||||
EXPECT_EQ(sscanf_no_float("1,2", "%d,%d", &value, &value), 2);
|
||||
EXPECT_EQ(sscanf_no_float("1 ,2", "%d,%d", &value, &value), 1);
|
||||
EXPECT_EQ(sscanf_no_float("1, 2", "%d,%d", &value, &value), 2);
|
||||
EXPECT_EQ(sscanf_no_float("12\n34", "%d%d", &value, &value), 2);
|
||||
}
|
||||
|
||||
TEST(SscanfNoFloat, PositionOnly) {
|
||||
int n = 0;
|
||||
EXPECT_EQ(sscanf_no_float("ab", "ab%n", &n), 0);
|
||||
EXPECT_EQ(n, 2);
|
||||
EXPECT_EQ(sscanf_no_float("", "%n", &n), 0);
|
||||
EXPECT_EQ(n, 0);
|
||||
}
|
||||
|
||||
TEST(SscanfNoFloat, EofVersusMatchingFailure) {
|
||||
int value = 0;
|
||||
EXPECT_EQ(sscanf_no_float("", "%d", &value), EOF);
|
||||
EXPECT_EQ(sscanf_no_float(" ", "%d", &value), EOF);
|
||||
EXPECT_EQ(sscanf_no_float("a", "ab"), EOF);
|
||||
EXPECT_EQ(sscanf_no_float("abc", "%d", &value), 0);
|
||||
EXPECT_EQ(sscanf_no_float("ac", "ab"), 0);
|
||||
EXPECT_EQ(sscanf_no_float("12abc", "%d%d", &value, &value), 1);
|
||||
EXPECT_EQ(sscanf_no_float("12", "%d%d", &value, &value), 1);
|
||||
}
|
||||
|
||||
// --- Conversions the scanner refuses rather than misparses ---
|
||||
|
||||
TEST(SscanfNoFloat, UnsupportedConversions) {
|
||||
float f = 0;
|
||||
double d = 0;
|
||||
wchar_t w[4] = {0};
|
||||
EXPECT_EQ(sscanf_no_float("1.5", "%f", &f), SSCANF_UNSUPPORTED);
|
||||
EXPECT_EQ(sscanf_no_float("1.5", "%lf", &d), SSCANF_UNSUPPORTED);
|
||||
EXPECT_EQ(sscanf_no_float("1e3", "%g", &d), SSCANF_UNSUPPORTED);
|
||||
EXPECT_EQ(sscanf_no_float("0x1p3", "%a", &d), SSCANF_UNSUPPORTED);
|
||||
EXPECT_EQ(sscanf_no_float("abc", "%ls", w), SSCANF_UNSUPPORTED);
|
||||
EXPECT_EQ(sscanf_no_float("abc", "%lc", w), SSCANF_UNSUPPORTED);
|
||||
// Earlier conversions do not hide a later unsupported one
|
||||
int value = 0;
|
||||
EXPECT_EQ(sscanf_no_float("1 2.5", "%d %f", &value, &f), SSCANF_UNSUPPORTED);
|
||||
}
|
||||
|
||||
} // namespace esphome::core::testing
|
||||
@@ -8,64 +8,8 @@ from unittest.mock import MagicMock, patch
|
||||
import pytest
|
||||
|
||||
from esphome.components import esp8266
|
||||
from esphome.components.esp8266 import check_rosetta, lambdas_use_scanf_float
|
||||
from esphome.core import EsphomeError, Lambda
|
||||
from esphome.types import ConfigType
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("src", "expected"),
|
||||
[
|
||||
# Basic float formats
|
||||
('sscanf(buf, "%f", &v)', True),
|
||||
('sscanf(buf, "%F", &v)', True),
|
||||
('sscanf(buf, "%e", &v)', True),
|
||||
('sscanf(buf, "%E", &v)', True),
|
||||
('sscanf(buf, "%g", &v)', True),
|
||||
('sscanf(buf, "%G", &v)', True),
|
||||
('sscanf(buf, "%a", &v)', True),
|
||||
('sscanf(buf, "%A", &v)', True),
|
||||
# With modifiers
|
||||
('sscanf(buf, "%lf", &v)', True),
|
||||
('sscanf(buf, "%Lf", &v)', True),
|
||||
('sscanf(buf, "%8lf", &v)', True),
|
||||
('sscanf(buf, "%*f")', True),
|
||||
('sscanf(buf, "%.2f", &v)', True),
|
||||
# Mixed formats
|
||||
('sscanf(buf, "%d,%f", &a, &b)', True),
|
||||
# fscanf and std::sscanf
|
||||
('fscanf(fp, "%f", &v)', True),
|
||||
('std::sscanf(buf, "%f", &v)', True),
|
||||
# Multi-line
|
||||
('sscanf(buf,\n"%f", &v)', True),
|
||||
# No float format
|
||||
('sscanf(buf, "%d", &v)', False),
|
||||
('sscanf(buf, "%s", s)', False),
|
||||
# printf not scanf
|
||||
('printf("%f", val)', False),
|
||||
# %f in a different statement after scanf
|
||||
('sscanf(buf, "%d", &x); printf("%f", val);', False),
|
||||
# scanf %f in comment only
|
||||
('// sscanf(buf, "%f", &v)\nsscanf(buf, "%d", &x)', False),
|
||||
('/* sscanf(buf, "%f") */\nsscanf(buf, "%d", &x)', False),
|
||||
],
|
||||
)
|
||||
def test_lambdas_use_scanf_float(src: str, expected: bool) -> None:
|
||||
"""Test scanf float detection in lambda source."""
|
||||
config: ConfigType = {"test": [Lambda(src)]}
|
||||
assert lambdas_use_scanf_float(config) is expected
|
||||
|
||||
|
||||
def test_lambdas_use_scanf_float_no_lambdas() -> None:
|
||||
"""Test with config containing no lambdas."""
|
||||
config: ConfigType = {"key": "value", "list": [1, 2]}
|
||||
assert lambdas_use_scanf_float(config) is False
|
||||
|
||||
|
||||
def test_lambdas_use_scanf_float_nested() -> None:
|
||||
"""Test detection in deeply nested config."""
|
||||
config: ConfigType = {"a": {"b": {"c": [Lambda('sscanf(buf, "%f", &v)')]}}}
|
||||
assert lambdas_use_scanf_float(config) is True
|
||||
from esphome.components.esp8266 import check_rosetta
|
||||
from esphome.core import EsphomeError
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
|
||||
@@ -0,0 +1,162 @@
|
||||
"""Unit tests for esphome.code_scan."""
|
||||
|
||||
from pathlib import Path
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from esphome.code_scan import (
|
||||
external_components_use_scanf_float,
|
||||
includes_use_scanf_float,
|
||||
keep_float_scanf,
|
||||
lambdas_use_scanf_float,
|
||||
user_code_uses_scanf_float,
|
||||
)
|
||||
from esphome.const import CONF_ESPHOME, CONF_INCLUDES, CONF_INCLUDES_C
|
||||
from esphome.core import CORE, Lambda
|
||||
from esphome.loader import CORE_COMPONENTS_PATH, FileResource
|
||||
from esphome.types import ConfigType
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("src", "expected"),
|
||||
[
|
||||
# Basic float formats
|
||||
('sscanf(buf, "%f", &v)', True),
|
||||
('sscanf(buf, "%F", &v)', True),
|
||||
('sscanf(buf, "%e", &v)', True),
|
||||
('sscanf(buf, "%E", &v)', True),
|
||||
('sscanf(buf, "%g", &v)', True),
|
||||
('sscanf(buf, "%G", &v)', True),
|
||||
('sscanf(buf, "%a", &v)', True),
|
||||
('sscanf(buf, "%A", &v)', True),
|
||||
# With modifiers
|
||||
('sscanf(buf, "%lf", &v)', True),
|
||||
('sscanf(buf, "%Lf", &v)', True),
|
||||
('sscanf(buf, "%8lf", &v)', True),
|
||||
('sscanf(buf, "%*f")', True),
|
||||
('sscanf(buf, "%.2f", &v)', True),
|
||||
# Mixed formats, including a ';' inside the format string
|
||||
('sscanf(buf, "%d,%f", &a, &b)', True),
|
||||
('sscanf(buf, "%d;%f", &a, &b)', True),
|
||||
('sscanf(buf, "a;b", &a); sscanf(buf, "%d", &b); printf("%f", v)', False),
|
||||
# Escaped quotes, nested calls, a ')' inside the format, an unterminated call
|
||||
('sscanf(buf, "\\"%f\\"", &v)', True),
|
||||
('sscanf(f(x), "%f", &v)', True),
|
||||
('sscanf(s.substr(0, 2).c_str(), "%f", &v)', True),
|
||||
('sscanf(strtok(s, ","), "%f", &v)', True),
|
||||
('sscanf(strtok(s, ","), "%d", &v)', False),
|
||||
('sscanf(buf, "%d)", &a); g("%f")', False),
|
||||
('sscanf(buf, "%d", &a; g("%f")', False),
|
||||
('sscanf(buf, "%f"', True),
|
||||
# fscanf and std::sscanf
|
||||
('fscanf(fp, "%f", &v)', True),
|
||||
('std::sscanf(buf, "%f", &v)', True),
|
||||
# Multi-line
|
||||
('sscanf(buf,\n"%f", &v)', True),
|
||||
# No float format
|
||||
('sscanf(buf, "%d", &v)', False),
|
||||
('sscanf(buf, "%s", s)', False),
|
||||
# printf not scanf
|
||||
('printf("%f", val)', False),
|
||||
# %f in a different statement after scanf
|
||||
('sscanf(buf, "%d", &x); printf("%f", val);', False),
|
||||
# scanf %f in comment only
|
||||
('// sscanf(buf, "%f", &v)\nsscanf(buf, "%d", &x)', False),
|
||||
('/* sscanf(buf, "%f") */\nsscanf(buf, "%d", &x)', False),
|
||||
],
|
||||
)
|
||||
def test_lambdas_use_scanf_float(src: str, expected: bool) -> None:
|
||||
"""Test scanf float detection in lambda source."""
|
||||
config: ConfigType = {"test": [Lambda(src)]}
|
||||
assert lambdas_use_scanf_float(config) is expected
|
||||
|
||||
|
||||
def test_lambdas_use_scanf_float_no_lambdas() -> None:
|
||||
"""Test with config containing no lambdas."""
|
||||
config: ConfigType = {"key": "value", "list": [1, 2]}
|
||||
assert lambdas_use_scanf_float(config) is False
|
||||
|
||||
|
||||
def test_lambdas_use_scanf_float_nested() -> None:
|
||||
"""Test detection in deeply nested config."""
|
||||
config: ConfigType = {"a": {"b": {"c": [Lambda('sscanf(buf, "%f", &v)')]}}}
|
||||
assert lambdas_use_scanf_float(config) is True
|
||||
|
||||
|
||||
def test_includes_use_scanf_float(setup_core: Path) -> None:
|
||||
"""A float scanf in an includes: or includes_c: file is found; comments and non-source files are ignored."""
|
||||
(setup_core / "parse.h").write_text(
|
||||
'static void f(const char *b) { float v; sscanf(b, "%f", &v); }'
|
||||
)
|
||||
(setup_core / "lib").mkdir()
|
||||
(setup_core / "lib" / "int.h").write_text(
|
||||
'static int g(const char *b) { int v; sscanf(b, "%d", &v); return v; }'
|
||||
)
|
||||
(setup_core / "lib" / "notes.txt").write_text('sscanf(b, "%f", &v)')
|
||||
(setup_core / "commented.h").write_text(
|
||||
'// sscanf(b, "%f", &v)\n/* sscanf(b, "%g", &v) */'
|
||||
)
|
||||
|
||||
assert (
|
||||
includes_use_scanf_float({CONF_ESPHOME: {CONF_INCLUDES: ["parse.h"]}}) is True
|
||||
)
|
||||
assert (
|
||||
includes_use_scanf_float({CONF_ESPHOME: {CONF_INCLUDES_C: ["parse.h"]}}) is True
|
||||
)
|
||||
assert includes_use_scanf_float({CONF_ESPHOME: {CONF_INCLUDES: ["lib"]}}) is False
|
||||
assert (
|
||||
includes_use_scanf_float(
|
||||
{CONF_ESPHOME: {CONF_INCLUDES: ["commented.h", "<cstdio>"]}}
|
||||
)
|
||||
is False
|
||||
)
|
||||
assert includes_use_scanf_float({}) is False
|
||||
|
||||
|
||||
def test_keep_float_scanf() -> None:
|
||||
"""Explicit values win; unset follows whether user code scans a float."""
|
||||
float_config: ConfigType = {
|
||||
CONF_ESPHOME: {"on_boot": [Lambda('sscanf(b, "%f", &v)')]}
|
||||
}
|
||||
int_config: ConfigType = {
|
||||
CONF_ESPHOME: {"on_boot": [Lambda('sscanf(b, "%d", &v)')]}
|
||||
}
|
||||
assert keep_float_scanf(True, int_config, "x", "y") is True
|
||||
assert keep_float_scanf(False, float_config, "x", "y") is False
|
||||
assert keep_float_scanf(False, int_config, "x", "y") is False
|
||||
assert keep_float_scanf(None, float_config, "x", "y") is True
|
||||
assert keep_float_scanf(None, int_config, "x", "y") is False
|
||||
|
||||
|
||||
def test_external_components_use_scanf_float(setup_core: Path) -> None:
|
||||
"""Sources of components outside the esphome package are scanned; in-tree ones are skipped."""
|
||||
|
||||
def manifest(package_dir: Path, *sources: str) -> MagicMock:
|
||||
m = MagicMock()
|
||||
m.is_platform_component = False
|
||||
m.module.__file__ = str(package_dir / "__init__.py")
|
||||
m.resources = [FileResource("pkg", name) for name in sources]
|
||||
return m
|
||||
|
||||
(setup_core / "ext_float").mkdir()
|
||||
(setup_core / "ext_float" / "parse.cpp").write_text(
|
||||
'void f(const char *b) { float v; sscanf(b, "%f", &v); }'
|
||||
)
|
||||
(setup_core / "ext_int").mkdir()
|
||||
(setup_core / "ext_int" / "parse.cpp").write_text(
|
||||
'int g(const char *b) { int v; sscanf(b, "%d", &v); return v; }'
|
||||
)
|
||||
lookup = {
|
||||
"ext_float": manifest(setup_core / "ext_float", "parse.cpp"),
|
||||
"ext_int": manifest(setup_core / "ext_int", "parse.cpp"),
|
||||
"sensor": manifest(CORE_COMPONENTS_PATH / "sensor", "sensor.cpp"),
|
||||
"esphome": manifest(CORE_COMPONENTS_PATH.parent / "core", "config.cpp"),
|
||||
}
|
||||
|
||||
with patch("esphome.config.get_component", side_effect=lookup.get):
|
||||
CORE.config = {"esphome": {}, "sensor": {}, "ext_int": {}}
|
||||
assert external_components_use_scanf_float(CORE.config) is False
|
||||
CORE.config = {"sensor": {}, "ext_float": {}}
|
||||
assert external_components_use_scanf_float(CORE.config) is True
|
||||
assert user_code_uses_scanf_float(CORE.config) is True
|
||||
@@ -1,6 +1,7 @@
|
||||
"""Unit tests for esphome.config_helpers module."""
|
||||
|
||||
from collections.abc import Callable
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
@@ -10,6 +11,7 @@ from esphome.config_helpers import (
|
||||
filter_source_files_from_platform,
|
||||
frameworks_for_platforms,
|
||||
get_logger_level,
|
||||
iter_include_files,
|
||||
)
|
||||
from esphome.const import (
|
||||
CONF_LEVEL,
|
||||
@@ -172,3 +174,15 @@ def test_filter_source_files_from_defines() -> None:
|
||||
|
||||
mock_core.defines = set()
|
||||
assert sorted(filter_func()) == ["automation.cpp", "filter.cpp"]
|
||||
|
||||
|
||||
def test_iter_include_files(setup_core: Path) -> None:
|
||||
"""Files yield their name, directory members keep the directory prefix, system headers are skipped."""
|
||||
(setup_core / "one.h").write_text("")
|
||||
(setup_core / "lib").mkdir()
|
||||
(setup_core / "lib" / "two.h").write_text("")
|
||||
files = {
|
||||
basename.as_posix()
|
||||
for _, basename in iter_include_files(["one.h", "lib", "<cstdio>"])
|
||||
}
|
||||
assert files == {"one.h", "lib/two.h"}
|
||||
|
||||
Reference in New Issue
Block a user