util: Use a struct for afsUUID_to_string 31/13831/12
authorCheyenne Wills <cwills@sinenomine.net>
Mon, 2 Dec 2019 20:12:00 +0000 (13:12 -0700)
committerBenjamin Kaduk <kaduk@mit.edu>
Mon, 9 Dec 2019 08:19:28 +0000 (03:19 -0500)
Replace the use of a character array with a structure that contains
the size of the buffer that is needed.  This allows the C compiler to
perform a type check to ensure the correct sized buffer is used. In
addition, the size of the buffer is now specified in just one location.

Change the signature of the afsUUID_to_string function to return a
pointer to the start of a formatted UUID. This allows the use of
afsUUID_to_string in a way that is consistent with other object
formatting functions:

    struct uuid_fmtbuf uuidstr;
    printf("... %s ...",
             afsUUID_to_string(uuid, &uuidstr));

Update callers to use the new uuid_fmtbuf struct when calling
afsUUID_to_string.

Change-Id: I6d6f86ce6c058defc6256e8e88dee4449dd4f7e6
Reviewed-on: https://gerrit.openafs.org/13831
Tested-by: BuildBot <buildbot@rampaginggeek.com>
Reviewed-by: Benjamin Kaduk <kaduk@mit.edu>

src/WINNT/afsd/cm_getaddrs.c
src/WINNT/afsd/cm_server.c
src/WINNT/afsd/cm_volume.c
src/libafscp/afscp_server.c
src/util/afsutil_prototypes.h
src/util/uuid.c
src/venus/cmdebug.c
src/viced/host.c
src/viced/state_analyzer.c
src/volser/vos.c

index 29e9d1a..73752bc 100644 (file)
@@ -244,17 +244,17 @@ cm_GetAddrsU(cm_cell_t *cellp, cm_user_t *userp, cm_req_t *reqp,
        code = cm_MapVLRPCError(code, reqp);
 
        if (afsd_logp->enabled) {
-           char uuidstr[128];
-           afsUUID_to_string(Uuid, uuidstr, sizeof(uuidstr));
+           struct uuid_fmtbuf uuidstr;
+           afsUUID_to_string(Uuid, &uuidstr);
 
            if (code)
                osi_Log2(afsd_logp,
                          "CALL VL_GetAddrsU serverNumber %s FAILURE, code 0x%x",
-                         osi_LogSaveString(afsd_logp, uuidstr),
+                         osi_LogSaveString(afsd_logp, uuidstr.buffer),
                          code);
            else
                osi_Log1(afsd_logp, "CALL VL_GetAddrsU serverNumber %s SUCCESS",
-                         osi_LogSaveString(afsd_logp, uuidstr));
+                         osi_LogSaveString(afsd_logp, uuidstr.buffer));
        }
 
        if (code)
index fa3a7e9..155f1b4 100644 (file)
@@ -1606,7 +1606,7 @@ int cm_DumpServers(FILE *outputFile, char *cookie, int lock)
     int zilch;
     cm_server_t *tsp;
     char output[1024];
-    char uuidstr[128];
+    struct uuid_fmtbuf uuidstr;
     char hoststr[16];
 
     if (lock)
@@ -1634,8 +1634,7 @@ int cm_DumpServers(FILE *outputFile, char *cookie, int lock)
         default:
             type = "unknown";
         }
-
-        afsUUID_to_string(&tsp->uuid, uuidstr, sizeof(uuidstr));
+        afsUUID_to_string(&tsp->uuid, &uuidstr);
         afs_inet_ntoa_r(tsp->addr.sin_addr.s_addr, hoststr);
         down = ctime(&tsp->downTime);
         down[strlen(down)-1] = '\0';
@@ -1645,7 +1644,7 @@ int cm_DumpServers(FILE *outputFile, char *cookie, int lock)
                 "flags=0x%x waitCount=%u pingCount=%d rank=%u downTime=\"%s\" "
                 "refCount=%u\r\n",
                  cookie, tsp, tsp->cellp ? tsp->cellp->name : "", hoststr,
