Add buffer size param to warc_uuid_str

* src/http.c (gethttp): Add buffer size param to warc_uuid_str.
* src/warc.c: Likewise
* src/warc.h: Likewise
This commit is contained in:
Tim Rühsen
2020-02-13 11:28:14 +01:00
parent e5d0dda561
commit 233f982f9d
3 changed files with 20 additions and 21 deletions

View File

@@ -3365,7 +3365,7 @@ gethttp (const struct url *u, struct url *original_url, struct http_stat *hs,
/* Generate a timestamp and uuid for this request. */ /* Generate a timestamp and uuid for this request. */
warc_timestamp (warc_timestamp_str, sizeof (warc_timestamp_str)); warc_timestamp (warc_timestamp_str, sizeof (warc_timestamp_str));
warc_uuid_str (warc_request_uuid); warc_uuid_str (warc_request_uuid, sizeof (warc_request_uuid));
/* Create a request record and store it in the WARC file. */ /* Create a request record and store it in the WARC file. */
warc_result = warc_write_request_record (u->url, warc_timestamp_str, warc_result = warc_write_request_record (u->url, warc_timestamp_str,

View File

@@ -631,7 +631,7 @@ warc_timestamp (char *timestamp, size_t timestamp_size)
The string will be 47 characters long. */ The string will be 47 characters long. */
#if HAVE_LIBUUID #if HAVE_LIBUUID
void void
warc_uuid_str (char *urn_str) warc_uuid_str (char *urn_str, size_t urn_size)
{ {
char uuid_str[37]; char uuid_str[37];
uuid_t record_id; uuid_t record_id;
@@ -639,11 +639,11 @@ warc_uuid_str (char *urn_str)
uuid_generate (record_id); uuid_generate (record_id);
uuid_unparse (record_id, uuid_str); uuid_unparse (record_id, uuid_str);
sprintf (urn_str, "<urn:uuid:%s>", uuid_str); snprintf (urn_str, urn_size, "<urn:uuid:%s>", uuid_str);
} }
#elif HAVE_UUID_CREATE #elif HAVE_UUID_CREATE
void void
warc_uuid_str (char *urn_str) warc_uuid_str (char *urn_str, size_t urn_size)
{ {
char *uuid_str; char *uuid_str;
uuid_t record_id; uuid_t record_id;
@@ -651,7 +651,7 @@ warc_uuid_str (char *urn_str)
uuid_create (&record_id, NULL); uuid_create (&record_id, NULL);
uuid_to_string (&record_id, &uuid_str, NULL); uuid_to_string (&record_id, &uuid_str, NULL);
sprintf (urn_str, "<urn:uuid:%s>", uuid_str); snprintf (urn_str, urn_size, "<urn:uuid:%s>", uuid_str);
xfree (uuid_str); xfree (uuid_str);
} }
#else #else
@@ -662,7 +662,7 @@ typedef RPC_STATUS (RPC_ENTRY * UuidToString_proc) (UUID *, unsigned char **);
typedef RPC_STATUS (RPC_ENTRY * RpcStringFree_proc) (unsigned char **); typedef RPC_STATUS (RPC_ENTRY * RpcStringFree_proc) (unsigned char **);
static int static int
windows_uuid_str (char *urn_str) windows_uuid_str (char *urn_str, size_t urn_size)
{ {
static UuidCreate_proc pfn_UuidCreate = NULL; static UuidCreate_proc pfn_UuidCreate = NULL;
static UuidToString_proc pfn_UuidToString = NULL; static UuidToString_proc pfn_UuidToString = NULL;
@@ -701,7 +701,7 @@ windows_uuid_str (char *urn_str)
{ {
if (pfn_UuidToString (&uuid, &uuid_str) == RPC_S_OK) if (pfn_UuidToString (&uuid, &uuid_str) == RPC_S_OK)
{ {
sprintf (urn_str, "<urn:uuid:%s>", uuid_str); snprintf (urn_str, urn_size, "<urn:uuid:%s>", uuid_str);
pfn_RpcStringFree (&uuid_str); pfn_RpcStringFree (&uuid_str);
return 1; return 1;
} }
@@ -719,7 +719,7 @@ windows_uuid_str (char *urn_str)
The string will be 47 characters long. */ The string will be 47 characters long. */
void void
warc_uuid_str (char *urn_str) warc_uuid_str (char *urn_str, size_t urn_size)
{ {
/* RFC 4122, a version 4 UUID with only random numbers */ /* RFC 4122, a version 4 UUID with only random numbers */
@@ -744,7 +744,7 @@ warc_uuid_str (char *urn_str)
* clock_seq_hi_and_reserved to zero and one, respectively. */ * clock_seq_hi_and_reserved to zero and one, respectively. */
uuid_data[8] = (uuid_data[8] & 0xBF) | 0x80; uuid_data[8] = (uuid_data[8] & 0xBF) | 0x80;
sprintf (urn_str, snprintf (urn_str, urn_size,
"<urn:uuid:%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x>", "<urn:uuid:%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x>",
uuid_data[0], uuid_data[1], uuid_data[2], uuid_data[3], uuid_data[4], uuid_data[0], uuid_data[1], uuid_data[2], uuid_data[3], uuid_data[4],
uuid_data[5], uuid_data[6], uuid_data[7], uuid_data[8], uuid_data[9], uuid_data[5], uuid_data[6], uuid_data[7], uuid_data[8], uuid_data[9],
@@ -765,7 +765,7 @@ warc_write_warcinfo_record (const char *filename)
/* Write warc-info record as the first record of the file. */ /* Write warc-info record as the first record of the file. */
/* We add the record id of this info record to the other records in the /* We add the record id of this info record to the other records in the
file. */ file. */
warc_uuid_str (warc_current_warcinfo_uuid_str); warc_uuid_str (warc_current_warcinfo_uuid_str, sizeof (warc_current_warcinfo_uuid_str));
warc_timestamp (timestamp, sizeof(timestamp)); warc_timestamp (timestamp, sizeof(timestamp));
@@ -895,11 +895,10 @@ warc_start_new_file (bool meta)
static bool static bool
warc_start_cdx_file (void) warc_start_cdx_file (void)
{ {
int filename_length = strlen (opt.warc_filename); char *cdx_filename = aprintf("%s.cdx", opt.warc_filename);
char *cdx_filename = alloca (filename_length + 4 + 1);
memcpy (cdx_filename, opt.warc_filename, filename_length);
memcpy (cdx_filename + filename_length, ".cdx", 5);
warc_current_cdx_file = fopen (cdx_filename, "a+"); warc_current_cdx_file = fopen (cdx_filename, "a+");
free(cdx_filename);
if (warc_current_cdx_file == NULL) if (warc_current_cdx_file == NULL)
return false; return false;
@@ -1205,7 +1204,7 @@ warc_write_metadata (void)
if (opt.warc_maxsize > 0) if (opt.warc_maxsize > 0)
warc_start_new_file (true); warc_start_new_file (true);
warc_uuid_str (manifest_uuid); warc_uuid_str (manifest_uuid, sizeof (manifest_uuid));
fflush (warc_manifest_fp); fflush (warc_manifest_fp);
warc_write_metadata_record (manifest_uuid, warc_write_metadata_record (manifest_uuid,
@@ -1423,7 +1422,7 @@ warc_write_revisit_record (const char *url, const char *timestamp_str,
char block_digest[BASE32_LENGTH(SHA1_DIGEST_SIZE) + 1 + 5]; char block_digest[BASE32_LENGTH(SHA1_DIGEST_SIZE) + 1 + 5];
char sha1_res_block[SHA1_DIGEST_SIZE]; char sha1_res_block[SHA1_DIGEST_SIZE];
warc_uuid_str (revisit_uuid); warc_uuid_str (revisit_uuid, sizeof (revisit_uuid));
sha1_stream (body, sha1_res_block); sha1_stream (body, sha1_res_block);
warc_base32_sha1_digest (sha1_res_block, block_digest, sizeof(block_digest)); warc_base32_sha1_digest (sha1_res_block, block_digest, sizeof(block_digest));
@@ -1518,7 +1517,7 @@ warc_write_response_record (const char *url, const char *timestamp_str,
/* Not a revisit, just store the record. */ /* Not a revisit, just store the record. */
warc_uuid_str (response_uuid); warc_uuid_str (response_uuid, sizeof (response_uuid));
fseeko (warc_current_file, 0L, SEEK_END); fseeko (warc_current_file, 0L, SEEK_END);
offset = ftello (warc_current_file); offset = ftello (warc_current_file);
@@ -1569,11 +1568,11 @@ warc_write_record (const char *record_type, const char *resource_uuid,
const ip_address *ip, const char *content_type, FILE *body, const ip_address *ip, const char *content_type, FILE *body,
off_t payload_offset) off_t payload_offset)
{ {
char uuid_buf[48];
if (resource_uuid == NULL) if (resource_uuid == NULL)
{ {
/* using uuid_buf allows const for resource_uuid in function declaration */ warc_uuid_str (uuid_buf, sizeof (uuid_buf));
char *uuid_buf = alloca (48);
warc_uuid_str (uuid_buf);
resource_uuid = uuid_buf; resource_uuid = uuid_buf;
} }

View File

@@ -6,7 +6,7 @@
void warc_init (void); void warc_init (void);
void warc_close (void); void warc_close (void);
void warc_uuid_str (char *id_str); void warc_uuid_str (char *id_str, size_t urn_size);
char * warc_timestamp (char *timestamp, size_t timestamp_size); char * warc_timestamp (char *timestamp, size_t timestamp_size);