﻿# ESP32 NVS string reads: required length includes the terminator

A local configuration reader must distinguish NVS string capacity, actual successful retrieval and an unchanged output after failure. A length query alone is insufficient.

Author: Retrivium team
Updated: 2026-11-06
Canonical URL: https://retrivium.ai/journal/edge-esp32-nvs-string-length-read-result/
Language: en

## A failed read can leave a plausible previous setting

An offline classifier loads the name of its active calibration profile from NVS into a reused buffer. A later read fails, but the application continues using the buffer and labels it the current stored setting. The text can look perfectly valid because it belongs to an earlier read. A displayable string is therefore weaker evidence than a successful retrieval. Preserve the operation status with the value and decide explicitly whether an older setting is an allowed fallback.

## The required string capacity includes its terminator

The [NVS documentation](https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/storage/nvs_flash.html) supports querying length with a null output pointer. For nvs_get_str, the required length includes the terminating zero. On the retrieval call, the length argument supplies available output capacity and returns actual length. Do not subtract the terminator before allocation or replace capacity with a character count. Treat the query result as an allocation requirement for this API, not as the length convention of every serialization format.

## The public contract keeps errors separate from output

The [public NVS header](https://raw.githubusercontent.com/espressif/esp-idf/master/components/nvs_flash/include/nvs.h) says retrieval errors do not modify the output and lists insufficient length as an error. Handle missing key, incompatible stored type and invalid capacity according to the application’s policy. None should silently become a fresh successful read merely because the old buffer remains readable. Keep the handle’s namespace and partition in the diagnostic context so a correct lookup in the wrong storage scope is also investigable.

## A length query does not finish the read procedure

Check the query result, bounded allocation and final retrieval result independently. If settings can change while this procedure runs, define application coordination or a bounded retry policy; do not treat separate calls as a promised application snapshot. When retrying, provide the actual capacity of the buffer being used. A previous successful length value is not authority to write beyond that capacity. Choose a documented string operation for text and a blob operation for arbitrary binary structures instead of forcing both through C-string assumptions.

## Make the fixture expose stale output

Propose a disposable namespace containing ordinary text and an empty string. Exercise exact documented capacity, an insufficient buffer, a missing key and a type mismatch while the output initially contains a recognizable old value. The application must distinguish each failure from accepted current data and retain the empty-string success case. No NVS partition was read or written here. Acceptance would establish correct retrieval handling, not power-loss durability, multi-key atomicity or suitability of the selected calibration profile.

## Keep storage text distinct from binary transport

[Triton BYTES framing](/journal/guide-triton-bytes-tensor/) uses a different length convention and binary representation. [Absent, null and empty values](/journal/provider-json-absent-null-empty/) explain another semantic distinction at an API boundary. In this reader, report lookup status, required capacity, actual successful value and any declared fallback. That record prevents both an off-by-terminator allocation and silent reuse of stale configuration. It also keeps successful string retrieval separate from the model’s decision to accept the profile named by that string.