-                 ntohs(tsp->addr.sin_port), uuidstr, type,
+                 ntohs(tsp->addr.sin_port), uuidstr.buffer, type,
                 tsp->capabilities, tsp->flags, tsp->waitCount, tsp->pingCount,
                 tsp->activeRank,
                  (tsp->flags & CM_SERVERFLAG_DOWN) ?  "down" : "up",
index c85107b..1fdbf41 100644 (file)
@@ -581,16 +581,15 @@ long cm_UpdateVolumeLocation(struct cm_cell *cellp, cm_user_t *userp, cm_req_t *
                  * vlserver.  If not, ...?
                  */
                 if (!afs_uuid_equal(&serverUUID[i], &tsp->uuid)) {
-                    char uuid1[128], uuid2[128];
+                    struct uuid_fmtbuf uuid1, uuid2;
                     char hoststr[16];
-
-                    afsUUID_to_string(&serverUUID[i], uuid1, sizeof(uuid1));
-                    afsUUID_to_string(&tsp->uuid, uuid2, sizeof(uuid2));
+                    afsUUID_to_string(&serverUUID[i], &uuid1);
+                    afsUUID_to_string(&tsp->uuid, &uuid2);
                     afs_inet_ntoa_r(serverNumber[i], hoststr);
 
                     osi_Log3(afsd_logp, "cm_UpdateVolumeLocation UUIDs do not match! %s != %s (%s)",
-                              osi_LogSaveString(afsd_logp, uuid1),
-                              osi_LogSaveString(afsd_logp, uuid2),
+                              osi_LogSaveString(afsd_logp, uuid1.buffer),
+                              osi_LogSaveString(afsd_logp, uuid2.buffer),
                               osi_LogSaveString(afsd_logp, hoststr));
                 }
             }
index 726d2e5..590b1fe 100644 (file)
@@ -264,9 +264,9 @@ afscp_ServerById(struct afscp_cell *thecell, afsUUID * u)
     bulkaddrs addrs;
     struct ListAddrByAttributes attrs;
     afs_int32 nentries, uniq;
