Bug 236093 - ports-mgmt/pkg: possible error in function format_rate_SI (src/event.c)
Summary: ports-mgmt/pkg: possible error in function format_rate_SI (src/event.c)
Status: Closed FIXED
Alias: None
Product: Ports & Packages
Classification: Unclassified
Component: Individual Port(s) (show other bugs)
Version: Latest
Hardware: Any Any
: --- Affects Only Me
Assignee: freebsd-pkg (Nobody)
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-02-27 22:25 UTC by Samy Mahmoudi
Modified: 2021-08-18 08:56 UTC (History)
0 users

See Also:
bugzilla: maintainer-feedback? (pkg)


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Samy Mahmoudi 2019-02-27 22:25:34 UTC
src/event.c:

87 /* units for format_size */                                                     
88 static const char *unit_SI[] = { " ", "k", "M", "G", "T", };

109 static void                                                                     
110 format_rate_SI(char *buf, int size, off_t bytes)                                
111 {                                                                               
112         int i;                                                                  
113                                                                                 
114         bytes *= 100;                                                           
115         for (i = 0; bytes >= 100*1000 && unit_SI[i][0] != 'T'; i++)             
116                 bytes = (bytes + 500) / 1000;                                   
117         if (i == 0) {                                                           
118                 i++;                                                            
119                 bytes = (bytes + 500) / 1000;                                   
120         }                                                                       
121         snprintf(buf, size, "%3lld.%1lld%s%s",                                  
122             (long long) (bytes + 5) / 100,                                      
123             (long long) (bytes + 5) / 10 % 10,                                  
124             unit_SI[i],                                                         
125             i ? "B" : " ");                                                     
126 }

Unless I am mistaken, i can not be zero at lines 124-125. As a consequence:

• the first element of unit_SI is never used
• in the call to snprintf, the last argument is always "B", never " "
Comment 1 Baptiste Daroussin freebsd_committer freebsd_triage 2021-08-18 08:56:37 UTC
Nice catch, fixed, thank you!