1
0
Fork 0
mirror of https://github.com/vbatts/go-mtree.git synced 2025-10-03 20:21:01 +00:00

compare: improve tar_time truncation

Rather than parsing the value as a float and then truncating it, just
parse it as an integer in the first place (this also adds some
validation that we are parsing a reasonable-looking value).

While we're at it, add some integration tests for this code to make sure
this quite complicated special-case behaviour doesn't regress.

Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
This commit is contained in:
Aleksa Sarai 2025-09-09 17:55:02 +10:00
parent 0beb885cbf
commit 604ab42863
No known key found for this signature in database
GPG key ID: 2897FAD2B7E9446F
2 changed files with 116 additions and 9 deletions

View file

@ -4,7 +4,6 @@ import (
"encoding/json"
"fmt"
"slices"
"strconv"
)
// XXX: Do we need a Difference interface to make it so people can do var x
@ -253,23 +252,35 @@ func compareEntry(oldEntry, newEntry Entry) ([]KeyDelta, error) {
// Make a new tar_time.
if diffs["tar_time"].Old == nil {
time, err := strconv.ParseFloat(timeStateT.Old.Value(), 64)
if err != nil {
return nil, fmt.Errorf("failed to parse old time: %s", err)
var (
timeSec, timeNsec int64
// used to check for trailing characters
trailing rune
)
val := timeStateT.Old.Value()
n, _ := fmt.Sscanf(val, "%d.%d%c", &timeSec, &timeNsec, &trailing)
if n != 2 {
return nil, fmt.Errorf("failed to parse old time: invalid format %q", val)
}
newTime := new(KeyVal)
*newTime = KeyVal(fmt.Sprintf("tar_time=%d.000000000", int64(time)))
*newTime = KeyVal(fmt.Sprintf("tar_time=%d.%9.9d", timeSec, 0))
diffs["tar_time"].Old = newTime
} else if diffs["tar_time"].New == nil {
time, err := strconv.ParseFloat(timeStateT.New.Value(), 64)
if err != nil {
return nil, fmt.Errorf("failed to parse new time: %s", err)
var (
timeSec, timeNsec int64
// used to check for trailing characters
trailing rune
)
val := timeStateT.New.Value()
n, _ := fmt.Sscanf(val, "%d.%d%c", &timeSec, &timeNsec, &trailing)
if n != 2 {
return nil, fmt.Errorf("failed to parse new time: invalid format %q", val)
}
newTime := new(KeyVal)
*newTime = KeyVal(fmt.Sprintf("tar_time=%d.000000000", int64(time)))
*newTime = KeyVal(fmt.Sprintf("tar_time=%d.%9.9d", timeSec, 0))
diffs["tar_time"].New = newTime
} else {