]> granicus.if.org Git - graphviz/commitdiff
cdt: squash -Wconversion warning
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Thu, 3 Nov 2022 04:37:56 +0000 (21:37 -0700)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Fri, 4 Nov 2022 04:18:04 +0000 (21:18 -0700)
This squashes the warning:

  dtstrhash.c:23:21: warning: conversion from ‘long int’ to ‘int’ may change
    value [-Wconversion]
     23 |                 n = s - (unsigned char*)args;
        |                     ^

This cast is mostly guaranteed safe by the inability to practically pass a
>INT_MAX length string into this function.

lib/cdt/dtstrhash.c

index 41c99d3dbe616b4988b0382523a849948ba7ed73..39f7089e4bf518e17caaa5a8f13baba2aff4455c 100644 (file)
@@ -1,4 +1,7 @@
+#include       <assert.h>
 #include       <cdt/dthdr.h>
+#include       <limits.h>
+#include       <string.h>
 
 /* Hashing a string into an unsigned integer.
 ** The basic method is to continuingly accumulate bytes and multiply
@@ -20,7 +23,8 @@ uint dtstrhash(uint h, void* args, int n)
        if(n <= 0)
        {       for(; *s != 0; s += s[1] ? 2 : 1)
                        h = (h + ((unsigned)s[0] << 8u) + (unsigned)s[1]) * DT_PRIME;
-               n = s - (unsigned char*)args;
+               assert(strlen(args) <= INT_MAX);
+               n = (int)(s - (unsigned char*)args);
        }
        else
        {       unsigned char*  ends;