Skip to content

Replace trivial calls to snprintf with safe_strcpy#376

Open
dpw13 wants to merge 1 commit intoopensensor:mainfrom
dpw13:fix/snprintf-to-strcpy
Open

Replace trivial calls to snprintf with safe_strcpy#376
dpw13 wants to merge 1 commit intoopensensor:mainfrom
dpw13:fix/snprintf-to-strcpy

Conversation

@dpw13
Copy link
Copy Markdown
Contributor

@dpw13 dpw13 commented Apr 11, 2026

This PR replaces two different cases of snprintf() with the simpler safe_strcpy():

  1. snprintf(dest, size, "%s", string): These are safe but unnecessarily complex when a string is being copied.
  2. snprintf(dest, size, string): These are safe if the string is constant and doesn't contain any format specifiers, but is still unnecessarily complex.

There are other instances in the codebase where snprintf is still used:

  • When nearby regions of code actually use format specifiers. I've kept the snprintf to match the nearby code for clarity.
  • When the return value of snprintf is used, e.g. to increment a pointer. We could modify safe_strcpy() to return the length of the string copied, or perhaps create a modified safe_strcat() that carries along an offset. This PR is only intended to replace trivial calls though, so I'm deferring changing these to a future update if/when we decide such a change is worthwhile.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR simplifies several string-copy sites by replacing trivial snprintf() usages with safe_strcpy(), reducing verbosity and eliminating any residual risk of accidental format-string interpretation in copy-only cases.

Changes:

  • Replaced snprintf(dst, size, "%s", src)-style copies with safe_strcpy(dst, src, size, 0) across web, video, database, and core modules.
  • Updated includes where needed to use the string utility header (utils/strings.h).

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/web/thumbnail_thread.c Uses safe_strcpy for input/output path copies and adds required include.
src/web/api_handlers_system.c Uses safe_strcpy for copy-only version/detail fields.
src/web/api_handlers_streams_test.c Uses safe_strcpy for a constant error message.
src/web/api_handlers_recordings_batch_download.c Uses safe_strcpy for ZIP entry name copy.
src/video/onvif_discovery.c Uses safe_strcpy to copy inet_ntoa() results into a local buffer.
src/video/onvif_discovery_thread.c Reorders project includes and uses safe_strcpy for IP address string copies.
src/video/hls/hls_unified_thread.c Uses safe_strcpy for local stream-name copy used in logging/cleanup.
src/video/go2rtc/go2rtc_api.c Uses safe_strcpy for JSON string extraction into caller-provided buffers.
src/database/db_recordings.c Uses safe_strcpy for constant SQL fragments prior to concatenation.
src/core/config.c Uses safe_strcpy for default config string initialization.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants