Ticket #303: win32-uuid-cleanup.patch

File win32-uuid-cleanup.patch, 2.1 KB (added by Olly Betts, 16 years ago)

cleanup patch

  • common/win32_uuid.cc

     
    2222
    2323#include "win32_uuid.h"
    2424
    25 #include <xapian/error.h>
     25#include "xapian/error.h"
     26
     27#include <cstring>
     28
     29using namespace std;
     30
     31/// The size of a UUID in bytes.
     32const size_t UUID_SIZE = 16;
     33
     34/// The size of a UUID string in bytes (not including trailing '\0').
     35const size_t UUID_STRING_SIZE = 36;
    2636
    2737void
    2838uuid_generate(uuid_t uu)
    2939{
    3040    UUID uuid;
    31     if (UuidCreate(&uuid) != RPC_S_OK) {
     41    if (rare(UuidCreate(&uuid) != RPC_S_OK)) {
    3242        // Throw a DatabaseCreateError, since we can't make a UUID.  The
    3343        // windows API documentation is a bit unclear about the situations in
    3444        // which this can happen, but if this behaviour causes a problem, an
     
    3646        // situation.
    3747        throw Xapian::DatabaseCreateError("Cannot create UUID");
    3848    }
    39     memcpy(uu, &uuid, 16);
     49    memcpy(uu, &uuid, UUID_SIZE);
    4050}
    4151
    4252int
     
    4555    UUID uuid;
    4656    if (UuidFromString((unsigned char*)in, &uuid) != RPC_S_OK)
    4757        return -1;
    48     memcpy(uu, &uuid, 16);
     58    memcpy(uu, &uuid, UUID_SIZE);
    4959    return 0;
    5060}
    5161
    52 void uuid_unparse_lower(const uuid_t uu, char *  out)
     62void uuid_unparse_lower(const uuid_t uu, char * out)
    5363{
    5464    UUID uuid;
    5565    char *uuidstr;
    56     memcpy(&uuid, uu, 16);
    57     if (UuidToString(&uuid, (unsigned char **)(&uuidstr)) != RPC_S_OK)
    58         return;
    59     int uuidlen = strlen(uuidstr);
    60     strncpy( out, strlwr(uuidstr), uuidlen );
     66    memcpy(&uuid, uu, UUID_SIZE);
     67    if (rare(UuidToString(&uuid, (unsigned char **)(&uuidstr)) != RPC_S_OK)) {
     68        // The only documented (or really conceivable) error code is
     69        // RPC_S_OUT_OF_MEMORY.
     70        throw std::bad_alloc();
     71    }
     72    memcpy(out, strlwr(uuidstr), UUID_STRING_SIZE);
     73    out[UUID_STRING_SIZE] = '\0';
    6174    RpcStringFree((unsigned char**)(&uuidstr));
    6275}
    6376
    6477void uuid_clear(uuid_t uu)
    6578{
    66     memset(uu, 0x00, 16);
     79    memset(uu, 0, UUID_SIZE);
    6780}
    6881
    6982int uuid_is_null(const uuid_t uu)
    7083{
    7184    int i = 0;
    72     while (i < 16) {
     85    while (i < UUID_SIZE) {
    7386        if (uu[i++])
    7487            return 0;
    7588    }