-    char s[512];
-    afsUUID_to_string(u, s, 511);
-    afs_dprintf(("GetServerByID %s\n", s));
+    struct uuid_fmtbuf s;
+    afsUUID_to_string(u, &s);
+    afs_dprintf(("GetServerByID %s\n", s.buffer));
 
     for (i = 0; i < thecell->nservers; i++) {
        if (afs_uuid_equal(&thecell->fsservers[i]->id, u)) {
@@ -400,10 +400,10 @@ afscp_ServerByAddr(struct afscp_cell *thecell, afs_uint32 addr)
                                         1, thecell->security,
                                         thecell->scindex);
     } else {
-       char s[512];
+       struct uuid_fmtbuf s;
 
-       afsUUID_to_string(&uuid, s, 511);
-       afs_dprintf(("GetServerByAddr 0x%x -> uuid %s\n", addr, s));
+       afsUUID_to_string(&uuid, &s);
+       afs_dprintf(("GetServerByAddr 0x%x -> uuid %s\n", addr, s.buffer));
 
        if (nentries > AFS_MAXHOSTS) {
            nentries = AFS_MAXHOSTS;
index bea842c..21f8378 100644 (file)
@@ -162,7 +162,11 @@ extern afs_int32 afs_uuid_create(afsUUID * uuid);
 extern u_short afs_uuid_hash(afsUUID * uuid);
 #if !defined(KERNEL) && !defined(UKERNEL)
 extern int afsUUID_from_string(const char *str, afsUUID * uuid);
-extern int afsUUID_to_string(const afsUUID * uuid, char *str, size_t strsz);
+
+struct uuid_fmtbuf {
+    char buffer[38];
+};
+extern char *afsUUID_to_string(const afsUUID * uuid, struct uuid_fmtbuf *buf);
 #endif
 
 /* volparse.c */
index dbf77d0..71b813a 100644 (file)
@@ -198,14 +198,21 @@ afsUUID_from_string(const char *str, afsUUID * uuid)
     return 0;
 }
 
-/*
+/**
  *    Converts a UUID from binary representation to a string representation.
+ *
+ *    @param[in]  uuid pointer to a afsUUID
+ *    @param[out] buf  format work buffer
+ *
+ *    @returns pointer to buffer containing string representation of "uuid"
  */
 
-int
-afsUUID_to_string(const afsUUID * uuid, char *str, size_t strsz)
+char *
+afsUUID_to_string(const afsUUID * uuid, struct uuid_fmtbuf *buf)
 {
-    snprintf(str, strsz, "%08x-%04x-%04x-%02x-%02x-%02x%02x%02x%02x%02x%02x",
+    memset(buf, 0, sizeof(*buf));
+    snprintf(buf->buffer, sizeof(buf->buffer),
+            "%08x-%04x-%04x-%02x-%02x-%02x%02x%02x%02x%02x%02x",
             uuid->time_low, uuid->time_mid, uuid->time_hi_and_version,
             (unsigned char)uuid->clock_seq_hi_and_reserved,
             (unsigned char)uuid->clock_seq_low, (unsigned char)uuid->node[0],
@@ -213,7 +220,7 @@ afsUUID_to_string(const afsUUID * uuid, char *str, size_t strsz)
             (unsigned char)uuid->node[3], (unsigned char)uuid->node[4],
             (unsigned char)uuid->node[5]);
 
-    return 0;
+    return buf->buffer;
 }
 #endif
 
index bb1c953..2f16274 100644 (file)
@@ -82,7 +82,7 @@ PrintInterfaces(struct rx_connection *aconn)
 #ifdef AFS_NT40_ENV
     char * p;
 #else
-    char uuidstr[128];
+    struct uuid_fmtbuf uuidstr;
 #endif
     int i, code;
     char hoststr[16];
@@ -104,8 +104,8 @@ PrintInterfaces(struct rx_connection *aconn)
     printf("UUID: %s\n",p);
     RpcStringFree(&p);
 #else
-    afsUUID_to_string(&addr.uuid, uuidstr, sizeof(uuidstr));
-    printf("UUID: %s\n",uuidstr);
+    afsUUID_to_string(&addr.uuid, &uuidstr);
+    printf("UUID: %s\n", uuidstr.buffer);
 #endif
 
     printf("Host interfaces:\n");
index fe10ac0..0bceeb1 100644 (file)
@@ -1113,7 +1113,7 @@ h_AddHostToUuidHashTable_r(struct afsUUID *uuid, struct host *host)
 {
     int index;
     struct h_UuidHashChain *chain;
-    char uuid1[128], uuid2[128];
+    struct uuid_fmtbuf uuid1, uuid2;
     char hoststr[16];
 
     /* hash into proper bucket */
@@ -1127,13 +1127,12 @@ h_AddHostToUuidHashTable_r(struct afsUUID *uuid, struct host *host)
        if (chain->hostPtr->z.interface &&
            afs_uuid_equal(&chain->hostPtr->z.interface->uuid, uuid)) {
            if (GetLogLevel() >= 125) {
-               afsUUID_to_string(&chain->hostPtr->z.interface->uuid, uuid1,
-                                 127);
-               afsUUID_to_string(uuid, uuid2, 127);
+               afsUUID_to_string(&chain->hostPtr->z.interface->uuid, &uuid1);
+               afsUUID_to_string(uuid, &uuid2);
                ViceLog(125, ("h_AddHostToUuidHashTable_r: host %p (uuid %s) exists as %s:%d (uuid %s)\n",
-                             host, uuid1,
+                             host, uuid1.buffer,
                              afs_inet_ntoa_r(chain->hostPtr->z.host, hoststr),
-                             ntohs(chain->hostPtr->z.port), uuid2));
+                             ntohs(chain->hostPtr->z.port), uuid2.buffer));
            }
            return;
        }
@@ -1149,11 +1148,11 @@ h_AddHostToUuidHashTable_r(struct afsUUID *uuid, struct host *host)
     hostUuidHashTable[index] = chain;
          if (GetLogLevel() < 125)
               return;
-     afsUUID_to_string(uuid, uuid2, 127);
+     afsUUID_to_string(uuid, &uuid2);
      ViceLog(125,
             ("h_AddHostToUuidHashTable_r: host %p (%s:%d) added as uuid %s\n",
              host, afs_inet_ntoa_r(chain->hostPtr->z.host, hoststr),
-             ntohs(chain->hostPtr->z.port), uuid2));
+             ntohs(chain->hostPtr->z.port), uuid2.buffer));
 }
 
 /* deletes a HashChain structure corresponding to this host */
