Skip to content

Fix use of format string functions with constant string #2107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 36 commits into from
May 23, 2025

Conversation

NathanChase22
Copy link
Contributor

In accordance with recent updates to the Go lang vet checker which flags whenever printf statements and their variations are formatted like this fmt.printf(var) where var is a non-constant string variable.

Source code is changed such that in these cases the statement is changed to: fmt.printf("%s",var)

I believe this helps communicate that the variable in the statement is a no-arg string variable.

Result: calling make unit-test with Go 1.24.1 no longer fails to build. However for the sake of maintaining compatibility the go.mod file still has the original version requirement

@NathanChase22 NathanChase22 requested a review from stgraber as a code owner May 11, 2025 04:13
@stgraber
Copy link
Member

Hmm, I see a lot of those that could be handled in a more efficient way.

For the cases that look like fmt.Sprintf("%s\r\n", i18n.G("something")), doing fmt.Print(i18n.G("something")+"\r\n") will be more efficient.

Same goes for the error handling case, instead of doing fmt.Errorf("%s", "Fixed error") or similar, you can do errors.New("Fixed error").

The goal of the new vet warning is to limit calls to otherwise expensive Printf style functions when not strictly needed. Using a work around to keep using the slow path is counterproductive. If we're going to be updating all those function calls, we may as well switch to the more efficient format.

@NathanChase22
Copy link
Contributor Author

NathanChase22 commented May 11, 2025

For the cases that look like fmt.Sprintf("%s\r\n", i18n.G("something")), doing fmt.Print(i18n.G("something")+"\r\n") will be more efficient.

This is because Sprintf() has formatting overhead that is unnecessary for the given use case?

Same goes for the error handling case, instead of doing fmt.Errorf("%s", "Fixed error") or similar, you can do errors.New("Fixed error").

Likewise using the errors package is preferable because we are doing a static string, meaning the formatting overhead of Errorf is unneeded?

The goal of the new vet warning is to limit calls to otherwise expensive Printf style functions when not strictly needed. Using a work around to keep using the slow path is counterproductive. If we're going to be updating all those function calls, we may as well switch to the more efficient format.

With this is in mind I can make those changes

EDIT: then cases such as api.StatusErrorf(http.StatusLocked, err.Error()) where it's a wrapper for Sprintf() I believe making it api.StatusErrorf(http.StatusLocked, "%s", err.Error()) is okay right? Since the error message is non-static but the compiler wants the formatting to be constant.

@stgraber
Copy link
Member

This is because Sprintf() has formatting overhead that is unnecessary for the given use case?

Correct. When we're passing it a fixed string without any formatted arguments, it causes Go to look through the string for any % type tokens that it needs to replace, effectively wasting CPU time.

Likewise using the errors package is preferable because we are doing a static string, meaning the formatting overhead of Errorf is unneeded?

Yep, same thing, fmt.Errorf needs to parse character by character to handle format strings. That's a waste of CPU time when there is none, so for any fixed strings, errors.New is to be preferred.

@stgraber
Copy link
Member

api.StatusErrorf(http.StatusLocked, err.Error()) is triggering the go vet check too?

I would have expect it to be limited to Go's own functions, so fmt.Printf, fmt.Errorf, ..., not our own functions.

If go vet goes deep enough to see that api.StatusErrorf is a wrapper around fmt.Errorf, then we'd probably want to introduce a api.StatusError which doesn't take a format string and instead only takes fixed errors.

@github-actions github-actions bot added the API Changes to the REST API label May 16, 2025
@NathanChase22
Copy link
Contributor Author

NathanChase22 commented May 16, 2025

api.StatusErrorf(http.StatusLocked, err.Error()) is triggering the go vet check too?

I would have expect it to be limited to Go's own functions, so fmt.Printf, fmt.Errorf, ..., not our own functions.

If go vet goes deep enough to see that api.StatusErrorf is a wrapper around fmt.Errorf, then we'd probably want to introduce a api.StatusError which doesn't take a format string and instead only takes fixed errors.

Looking at the actual implementation of api.StatusErrorf it checks whether there aren't any arguments and circumvents the call to fmt.Sprintf() , meaning in runtime the static strings aren't going through unnecessary formatting steps. Though this isn't caught by go vet

EDIT: also if we were to create a new function like StatusError() that will conflict with the struct that's called the same thing. I wasn't sure whether we should be renaming struct names in order to make this minor change

@stgraber stgraber changed the title Fix: altered printf calls with non-const values and no args to be formatted with %s Fix use of format string functions with constant string May 23, 2025
@stgraber
Copy link
Member

