[PATCH xserver 6/8] xkb: Silence some compiler warnings

Emil Velikov emil.l.velikov at gmail.com
Fri Apr 6 10:59:54 UTC 2018


On 5 April 2018 at 18:13, Adam Jackson <ajax at redhat.com> wrote:
> Of the form:
>
> ../xkb/XKBGAlloc.c: In function ‘SrvXkbAddGeomKeyAlias’:
> ../xkb/XKBGAlloc.c:591:13: warning: ‘strncpy’ specified bound 4 equals destination size [-Wstringop-truncation]
>              strncpy(alias->real, realStr, XkbKeyNameLength);
>              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> This is intentional; the code that reads from these fields never reads
> more than 4 bytes anyway. Rephrase things in terms of memcpy so that's
> clear. Obviously this is awful but in XKB awful is par.
>
> Signed-off-by: Adam Jackson <ajax at redhat.com>
> ---
>  xkb/XKBGAlloc.c | 11 ++++++-----
>  1 file changed, 6 insertions(+), 5 deletions(-)
>
> diff --git a/xkb/XKBGAlloc.c b/xkb/XKBGAlloc.c
> index e9f55fa434..8958b0c523 100644
> --- a/xkb/XKBGAlloc.c
> +++ b/xkb/XKBGAlloc.c
> @@ -588,7 +588,8 @@ XkbAddGeomKeyAlias(XkbGeometryPtr geom, char *aliasStr, char *realStr)
>           i++, alias++) {
>          if (strncmp(alias->alias, aliasStr, XkbKeyNameLength) == 0) {
>              memset(alias->real, 0, XkbKeyNameLength);
> -            strncpy(alias->real, realStr, XkbKeyNameLength);
> +            memcpy(alias->real, realStr,
> +                   min(XkbKeyNameLength, strlen(realStr)));

min(XkbKeyNameLength-1 ...
To make it clear that the resulting string is NULL terminated (props
to the memset above).

>              return alias;
>          }
>      }
> @@ -598,8 +599,8 @@ XkbAddGeomKeyAlias(XkbGeometryPtr geom, char *aliasStr, char *realStr)
>      }
>      alias = &geom->key_aliases[geom->num_key_aliases];
>      memset(alias, 0, sizeof(XkbKeyAliasRec));
> -    strncpy(alias->alias, aliasStr, XkbKeyNameLength);
> -    strncpy(alias->real, realStr, XkbKeyNameLength);
> +    memcpy(alias->alias, aliasStr, min(XkbKeyNameLength, strlen(aliasStr)));
> +    memcpy(alias->real, realStr, min(XkbKeyNameLength, strlen(realStr)));
Ditto - min(XkbKeyNameLength-1 ...

>      geom->num_key_aliases++;
>      return alias;
>  }
> @@ -814,8 +815,8 @@ XkbAddGeomOverlayKey(XkbOverlayPtr overlay,
>          (_XkbAllocOverlayKeys(row, 1) != Success))
>          return NULL;
>      key = &row->keys[row->num_keys];
> -    strncpy(key->under.name, under, XkbKeyNameLength);
> -    strncpy(key->over.name, over, XkbKeyNameLength);
> +    memcpy(key->under.name, under, min(XkbKeyNameLength, strlen(under)));
> +    memcpy(key->over.name, over, min(XkbKeyNameLength, strlen(over)));

AFAICT _XkbAllocOverlayKeys does memset(...0...), yet that may not
obvious to the reader or compiler.
It's worth staying consistent with the above memset + min(...-1...)?

-Emil


More information about the xorg-devel mailing list