@@ -1162,7 +1161,7 @@ h_DeleteHostFromUuidHashTable_r(struct host *host)
 {
      int index;
      struct h_UuidHashChain **uhp, *uth;
-     char uuid1[128];
+     struct uuid_fmtbuf uuid1;
      char hoststr[16];
 
      if (!host->z.interface)
@@ -1172,13 +1171,13 @@ h_DeleteHostFromUuidHashTable_r(struct host *host)
      index = h_UuidHashIndex(&host->z.interface->uuid);
 
      if (GetLogLevel() >= 125)
-        afsUUID_to_string(&host->z.interface->uuid, uuid1, 127);
+        afsUUID_to_string(&host->z.interface->uuid, &uuid1);
      for (uhp = &hostUuidHashTable[index]; (uth = *uhp); uhp = &uth->next) {
          opr_Assert(uth->hostPtr);
         if (uth->hostPtr == host) {
             ViceLog(125,
                     ("h_DeleteHostFromUuidHashTable_r: host %p (uuid %s %s:%d)\n",
-                     host, uuid1, afs_inet_ntoa_r(host->z.host, hoststr),
+                     host, uuid1.buffer, afs_inet_ntoa_r(host->z.host, hoststr),
                      ntohs(host->z.port)));
             *uhp = uth->next;
             free(uth);
@@ -1187,7 +1186,7 @@ h_DeleteHostFromUuidHashTable_r(struct host *host)
      }
      ViceLog(125,
             ("h_DeleteHostFromUuidHashTable_r: host %p (uuid %s %s:%d) not found\n",
-             host, uuid1, afs_inet_ntoa_r(host->z.host, hoststr),
+             host, uuid1.buffer, afs_inet_ntoa_r(host->z.host, hoststr),
              ntohs(host->z.port)));
      return 0;
 }
@@ -2141,18 +2140,20 @@ h_GetHost_r(struct rx_connection *tcon)
                || (identP->valid
                    && !afs_uuid_equal(&identP->uuid,
                                       &host->z.interface->uuid)))) {
-           char uuid1[128], uuid2[128];
+
+           struct uuid_fmtbuf uuid1, uuid2;
+
            if (identP->valid)
-               afsUUID_to_string(&identP->uuid, uuid1, 127);
+               afsUUID_to_string(&identP->uuid, &uuid1);
            if (host->z.interface)
-               afsUUID_to_string(&host->z.interface->uuid, uuid2, 127);
+               afsUUID_to_string(&host->z.interface->uuid, &uuid2);
            ViceLog(0,
                    ("CB: new identity for host %p (%s:%d), "
                     "deleting(%x %p %s %s)\n",
                     host, afs_inet_ntoa_r(host->z.host, hoststr), ntohs(host->z.port),
                     identP->valid, host->z.interface,
-                    identP->valid ? uuid1 : "no_uuid",
-                    host->z.interface ? uuid2 : "no_uuid"));
+                    identP->valid ? uuid1.buffer : "no_uuid",
+                    host->z.interface ? uuid2.buffer : "no_uuid"));
 
            /* The host in the cache is not the host for this connection */
             h_Lock_r(host);
