fix: increase buffer size for elapsed time on timeline#479
Open
aadamowski wants to merge 1 commit into
Open
Conversation
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.
Description
This pull request fixes a bug in the
nvtoptimeline where the elapsed time would erroneously display aserrwhen the terminal width was large and the update delay was significant.Problem
When running
nvtopin a wide terminal with a large update delay, the timeline would sometimes showerrinstead of the actual elapsed time (e.g.,1234s).This occurred because the buffer used to format the elapsed time string was too small. When the number of seconds exceeded the capacity of the buffer,
snprintfwould return a value indicating truncation, which the code interpreted as an error, triggering the fallback to the"err"string.Root Cause
In
src/interface.c, the buffer for the elapsed time string was defined with a hardcoded size of 5:As the application ran longer, the number of seconds would eventually require more than 4 characters (plus the null terminator). For example, "100s" is 4 characters, and "1000s" is 5 characters. When$\ge 5$ , which matched the following error check:
snprintfattempted to write a string longer than the buffer, it returned a valueFix
The fix involves three key changes in
src/interface.c:elapsedSecondsbuffer from5to16characters to safely accommodate much longer time durations.snprintfreturn value check to usesizeof(elapsedSeconds)instead of the hardcoded4. This ensures that the error handling logic scales correctly with the buffer size.(int)for thesizeofcomparison to avoid signed/unsigned comparison warnings.Verification Results
The fix was verified using the following steps:
Reproduction: A test was conducted by setting a large terminal width and a significant update delay, then checking the output for the error string using the following command:
The command successfully detected the
"err"string in the output, reproducing the bug.Build: The
nvtopbinary was rebuilt from source.Validation: The same test command was re-run. The command no longer detected
"err"and instead confirmed that the output contains valid elapsed time strings (e.g.,1570s). The test was repeated with increasingly large values of delay - including the max value of 99.9 seconds that is settable from the UI (UpdateInterval = 99900in~/.config/nvtop/interface.ini) and even larger values set directly in the config file (up to 86400 seconds, which corresponds to a full day long update interval).