Skip to content

Commit

Permalink
Correct calls to printf functions
Browse files Browse the repository at this point in the history
The existing calls often used incorrect specifiers for the argument types.

The PRIx64 style specifiers and z length modifiers for uint64_t style and
size_t integer types are portable between architectures while manual length
modifiers are not.

Drop casts from arguments where a length modifier should be used instead.

Add casts to arguments where needed to change types to what printf expects.

Correct types or signedness of variables where the printf specifier is better.

Switch from printing pointers as integers to printing them as pointers.

Also ensure a space is present before and after PRIx64 style specifiers,
for compatibility with C++11.

Suggested-by: https://github.com/csutils/csmock
See-also: pst-format#6
  • Loading branch information
pabs3 committed Nov 12, 2022
1 parent 1c6ae99 commit ea800b1
Show file tree
Hide file tree
Showing 9 changed files with 186 additions and 186 deletions.
6 changes: 3 additions & 3 deletions src/debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ void pst_debug_hexdumper(FILE *out, const char *buf, size_t size, int cols, int
if (cols == -1) cols = NUM_COL;
fprintf(out, "\n");
while (off < size) {
fprintf(out, "%06d %.*s%06"PRIx64"\t:", getpid(), le*4, indent, (int64_t)(off+delta));
fprintf(out, "%06d %.*s%06zx\t:", getpid(), le*4, indent, off+delta);
toff = off;
while (count < cols && off < size) {
fprintf(out, "%02hhx ", (unsigned char)buf[off]);
Expand Down Expand Up @@ -169,7 +169,7 @@ void pst_debug_close(void) {
void *pst_malloc(size_t size) {
void *mem = malloc(size);
if (!mem) {
fprintf(stderr, "pst_malloc: Out Of memory [req: %ld]\n", (long)size);
fprintf(stderr, "pst_malloc: Out Of memory [req: %zd]\n", size);
exit(1);
}
return mem;
Expand All @@ -179,7 +179,7 @@ void *pst_malloc(size_t size) {
void *pst_realloc(void *ptr, size_t size) {
void *mem = realloc(ptr, size);
if (!mem) {
fprintf(stderr, "pst_realloc: Out Of memory [req: %ld]\n", (long)size);
fprintf(stderr, "pst_realloc: Out Of memory [req: %zd]\n", size);
exit(1);
}
return mem;
Expand Down
4 changes: 2 additions & 2 deletions src/dumpblocks.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ int main(int argc, char* const* argv)
pst_index_ll *ptr = &pstfile.i_table[i];
size_t c = pst_ff_getIDblock_dec(&pstfile, ptr->i_id, &buf);
if (c) {
snprintf(outname, OUT_BUF, "%#"PRIx64, ptr->i_id);
snprintf(outname, OUT_BUF, "%#" PRIx64, ptr->i_id);
if ((fp = fopen(outname, "wb")) == NULL) {
printf("Failed to open file %s\n", outname);
continue;
}
pst_fwrite(buf, 1, c, fp);
fclose(fp);
} else {
printf("Failed to read block i_id %#"PRIx64"\n", ptr->i_id);
printf("Failed to read block i_id %#" PRIx64 "\n", ptr->i_id);
}
}
pst_close(&pstfile);
Expand Down
10 changes: 5 additions & 5 deletions src/getidblock.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,25 @@ void dumper(uint64_t i_id)
size_t readSize;
pst_desc_tree *ptr;

DEBUG_INFO(("\n\n\nLooking at block index1 id %#"PRIx64"\n", i_id));
DEBUG_INFO(("\n\n\nLooking at block index1 id %#" PRIx64 "\n", i_id));

if ((readSize = pst_ff_getIDblock_dec(&pstfile, i_id, &buf)) <= 0 || buf == 0) {
DIE(("Error loading block\n"));
}

DEBUG_INFO(("Printing block i_id %#"PRIx64", size %#"PRIx64"\n", i_id, (uint64_t)readSize));
DEBUG_INFO(("Printing block i_id %#" PRIx64 ", size %#zx\n", i_id, readSize));
if (binary) {
if (fwrite(buf, 1, readSize, stdout) != 0) {
DIE(("Error occurred during writing of buf to stdout\n"));
}
} else {
printf("Block id %#"PRIx64", size %#"PRIx64"\n", i_id, (uint64_t)readSize);
printf("Block id %#" PRIx64 ", size %#zx\n", i_id, readSize);
pst_debug_hexdumper(stdout, buf, readSize, 0x10, 0);
}
if (buf) free(buf);

if (process) {
DEBUG_INFO(("Parsing block id %#"PRIx64"\n", i_id));
DEBUG_INFO(("Parsing block id %#" PRIx64 "\n", i_id));
ptr = pstfile.d_head;
while (ptr) {
if (ptr->assoc_tree && ptr->assoc_tree->i_id == i_id)
Expand All @@ -68,7 +68,7 @@ void dump_desc(pst_desc_tree *ptr, pst_desc_tree *parent)
{
while (ptr) {
uint64_t parent_d_id = (parent) ? parent->d_id : 0;
printf("Descriptor block d_id %#"PRIx64" parent d_id %#"PRIx64" children %i desc.i_id=%#"PRIx64", assoc tree.i_id=%#"PRIx64"\n",
printf("Descriptor block d_id %#" PRIx64 " parent d_id %#" PRIx64 " children %" PRIi32 " desc.i_id=%#" PRIx64 ", assoc tree.i_id=%#" PRIx64 "\n",
ptr->d_id, parent_d_id, ptr->no_child,
(ptr->desc ? ptr->desc->i_id : (uint64_t)0),
(ptr->assoc_tree ? ptr->assoc_tree->i_id : (uint64_t)0));
Expand Down
290 changes: 145 additions & 145 deletions src/libpst.c

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/lspst.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ void process(pst_item *outeritem, pst_desc_tree *d_ptr, struct options o)
ff.skip_count++;
}
else {
DEBUG_INFO(("Desc Email ID %"PRIx64" [d_ptr->d_id = %"PRIx64"]\n", d_ptr->desc->i_id, d_ptr->d_id));
DEBUG_INFO(("Desc Email ID %" PRIx64 " [d_ptr->d_id = %" PRIx64 "]\n", d_ptr->desc->i_id, d_ptr->d_id));

item = pst_parse_item(&pstfile, d_ptr, NULL);
DEBUG_INFO(("About to process item @ %p.\n", item));
DEBUG_INFO(("About to process item @ %p.\n", (void*)item));
if (item) {
if (item->message_store) {
// there should only be one message_store, and we have already done it
Expand Down
6 changes: 3 additions & 3 deletions src/lzfu.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ char* pst_lzfu_decompress(char* rtfcomp, uint32_t compsize, size_t *size) {
LE32_CPU(lzfuhdr.cbRawSize);
LE32_CPU(lzfuhdr.dwMagic);
LE32_CPU(lzfuhdr.dwCRC);
//printf("total size: %d\n", lzfuhdr.cbSize+4);
//printf("raw size : %d\n", lzfuhdr.cbRawSize);
//printf("total size: %" PRIu32 "\n", lzfuhdr.cbSize+4);
//printf("raw size : %" PRIu32 "\n", lzfuhdr.cbRawSize);
//printf("compressed: %s\n", (lzfuhdr.dwMagic == LZFU_COMPRESSED ? "yes" : "no"));
//printf("CRC : %#x\n", lzfuhdr.dwCRC);
//printf("CRC : %#" PRIx32 "\n", lzfuhdr.dwCRC);
//printf("\n");
out_size = lzfuhdr.cbRawSize;
out_buf = (char*)pst_malloc(out_size);
Expand Down
14 changes: 7 additions & 7 deletions src/msg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ static void convert_8bit(pst_string &str, const char *charset) {
static void empty_property(GsfOutfile *out, uint32_t tag);
static void empty_property(GsfOutfile *out, uint32_t tag) {
vector<char> n(50);
snprintf(&n[0], n.size(), "__substg1.0_%08X", tag);
snprintf(&n[0], n.size(), "__substg1.0_%08" PRIX32, tag);
GsfOutput* dst = gsf_outfile_new_child(out, &n[0], false);
gsf_output_close(dst);
g_object_unref(G_OBJECT(dst));
Expand All @@ -74,7 +74,7 @@ static void string_property(GsfOutfile *out, property_list &prop, uint32_t tag,
size_t term = ((tag & 0x0000ffff) == 0x001e) ? 1 :
((tag & 0x0000ffff) == 0x001f) ? 2 : 0; // null terminator
vector<char> n(50);
snprintf(&n[0], n.size(), "__substg1.0_%08X", tag);
snprintf(&n[0], n.size(), "__substg1.0_%08" PRIX32, tag);
GsfOutput* dst = gsf_outfile_new_child(out, &n[0], false);
gsf_output_write(dst, size, (const guint8*)contents);
if (term) {
Expand All @@ -97,7 +97,7 @@ static void string_property(GsfOutfile *out, property_list &prop, uint32_t tag,
static void string_property(GsfOutfile *out, property_list &prop, uint32_t tag, FILE *fp);
static void string_property(GsfOutfile *out, property_list &prop, uint32_t tag, FILE *fp) {
vector<char> n(50);
snprintf(&n[0], n.size(), "__substg1.0_%08X", tag);
snprintf(&n[0], n.size(), "__substg1.0_%08" PRIX32, tag);
GsfOutput* dst = gsf_outfile_new_child(out, &n[0], false);

size_t size = 0;
Expand Down Expand Up @@ -293,7 +293,7 @@ void write_msg_email(char *fname, pst_item* item, pst_file* pst) {
{
vector<char> n(50);
{
snprintf(&n[0], n.size(), "__recip_version1.0_#%08X", top_head.recipient_count);
snprintf(&n[0], n.size(), "__recip_version1.0_#%08" PRIX32, top_head.recipient_count);
GsfOutput *output = gsf_outfile_new_child(out, &n[0], true);
{
int v = 1; // to
Expand All @@ -316,7 +316,7 @@ void write_msg_email(char *fname, pst_item* item, pst_file* pst) {
}
}
if (email.cc_address.str) {
snprintf(&n[0], n.size(), "__recip_version1.0_#%08X", top_head.recipient_count);
snprintf(&n[0], n.size(), "__recip_version1.0_#%08" PRIX32, top_head.recipient_count);
GsfOutput *output = gsf_outfile_new_child(out, &n[0], true);
{
int v = 2; // cc
Expand All @@ -335,7 +335,7 @@ void write_msg_email(char *fname, pst_item* item, pst_file* pst) {
}
}
if (email.bcc_address.str) {
snprintf(&n[0], n.size(), "__recip_version1.0_#%08X", top_head.recipient_count);
snprintf(&n[0], n.size(), "__recip_version1.0_#%08" PRIX32, top_head.recipient_count);
GsfOutput *output = gsf_outfile_new_child(out, &n[0], true);
{
int v = 3; // bcc
Expand All @@ -362,7 +362,7 @@ void write_msg_email(char *fname, pst_item* item, pst_file* pst) {
}
else if (a->data.data || a->i_id) {
vector<char> n(50);
snprintf(&n[0], n.size(), "__attach_version1.0_#%08X", top_head.attachment_count);
snprintf(&n[0], n.size(), "__attach_version1.0_#%08" PRIX32, top_head.attachment_count);
GsfOutput *output = gsf_outfile_new_child(out, &n[0], true);
{
FILE *fp = fopen("temp_file_attachment", "w+b");
Expand Down
2 changes: 1 addition & 1 deletion src/pst2dii.cpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ static void process(pst_item *outeritem, file_ll *parent, pst_desc_tree *d_ptr)
for (; d_ptr; d_ptr = d_ptr->next) {
if (d_ptr->desc) {
item = pst_parse_item(&pstfile, d_ptr, NULL);
DEBUG_INFO(("item pointer is %p\n", item));
DEBUG_INFO(("item pointer is %p\n", (void*)item));
if (item) {
if (item->folder && item->file_as.str && d_ptr->child ) {
//if this is a non-empty folder, we want to recurse into it
Expand Down
36 changes: 18 additions & 18 deletions src/readpst.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#define OUTPUT_TEMPLATE "%s.%s"
#define OUTPUT_KMAIL_DIR_TEMPLATE ".%s.directory"
#define KMAIL_INDEX "../.%s.index"
#define SEP_MAIL_FILE_TEMPLATE "%i%s"
#define SEP_MAIL_FILE_TEMPLATE "%" PRIi32 "%s"

// max size of the c_time char*. It will store the date of the email
#define C_TIME_SIZE 500
Expand Down Expand Up @@ -252,7 +252,7 @@ void process(pst_item *outeritem, pst_desc_tree *d_ptr)
DEBUG_WARN(("ERROR item's desc record is NULL\n"));
continue;
}
DEBUG_INFO(("Desc Email ID %#"PRIx64" [d_ptr->d_id = %#"PRIx64"]\n", d_ptr->desc->i_id, d_ptr->d_id));
DEBUG_INFO(("Desc Email ID %#" PRIx64 " [d_ptr->d_id = %#" PRIx64 "]\n", d_ptr->desc->i_id, d_ptr->d_id));

item = pst_parse_item(&pstfile, d_ptr, NULL);
DEBUG_INFO(("About to process item\n"));
Expand Down Expand Up @@ -1068,13 +1068,13 @@ void write_separate_attachment(char f_name[], pst_item_attach* attach, int attac
char *attach_filename = (attach->filename2.str) ? attach->filename2.str
: attach->filename1.str;
DEBUG_ENT("write_separate_attachment");
DEBUG_INFO(("Attachment %s Size is %#"PRIx64", data = %#"PRIxPTR", id %#"PRIx64"\n", attach_filename, (uint64_t)attach->data.size, attach->data.data, attach->i_id));
DEBUG_INFO(("Attachment %s Size is %#zx, data = %p, id %#" PRIx64 "\n", attach_filename, attach->data.size, (void*)attach->data.data, attach->i_id));

if (!attach->data.data) {
// make sure we can fetch data from the id
pst_index_ll *ptr = pst_getID(pst, attach->i_id);
if (!ptr) {
DEBUG_WARN(("Couldn't find i_id %#"PRIx64". Cannot save attachment to file\n", attach->i_id));
DEBUG_WARN(("Couldn't find i_id %#" PRIx64 ". Cannot save attachment to file\n", attach->i_id));
DEBUG_RET();
return;
}
Expand Down Expand Up @@ -1137,7 +1137,7 @@ void write_embedded_message(FILE* f_output, pst_item_attach* attach, char *bound
// a reference to an internal Outlook COM class.
// Log the skipped item and continue on.
if (!item) {
DEBUG_WARN(("write_embedded_message: pst_parse_item was unable to parse the embedded message in attachment ID %llu", attach->i_id));
DEBUG_WARN(("write_embedded_message: pst_parse_item was unable to parse the embedded message in attachment ID %" PRIu64, attach->i_id));
} else {
if (!item->email) {
DEBUG_WARN(("write_embedded_message: pst_parse_item returned type %d, not an email message", item->type));
Expand Down Expand Up @@ -1234,7 +1234,7 @@ char *rfc2231_string(char *inp) {
while (*y) {
if (!is_attribute_char(*x)) {
*(z++) = (uint8_t)'%';
snprintf(z, 3, "%2x", *y);
snprintf(z, 3, "%2" PRIx8, *y);
z += 2;
}
else {
Expand All @@ -1250,7 +1250,7 @@ char *rfc2231_string(char *inp) {
void write_inline_attachment(FILE* f_output, pst_item_attach* attach, char *boundary, pst_file* pst)
{
DEBUG_ENT("write_inline_attachment");
DEBUG_INFO(("Attachment Size is %#"PRIx64", data = %#"PRIxPTR", id %#"PRIx64"\n", (uint64_t)attach->data.size, attach->data.data, attach->i_id));
DEBUG_INFO(("Attachment Size is %#zx, data = %p, id %#" PRIx64 "\n", attach->data.size, (void*)attach->data.data, attach->i_id));

if (!attach->data.data) {
// make sure we can fetch data from the id
Expand Down Expand Up @@ -1500,7 +1500,7 @@ int test_base64(char *body, size_t len)
DEBUG_ENT("test_base64");
while (len--) {
if ((*b < 32) && (*b != 9) && (*b != 10)) {
DEBUG_INFO(("found base64 byte %d\n", (int)*b));
DEBUG_INFO(("found base64 byte %" PRId8 "\n", *b));
DEBUG_HEXDUMPC(body, strlen(body), 0x10);
b64 = 1;
break;
Expand Down Expand Up @@ -2228,7 +2228,7 @@ void write_appointment(FILE* f_output, pst_item* item)
pst_convert_utf8_null(item, &item->body);
pst_convert_utf8_null(item, &appointment->location);

fprintf(f_output, "UID:%#"PRIx64"\n", item->block_id);
fprintf(f_output, "UID:%#" PRIx64 "\n", item->block_id);
fprintf(f_output, "DTSTAMP:%s\n", pst_rfc2445_datetime_format_now(sizeof(time_buffer), time_buffer));
if (item->create_date)
fprintf(f_output, "CREATED:%s\n", pst_rfc2445_datetime_format(item->create_date, sizeof(time_buffer), time_buffer));
Expand Down Expand Up @@ -2262,12 +2262,12 @@ void write_appointment(FILE* f_output, pst_item* item)
const char* days[] = {"SU", "MO", "TU", "WE", "TH", "FR", "SA"};
pst_recurrence *rdata = pst_convert_recurrence(appointment);
fprintf(f_output, "RRULE:FREQ=%s", rules[rdata->type]);
if (rdata->count) fprintf(f_output, ";COUNT=%u", rdata->count);
if (rdata->count) fprintf(f_output, ";COUNT=%" PRIu32, rdata->count);
if ((rdata->interval != 1) &&
(rdata->interval)) fprintf(f_output, ";INTERVAL=%u", rdata->interval);
if (rdata->dayofmonth) fprintf(f_output, ";BYMONTHDAY=%d", rdata->dayofmonth);
if (rdata->monthofyear) fprintf(f_output, ";BYMONTH=%d", rdata->monthofyear);
if (rdata->position) fprintf(f_output, ";BYSETPOS=%d", rdata->position);
(rdata->interval)) fprintf(f_output, ";INTERVAL=%" PRIu32, rdata->interval);
if (rdata->dayofmonth) fprintf(f_output, ";BYMONTHDAY=%" PRId32, rdata->dayofmonth);
if (rdata->monthofyear) fprintf(f_output, ";BYMONTH=%" PRId32, rdata->monthofyear);
if (rdata->position) fprintf(f_output, ";BYSETPOS=%" PRId32, rdata->position);
if (rdata->bydaymask) {
char byday[40];
int empty = 1;
Expand Down Expand Up @@ -2325,7 +2325,7 @@ void write_appointment(FILE* f_output, pst_item* item)
// ignore bogus alarms
if (appointment->alarm && (appointment->alarm_minutes >= 0) && (appointment->alarm_minutes < 1440)) {
fprintf(f_output, "BEGIN:VALARM\n");
fprintf(f_output, "TRIGGER:-PT%dM\n", appointment->alarm_minutes);
fprintf(f_output, "TRIGGER:-PT%" PRId32 "M\n", appointment->alarm_minutes);
fprintf(f_output, "ACTION:DISPLAY\n");
fprintf(f_output, "DESCRIPTION:Reminder\n");
fprintf(f_output, "END:VALARM\n");
Expand Down Expand Up @@ -2401,7 +2401,7 @@ void create_enter_dir(struct file_ll* f, pst_item *item)
while ((f->output[t] = fopen(temp, "r"))) {
DEBUG_INFO(("need to increase filename because one already exists with that name\n"));
x++;
sprintf(temp, "%s%08d", f->name[t], x);
sprintf(temp, "%s%08" PRId32, f->name[t], x);
DEBUG_INFO(("- bump file name and try \"%s\"\n", temp));
if (x == 99999999) {
DIE(("create_enter_dir: Why can I not create a folder %s? I have tried %i extensions...\n", f->name[t], x));
Expand Down Expand Up @@ -2434,7 +2434,7 @@ void close_enter_dir(struct file_ll *f)
f->dname, f->item_count, f->skip_count, f->stored_count));
if (output_mode != OUTPUT_QUIET) {
pst_debug_lock();
printf("\t\"%s\" - %i items done, %i items skipped.\n", f->dname, f->item_count, f->skip_count);
printf("\t\"%s\" - %" PRIi32 " items done, %" PRIi32 " items skipped.\n", f->dname, f->item_count, f->skip_count);
fflush(stdout);
pst_debug_unlock();
}
Expand Down Expand Up @@ -2462,7 +2462,7 @@ void close_enter_dir(struct file_ll *f)
else if (mode == MODE_RECURSE) {
if (mode_thunder) {
FILE *type_file = fopen(".size", "w");
fprintf(type_file, "%i %i\n", f->item_count, f->stored_count);
fprintf(type_file, "%" PRIi32 " %" PRIi32 "\n", f->item_count, f->stored_count);
fclose(type_file);
}
close_recurse_dir();
Expand Down

0 comments on commit ea800b1

Please sign in to comment.