@@ -3405,14 +3406,15 @@ h_stateVerifyUuidHash(struct fs_dump_state * state, struct host * h)
     struct h_UuidHashChain *chain;
     afsUUID * uuidp = &h->z.interface->uuid;
     int index = h_UuidHashIndex(uuidp);
-    char tmp[40];
+    struct uuid_fmtbuf tmp;
     int chain_len = 0;
 
     for (chain = hostUuidHashTable[index]; chain; chain = chain->next) {
        host = chain->hostPtr;
        if (host == NULL) {
-           afsUUID_to_string(uuidp, tmp, sizeof(tmp));
-           ViceLog(0, ("h_stateVerifyUuidHash: error: uuid hash chain has NULL host ptr (lookup uuid %s)\n", tmp));
+           afsUUID_to_string(uuidp, &tmp);
+           ViceLog(0, ("h_stateVerifyUuidHash: error: uuid hash chain has NULL host ptr (lookup uuid %s)\n",
+                       tmp.buffer));
            ret = 1;
            goto done;
        }
@@ -3436,13 +3438,15 @@ h_stateVerifyUuidHash(struct fs_dump_state * state, struct host * h)
 
     /* Fall through, so host not found */
 
-    afsUUID_to_string(uuidp, tmp, sizeof(tmp));
+    afsUUID_to_string(uuidp, &tmp);
     if (state->mode == FS_STATE_LOAD_MODE) {
-       ViceLog(0, ("h_stateVerifyUuidHash: error: uuid %s not found in hash\n", tmp));
+       ViceLog(0, ("h_stateVerifyUuidHash: error: uuid %s not found in hash\n",
+                   tmp.buffer));
        ret = 1;
        goto done;
     } else {
-       ViceLog(0, ("h_stateVerifyUuidHash: warning: uuid %s not found in hash\n", tmp));
+       ViceLog(0, ("h_stateVerifyUuidHash: warning: uuid %s not found in hash\n",
+                   tmp.buffer));
        state->flags.warnings_generated = 1;
     }
 
@@ -4141,7 +4145,7 @@ initInterfaceAddr_r(struct host *host, struct interfaceAddr *interf)
     int found;
     struct Interface *interface;
     char hoststr[16];
-    char uuidstr[128];
+    struct uuid_fmtbuf uuidstr;
     afs_uint16 port7001 = htons(7001);
 
     opr_Assert(host);
@@ -4257,9 +4261,9 @@ initInterfaceAddr_r(struct host *host, struct interfaceAddr *interf)
     h_AddHostToUuidHashTable_r(&interface->uuid, host);
 
     if (GetLogLevel() >= 125) {
-       afsUUID_to_string(&interface->uuid, uuidstr, 127);
+       afsUUID_to_string(&interface->uuid, &uuidstr);
 
-       ViceLog(125, ("--- uuid %s\n", uuidstr));
+       ViceLog(125, ("--- uuid %s\n", uuidstr.buffer));
        for (i = 0; i < host->z.interface->numberOfInterfaces; i++) {
            ViceLog(125, ("--- alt address %s:%d\n",
                          afs_inet_ntoa_r(host->z.interface->interface[i].addr, hoststr),
index d5b8dbb..b46c4ca 100644 (file)
@@ -751,7 +751,7 @@ print_cb_help(void)
 static void
 dump_hdr(void)
 {
-    char uuid_str[40];
+    struct uuid_fmtbuf uuid_str;
     afs_uint32 hi, lo;
 
     if (get_hdr())
@@ -766,8 +766,8 @@ dump_hdr(void)
     DPFT1("timestamp", hdrs.hdr.timestamp);
     DPFV1("sys_name", "u", hdrs.hdr.sys_name);
 
-    afsUUID_to_string(&hdrs.hdr.server_uuid, uuid_str, sizeof(uuid_str));
-    DPFS1("server_uuid", uuid_str);
+    afsUUID_to_string(&hdrs.hdr.server_uuid, &uuid_str);
+    DPFS1("server_uuid", uuid_str.buffer);
     DPFV1("valid", "d", hdrs.hdr.valid);
     DPFV1("endianness", "d", hdrs.hdr.endianness);
     DPFV1("stats_detailed", "d", hdrs.hdr.stats_detailed);
@@ -1265,6 +1265,7 @@ static void
 dump_he_interfaces(void)
 {
     char temp_str[40];
+    struct uuid_fmtbuf uuidstr;
     struct Interface * ifp;
     int len, i;
     char hoststr[16];
@@ -1281,8 +1282,8 @@ dump_he_interfaces(void)
     DPFSO0("Interface");
     DPFV1("numberOfInterfaces", "u", ifp->numberOfInterfaces);
 
-    afsUUID_to_string(&ifp->uuid, temp_str, sizeof(temp_str));
-    DPFS1("uuid", temp_str);
+    afsUUID_to_string(&ifp->uuid, &uuidstr);
+    DPFS1("uuid", uuidstr.buffer);
     for (i = 0; i < he_cursor.hdr.interfaces; i++) {
        snprintf(temp_str, sizeof(temp_str), "interface[%d]", i);
        DPFSO1(temp_str);
index ac0ca41..ab18ed0 100644 (file)
@@ -5261,12 +5261,11 @@ ChangeAddr(struct cmd_syndesc *as, void *arock)
        afs_int32 m_uniq = 0;
        afsUUID m_uuid;
        ListAddrByAttributes m_attrs;
-       char buffer[128];
+       struct uuid_fmtbuf buffer;
 
        memset(&m_attrs, 0, sizeof(m_attrs));
        memset(&m_uuid, 0, sizeof(m_uuid));
        memset(&m_addrs, 0, sizeof(m_addrs));
-       memset(buffer, 0, sizeof(buffer));
 
        m_attrs.Mask = VLADDR_IPADDR;
        m_attrs.ipaddr = ntohl(ip1);    /* -oldaddr */
@@ -5277,9 +5276,10 @@ ChangeAddr(struct cmd_syndesc *as, void *arock)
        xdr_free((xdrproc_t) xdr_bulkaddrs, &m_addrs);
        switch (vcode) {
        case 0:         /* mh entry detected */
-           afsUUID_to_string(&m_uuid, buffer, sizeof(buffer) - 1);
+           afsUUID_to_string(&m_uuid, &buffer);
            fprintf(STDERR, "vos: Refusing to change address in multi-homed server entry.\n");
-           fprintf(STDERR, "     -oldaddr address is registered to file server UUID %s\n", buffer);
+           fprintf(STDERR, "     -oldaddr address is registered to file server UUID %s\n",
+                           buffer.buffer);
            fprintf(STDERR, "     Please restart the file server or use vos setaddrs.\n");
            return EINVAL;
        case VL_NOENT:
@@ -5328,11 +5328,11 @@ print_addrs(const bulkaddrs * addrs, afsUUID * m_uuid, int nentries,
 {
     int i;
     afs_uint32 addr;
-    char buf[1024];
+    struct uuid_fmtbuf buf;
 
     if (print) {
-       afsUUID_to_string(m_uuid, buf, sizeof(buf));
-       printf("UUID: %s\n", buf);
+       afsUUID_to_string(m_uuid, &buf);
+       printf("UUID: %s\n", buf.buffer);
     }
 
     /* print out the list of all the server */