Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

soc: qcom: cmd-db: replace strscpy_pad() with strncpy()

Commit ac0126a01735 ("soc: qcom: cmd-db: replace strncpy() with
strscpy_pad()") breaks booting on my sc7280-herobrine-herobrine
device. From printouts I see that at bootup the function is called
with an id of "lnbclka2" which is 8 bytes big.

Previously all 8 bytes of this string were copied to the
destination. Now only 7 bytes will be copied since strscpy_pad() saves
a byte for '\0' termination.

We don't need the '\0' termination in the destination. Let's go back
to strncpy(). According to the warning:
If a caller is using non-NUL-terminated strings, strncpy() can still
be used, but destinations should be marked with the __nonstring
attribute to avoid future compiler warnings.
...so we'll do that.

While we're at it, let's change the query array to use
"sizeof(ent->id)" so it can't possibly go out of sync with our later
copy.

Fixes: ac0126a01735 ("soc: qcom: cmd-db: replace strncpy() with strscpy_pad()")
Signed-off-by: Douglas Anderson <dianders@chromium.org>
Reviewed-by: Matthias Kaehlcke <mka@chromium.org>
Reviewed-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
Link: https://lore.kernel.org/r/20220628064301.v3.1.Ie7b480cd99e2c13319220cbc108caf2bcd41286b@changeid

authored by

Douglas Anderson and committed by
Bjorn Andersson
fe72f9bc 2ea6af6c

+7 -3
+7 -3
drivers/soc/qcom/cmd-db.c
··· 141 141 const struct rsc_hdr *rsc_hdr; 142 142 const struct entry_header *ent; 143 143 int ret, i, j; 144 - u8 query[8]; 144 + u8 query[sizeof(ent->id)] __nonstring; 145 145 146 146 ret = cmd_db_ready(); 147 147 if (ret) 148 148 return ret; 149 149 150 - /* Pad out query string to same length as in DB */ 151 - strscpy_pad(query, id, sizeof(query)); 150 + /* 151 + * Pad out query string to same length as in DB. NOTE: the output 152 + * query string is not necessarily '\0' terminated if it bumps up 153 + * against the max size. That's OK and expected. 154 + */ 155 + strncpy(query, id, sizeof(query)); 152 156 153 157 for (i = 0; i < MAX_SLV_ID; i++) { 154 158 rsc_hdr = &cmd_db_header->header[i];