Replace trivial calls to snprintf with safe_strcpy#376
Open
dpw13 wants to merge 1 commit intoopensensor:mainfrom
Open
Replace trivial calls to snprintf with safe_strcpy#376dpw13 wants to merge 1 commit intoopensensor:mainfrom
dpw13 wants to merge 1 commit intoopensensor:mainfrom
Conversation
Contributor
There was a problem hiding this comment.
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 withsafe_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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR replaces two different cases of
snprintf()with the simplersafe_strcpy():snprintf(dest, size, "%s", string): These are safe but unnecessarily complex when a string is being copied.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
snprintfis still used:snprintfto match the nearby code for clarity.snprintfis used, e.g. to increment a pointer. We could modifysafe_strcpy()to return the length of the string copied, or perhaps create a modifiedsafe_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.