Thanks for this. It took me a little while to review all of it but I've now done so and also took the opportunity to slice it into smaller commits which will help with backporting to the LTS branch.

@stgraber stgraber merged commit 5c2c4b0 into lxc:main May 23, 2025
37 of 38 checks passed
@NathanChase22 NathanChase22 deleted the fix-nonconstant branch June 10, 2025 02:00
tmeijn pushed a commit to tmeijn/dotfiles that referenced this pull request Jun 12, 2025
This MR contains the following updates:

| Package | Update | Change |
|---|---|---|
| [lxc/incus](https://github.com/lxc/incus) | minor | `v6.12.0` -> `v6.13.0` |

MR created with the help of [el-capitano/tools/renovate-bot](https://gitlab.com/el-capitano/tools/renovate-bot).

**Proposed changes to behavior should be submitted there as MRs.**

---

### Release Notes

<details>
<summary>lxc/incus (lxc/incus)</summary>

### [`v6.13.0`](https://github.com/lxc/incus/releases/tag/v6.13.0): Incus 6.13

[Compare Source](lxc/incus@v6.12.0...v6.13.0)

### Announcement

https://discuss.linuxcontainers.org/t/incus-6-13-has-been-released/23899

#### What's Changed

-   Add server side filtering for `incus network list` by [@&#8203;Abdomash](https://github.com/Abdomash) in lxc/incus#1989
-   doc: Fix default value of ipv4.dhcp.gateway to IPv4 address by [@&#8203;hnakamur](https://github.com/hnakamur) in lxc/incus#1991
-   doc: Fix default value of ipv6.routes network_bridge by [@&#8203;hnakamur](https://github.com/hnakamur) in lxc/incus#1992
-   doc: Fix Debian 12 nickname by [@&#8203;hnakamur](https://github.com/hnakamur) in lxc/incus#1993
-   Fix issues with nftables and address sets by [@&#8203;stgraber](https://github.com/stgraber) in lxc/incus#1995
-   incusd/dns: fix typo in error log by [@&#8203;gwenya](https://github.com/gwenya) in lxc/incus#1998
-   Port pci to gendoc by [@&#8203;DinglyCoder](https://github.com/DinglyCoder) in lxc/incus#1996
-   rename reverters from revert to reverter to not conflict with the package by [@&#8203;The127](https://github.com/The127) in lxc/incus#2000
-   incusd: Refactor error checks with errors.is and errors.As by [@&#8203;The127](https://github.com/The127) in lxc/incus#2002
-   Rename vars using builtin names by [@&#8203;The127](https://github.com/The127) in lxc/incus#2006
-   incusd: Refactor getting heartbeat mode name into a function by [@&#8203;The127](https://github.com/The127) in lxc/incus#2005
-   Unify receiver types  by [@&#8203;The127](https://github.com/The127) in lxc/incus#2004
-   incusd: Refactor unused parameters by [@&#8203;The127](https://github.com/The127) in lxc/incus#2001
-   incusd/firewall/nftables: Fix handling of address set deletion by [@&#8203;stgraber](https://github.com/stgraber) in lxc/incus#2012
-   Add server-side filtering for incus storage bucket list  by [@&#8203;allisoncchen](https://github.com/allisoncchen) in lxc/incus#2008
-   Add support for running the Incus agent on Windows by [@&#8203;stgraber](https://github.com/stgraber) in lxc/incus#2014
-   incusd/network/ovn: Wait up to 10s for OVN northd to allocate an IP by [@&#8203;stgraber](https://github.com/stgraber) in lxc/incus#2024
-   Refactor unnecessary if checks by [@&#8203;The127](https://github.com/The127) in lxc/incus#2017
-   incusd: refactor process kill error being ignored by [@&#8203;The127](https://github.com/The127) in lxc/incus#2018
-   incusd/instance: fix device finding logic by [@&#8203;The127](https://github.com/The127) in lxc/incus#2019
-   incusd: remove unreachable code in api internal by [@&#8203;The127](https://github.com/The127) in lxc/incus#2016
-   Rewrite legacy QEMU config code by [@&#8203;bensmrs](https://github.com/bensmrs) in lxc/incus#2011
-   incus: refactor admin_init.go config initialization by [@&#8203;The127](https://github.com/The127) in lxc/incus#2023
-   Extend incus-migrate to support uploading filesystems and disks as custom volumes by [@&#8203;presztak](https://github.com/presztak) in lxc/incus#2022
-   Add server-side filtering for `incus project list` by [@&#8203;rahafjrw](https://github.com/rahafjrw) in lxc/incus#2015
-   incusd/network/bridge: Add missing line breaks by [@&#8203;stgraber](https://github.com/stgraber) in lxc/incus#2029
-   Port ovn network documentation to gendoc  by [@&#8203;lahariguduru](https://github.com/lahariguduru) in lxc/incus#2027
-   Remove gopkg.in/tomb.v2 dependency by [@&#8203;nanjj](https://github.com/nanjj) in lxc/incus#2031
-   Update gendoc for network_physical and network_bridge by [@&#8203;janetkimmm](https://github.com/janetkimmm) in lxc/incus#2030
-   incusd/instance/qemu: Don't allow hotplug when at maxmem by [@&#8203;stgraber](https://github.com/stgraber) in lxc/incus#2033
-   incusd/server/device/nic_routed: Added host_tables by [@&#8203;AbhinavTiruvee](https://github.com/AbhinavTiruvee) in lxc/incus#2009
-   incusd/storage: Fix migration error due to rounding by [@&#8203;stgraber](https://github.com/stgraber) in lxc/incus#2041
-   incusd: add missing err handling for transactions by [@&#8203;The127](https://github.com/The127) in lxc/incus#2040
-   Rename result of `mux.NewRouter()` so they dont collide with the mux package by [@&#8203;The127](https://github.com/The127) in lxc/incus#2037
-   Rename instances of sha256.New() so they dont collide with the package name by [@&#8203;The127](https://github.com/The127) in lxc/incus#2038
-   incusd/storage/zfs: Optimize snapshot deletion by [@&#8203;stgraber](https://github.com/stgraber) in lxc/incus#2043
-   Missing case statements for iota constants by [@&#8203;The127](https://github.com/The127) in lxc/incus#2039
-   incusd: Refactor error list by [@&#8203;The127](https://github.com/The127) in lxc/incus#2026
-   Remove redundant map size by [@&#8203;The127](https://github.com/The127) in lxc/incus#2053
-   internal/dnsutil: remove unused package dnsutil by [@&#8203;The127](https://github.com/The127) in lxc/incus#2046
-   Rework QEMU config override logic by [@&#8203;bensmrs](https://github.com/bensmrs) in lxc/incus#2048
-   tools: Add govulncheck by [@&#8203;breml](https://github.com/breml) in lxc/incus#2050
-   incusd/dnsmasq: refactor DHCPValidIP condition checks by [@&#8203;The127](https://github.com/The127) in lxc/incus#2020
-   incusd: rename instanceActionToOptype to instanceActionToOpType by [@&#8203;The127](https://github.com/The127) in lxc/incus#2047
-   internal/iprange: add tests for the iprange.Range struct by [@&#8203;The127](https://github.com/The127) in lxc/incus#2045
-   Port macvlan network documentation to gendoc by [@&#8203;kmxtn](https://github.com/kmxtn) in lxc/incus#2042
-   incusd/certificates: Properly handle PEM encoding on POST by [@&#8203;stgraber](https://github.com/stgraber) in lxc/incus#2056
-   lint: Codespell exclude generated docs by [@&#8203;breml](https://github.com/breml) in lxc/incus#2051
-   incusd/instance/qemu: Don't allow QEMU RSS to exceed memory limit by [@&#8203;stgraber](https://github.com/stgraber) in lxc/incus#2058
-   Add server-side filtering for `incus config trust list` by [@&#8203;allisoncchen](https://github.com/allisoncchen) in lxc/incus#2057
-   Add support for additional disks to `incus-migrate` by [@&#8203;presztak](https://github.com/presztak) in lxc/incus#2064
-   Restart builtin DNS server on failure by [@&#8203;gwenya](https://github.com/gwenya) in lxc/incus#2062
-   Port sriov network documentation to gendoc by [@&#8203;cory-chang](https://github.com/cory-chang) in lxc/incus#2059
-   Db generate fixes by [@&#8203;masnax](https://github.com/masnax) in lxc/incus#2066
-   Add support for split image publishing by [@&#8203;saahirN](https://github.com/saahirN) in lxc/incus#2013
-   Added CLI Configuration Option for Default Table Layout by [@&#8203;arojas2003](https://github.com/arojas2003) in lxc/incus#2044
-   incusd/instance/qemu: Limit memory hotplug slots to 8 by [@&#8203;stgraber](https://github.com/stgraber) in lxc/incus#2065
-   incusd/device/sriov: Handle cards without configurable spoof checking by [@&#8203;stgraber](https://github.com/stgraber) in lxc/incus#2069
-   incusd/firewall/nftables: disable UDP checksum validation for packets on bridged network by [@&#8203;MaheshPunjabi](https://github.com/MaheshPunjabi) in lxc/incus#2076
-   Use snake case entity names for ID column names by [@&#8203;masnax](https://github.com/masnax) in lxc/incus#2079
-   Allow providing certificates as part of preseed data [#&#8203;1804](lxc/incus#1804) by [@&#8203;DinglyCoder](https://github.com/DinglyCoder) in lxc/incus#2078
-   Porting `network_acls` to database generator by [@&#8203;Aryan470](https://github.com/Aryan470) in lxc/incus#2035
-   client: Fix required extension for GetNetworkAddressSetsAllProjects by [@&#8203;breml](https://github.com/breml) in lxc/incus#2082
-   Move cluster resource caching to point of consumption  by [@&#8203;janetkimmm](https://github.com/janetkimmm) in lxc/incus#2072
-   Implement stateful DHCPv6 by [@&#8203;rahafjrw](https://github.com/rahafjrw) in lxc/incus#2060
-   Adding support for incus storage volume mount by [@&#8203;Aryan470](https://github.com/Aryan470) in lxc/incus#2071
-   incus/remote: Add "get-client-certificate" and "get-client-token" by [@&#8203;stoven2k17](https://github.com/stoven2k17) in lxc/incus#2088
-   Ban the use of LLMs and AI assistants/agents by [@&#8203;stgraber](https://github.com/stgraber) in lxc/incus#2089
-   Add support for .OVA import by [@&#8203;presztak](https://github.com/presztak) in lxc/incus#2092
-   Add common aliases for add/create remove/delete/rm in the CLI by [@&#8203;joecwilson](https://github.com/joecwilson) in lxc/incus#1955
-   Improve handling of Windows agent by [@&#8203;stgraber](https://github.com/stgraber) in lxc/incus#2103
-   Include OS metrics on Incus OS by [@&#8203;stgraber](https://github.com/stgraber) in lxc/incus#2104
-   incusd/instance/lxc: Refactor inheritInitPidFd by [@&#8203;gwenya](https://github.com/gwenya) in lxc/incus#2106
-   Removed useless else in Makefile by [@&#8203;nanjj](https://github.com/nanjj) in lxc/incus#2115
-   incusd/main_forknet: Don't touch resolv.conf when no leases by [@&#8203;stgraber](https://github.com/stgraber) in lxc/incus#2118
-   incusd/storage/ceph: Fix parent tracking for VMs by [@&#8203;stgraber](https://github.com/stgraber) in lxc/incus#2119
-   Fix Github test failures by [@&#8203;stgraber](https://github.com/stgraber) in lxc/incus#2120
-   incus: Simplify code by using modern constructs by [@&#8203;nanjj](https://github.com/nanjj) in lxc/incus#2111
-   internal: Simplify code by using modern constructs by [@&#8203;nanjj](https://github.com/nanjj) in lxc/incus#2113
-   Simplify code by using modern constructs by [@&#8203;nanjj](https://github.com/nanjj) in lxc/incus#2112
-   Simplify code by using modern constructs by [@&#8203;nanjj](https://github.com/nanjj) in lxc/incus#2110
-   Translations update from Hosted Weblate by [@&#8203;weblate](https://github.com/weblate) in lxc/incus#2123
-   incusd/instances: Tweak storage migration errors by [@&#8203;stgraber](https://github.com/stgraber) in lxc/incus#2131
-   Add +invtsc to cpu extensions ensure tsc as current clocksource by [@&#8203;nanjj](https://github.com/nanjj) in lxc/incus#2127
-   incusd/instance: Fix incorrect cluster.Connect call by [@&#8203;stgraber](https://github.com/stgraber) in lxc/incus#2134
-   incusd/instances_post: Prevent pointless device overrides by [@&#8203;stgraber](https://github.com/stgraber) in lxc/incus#2133
-   Use the umoci Go package instead of the command by [@&#8203;presztak](https://github.com/presztak) in lxc/incus#1880
-   incusd/forkproxy: join the correct mntns for listen by [@&#8203;cyphar](https://github.com/cyphar) in lxc/incus#2136
-   Fix use of format string functions with constant string by [@&#8203;NathanChase22](https://github.com/NathanChase22) in lxc/incus#2107
-   incus-migrate: prompt for cluster target by [@&#8203;MaheshPunjabi](https://github.com/MaheshPunjabi) in lxc/incus#2124
-   Use our own QMP monitor by [@&#8203;nanjj](https://github.com/nanjj) in lxc/incus#2105
-   incusd/instance/qmp: remove weird qmp bug workaround by [@&#8203;nanjj](https://github.com/nanjj) in lxc/incus#2139
-   Make sure limits.memory <= root,size.state by [@&#8203;nanjj](https://github.com/nanjj) in lxc/incus#2138
-   incusd/instance/qmp: Refactor qmpWriteMsg by [@&#8203;nanjj](https://github.com/nanjj) in lxc/incus#2140
-   Allow overriding the external address (NAT) for OVN NICs by [@&#8203;OGCbn](https://github.com/OGCbn) in lxc/incus#2090
-   Static Binding of OVN uplink gateway MAC by [@&#8203;davidbockelman](https://github.com/davidbockelman) in lxc/incus#2091
-   incusd: fix some trivial typos by [@&#8203;mrstux](https://github.com/mrstux) in lxc/incus#2147
-   incusd/storage: Fix squashfs unpacking to NFS destinations by [@&#8203;stgraber](https://github.com/stgraber) in lxc/incus#2149
-   Remove pending cluster member on join failure by [@&#8203;presztak](https://github.com/presztak) in lxc/incus#2145
-   incusd/instance/qmp: associated request/reply with command id by [@&#8203;nanjj](https://github.com/nanjj) in lxc/incus#2142
-   RTC base enhancement via rtc change event handling by [@&#8203;nanjj](https://github.com/nanjj) in lxc/incus#2141
-   incusd/storage/linstor: Prevent mounting unreachable pools by [@&#8203;bensmrs](https://github.com/bensmrs) in lxc/incus#2101
-   Return empty slice instead of nil when no storage pool is present by [@&#8203;presztak](https://github.com/presztak) in lxc/incus#2152
-   Clear the "volatile.cpu.nodes" if needed by [@&#8203;presztak](https://github.com/presztak) in lxc/incus#2153
-   vscode: Add VSCode launch.json for incusd "Run and Debug" functionality by [@&#8203;mrstux](https://github.com/mrstux) in lxc/incus#2155
-   Add support for specifying username in CephFs commands by [@&#8203;presztak](https://github.com/presztak) in lxc/incus#2154
-   Port network_load_balancers to database generator by [@&#8203;davidbockelman](https://github.com/davidbockelman) in lxc/incus#2068
-   Porting network zone database functions to database generator. by [@&#8203;irhndt](https://github.com/irhndt) in lxc/incus#2108
-   Allow updating root disk size and root io.bus simultaneously by [@&#8203;presztak](https://github.com/presztak) in lxc/incus#2158
-   Fix daily test regressions by [@&#8203;stgraber](https://github.com/stgraber) in lxc/incus#2162

#### New Contributors

-   [@&#8203;DinglyCoder](https://github.com/DinglyCoder) made their first contribution in lxc/incus#1996
-   [@&#8203;The127](https://github.com/The127) made their first contribution in lxc/incus#2000
-   [@&#8203;allisoncchen](https://github.com/allisoncchen) made their first contribution in lxc/incus#2008
-   [@&#8203;lahariguduru](https://github.com/lahariguduru) made their first contribution in lxc/incus#2027
-   [@&#8203;janetkimmm](https://github.com/janetkimmm) made their first contribution in lxc/incus#2030
-   [@&#8203;cory-chang](https://github.com/cory-chang) made their first contribution in lxc/incus#2059
-   [@&#8203;arojas2003](https://github.com/arojas2003) made their first contribution in lxc/incus#2044
-   [@&#8203;Aryan470](https://github.com/Aryan470) made their first contribution in lxc/incus#2035
-   [@&#8203;stoven2k17](https://github.com/stoven2k17) made their first contribution in lxc/incus#2088
-   [@&#8203;OGCbn](https://github.com/OGCbn) made their first contribution in lxc/incus#2090
-   [@&#8203;davidbockelman](https://github.com/davidbockelman) made their first contribution in lxc/incus#2091

**Full Changelog**: lxc/incus@v6.12.0...v6.13.0

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever MR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this MR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this MR, check this box

---

This MR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0MC41MC4wIiwidXBkYXRlZEluVmVyIjoiNDAuNTAuMCIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOlsiUmVub3ZhdGUgQm90Il19-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Changes to the REST API
Development

Successfully merging this pull request may close these issues.

2 participants