ewilken / hap-rs

Rust implementation of the Apple HomeKit Accessory Protocol (HAP)
Apache License 2.0
197 stars 35 forks source link

Bug in FileStorage #35

Closed Gaelan closed 3 years ago

Gaelan commented 3 years ago

FileStorage has a bug: when overwriting an existing file with a shorter string, it fails to truncate the end, resulting in config.json looking something like this:

{"socket_addr":"10.0.1.25:32000","pin":{"pin":[3,1,4,1,5,9,2,6]},"name":"Patio Heater","device_id":"30-18-31-41-00-00","device_ed25519_keypair":[245,41,240,83,254,253,1,8,2,237,61,117,97,67,53,117,130,70,148,135,56,158,9,132,174,48,147,103,59,160,244,236,179,231,26,227,82,107,115,144,30,112,96,96,109,12,248,103,4,78,110,151,220,84,35,18,171,88,25,37,63,42,131,254],"configuration_number":2,"state_number":1,"category":"Heater","protocol_version":"1.0","status_flag":"NotPaired","feature_flag":"Zero","max_peers":null}ers":null}

(note the end)

The issue is here:

https://github.com/ewilken/hap-rs/blob/596e70b4613816a50172c115c9ca3c5451f027e9/src/storage/file_storage.rs#L68

You need to add .truncate(true).