icoutils/0009-common-strbuf.c-Use-size_t-and-ssize_t.patch
Richard W.M. Jones 86a1ed311e Add a series of upstream patches to enable compiler warnings and
fix multiple issues.

Revert one of the checks which breaks processing of PE binaries.

Removed the 'Group' line, not needed with modern Fedora/RPM.
2017-03-10 12:18:04 +00:00

412 lines
15 KiB
Diff

From edb7fc727a29f4ae8dcd6fee02fb5c08286808c5 Mon Sep 17 00:00:00 2001
From: "Richard W.M. Jones" <rjones@redhat.com>
Date: Thu, 9 Mar 2017 13:25:33 +0000
Subject: [PATCH 09/26] common/strbuf.c: Use size_t and ssize_t.
On 64 bit machines, strings can be longer than 2^32 characters.
size_t or ssize_t should be used to handle all offsets and lengths of
such strings to avoid overflow.
This makes the fairly mechanical change int32_t -> ssize_t and
uint32_t -> size_t.
Signed-off-by: Richard W.M. Jones <rjones@redhat.com>
---
common/strbuf.c | 80 ++++++++++++++++++++++++++++-----------------------------
common/strbuf.h | 50 ++++++++++++++++++------------------
2 files changed, 65 insertions(+), 65 deletions(-)
diff --git a/common/strbuf.c b/common/strbuf.c
index 5c42873..cf90d5a 100644
--- a/common/strbuf.c
+++ b/common/strbuf.c
@@ -36,12 +36,12 @@
#define DEFAULT_STRBUF_CAPACITY 16
-#define SWAP_INT32(a,b) { int32_t _t = (a); (a) = (b); (b) = _t; }
+#define SWAP_SSIZE_T(a,b) { ssize_t _t = (a); (a) = (b); (b) = _t; }
-static int32_t
-normalize_strbuf_pos(StrBuf *sb, int32_t pos)
+static ssize_t
+normalize_strbuf_pos(StrBuf *sb, ssize_t pos)
{
- if (pos >= sb->len)
+ if (pos >= (ssize_t) sb->len)
return sb->len;
if (pos >= 0)
return pos;
@@ -51,8 +51,8 @@ normalize_strbuf_pos(StrBuf *sb, int32_t pos)
return 0;
}
-static int32_t
-normalize_str_pos(const char *str, int32_t pos)
+static ssize_t
+normalize_str_pos(const char *str, ssize_t pos)
{
if (str == NULL)
return 0;
@@ -70,13 +70,13 @@ strbuf_buffer(StrBuf *sb)
return sb->buf;
}
-uint32_t
+size_t
strbuf_length(StrBuf *sb)
{
return sb->len;
}
-uint32_t
+size_t
strbuf_capacity(StrBuf *sb)
{
return sb->capacity;
@@ -89,7 +89,7 @@ strbuf_new(void)
}
StrBuf *
-strbuf_new_with_capacity(uint32_t capacity)
+strbuf_new_with_capacity(size_t capacity)
{
StrBuf *sb;
sb = xmalloc(sizeof(StrBuf));
@@ -102,24 +102,24 @@ strbuf_new_with_capacity(uint32_t capacity)
}
StrBuf *
-strbuf_new_from_char_n(uint32_t times, char ch)
+strbuf_new_from_char_n(size_t times, char ch)
{
return strbuf_new_from_substring_n(times, &ch, 0, 1);
}
StrBuf *
-strbuf_new_from_substring_n(uint32_t times, const char *substr, int32_t subsp, int32_t subep)
+strbuf_new_from_substring_n(size_t times, const char *substr, ssize_t subsp, ssize_t subep)
{
subsp = normalize_str_pos(substr, subsp);
subep = normalize_str_pos(substr, subep);
if (subsp > subep)
- SWAP_INT32(subsp, subep);
+ SWAP_SSIZE_T(subsp, subep);
return strbuf_new_from_data_n(times, substr+subsp, subep-subsp);
}
StrBuf *
-strbuf_new_from_data_n(uint32_t times, const void *mem, uint32_t len)
+strbuf_new_from_data_n(size_t times, const void *mem, size_t len)
{
StrBuf *sb;
@@ -133,7 +133,7 @@ strbuf_new_from_data_n(uint32_t times, const void *mem, uint32_t len)
}
StrBuf *
-strbuf_newf_n(uint32_t times, const char *fmt, ...)
+strbuf_newf_n(size_t times, const char *fmt, ...)
{
va_list ap;
StrBuf *buf;
@@ -146,7 +146,7 @@ strbuf_newf_n(uint32_t times, const char *fmt, ...)
}
StrBuf *
-strbuf_vnewf_n(uint32_t times, const char *fmt, va_list ap)
+strbuf_vnewf_n(size_t times, const char *fmt, va_list ap)
{
char *str;
int len;
@@ -171,7 +171,7 @@ strbuf_free(StrBuf *sb)
}
char *
-strbuf_free_to_substring(StrBuf *sb, int32_t sp, int32_t ep)
+strbuf_free_to_substring(StrBuf *sb, ssize_t sp, ssize_t ep)
{
char *buf;
@@ -182,7 +182,7 @@ strbuf_free_to_substring(StrBuf *sb, int32_t sp, int32_t ep)
sb->buf[ep-sp] = '\0';
/* Call realloc so that unused memory can be used for other purpose. */
- if (sp == 0 && ep == sb->len)
+ if (sp == 0 && ep == (ssize_t) sb->len)
buf = sb->buf;
else
buf = xrealloc(sb->buf, ep-sp+1);
@@ -191,21 +191,21 @@ strbuf_free_to_substring(StrBuf *sb, int32_t sp, int32_t ep)
}
void
-strbuf_replace_char_n(StrBuf *sb, int32_t sp, int32_t ep, uint32_t times, char ch)
+strbuf_replace_char_n(StrBuf *sb, ssize_t sp, ssize_t ep, size_t times, char ch)
{
strbuf_replace_substring_n(sb, sp, ep, times, &ch, 0, 1);
}
void
-strbuf_replace_data_n(StrBuf *sb, int32_t sp, int32_t ep, uint32_t times, const void *mem, uint32_t len)
+strbuf_replace_data_n(StrBuf *sb, ssize_t sp, ssize_t ep, size_t times, const void *mem, size_t len)
{
- uint32_t addlen;
- uint32_t dellen;
+ size_t addlen;
+ size_t dellen;
sp = normalize_strbuf_pos(sb, sp);
ep = normalize_strbuf_pos(sb, ep);
if (sp > ep)
- SWAP_INT32(sp, ep);
+ SWAP_SSIZE_T(sp, ep);
addlen = len * times;
dellen = ep-sp;
@@ -223,18 +223,18 @@ strbuf_replace_data_n(StrBuf *sb, int32_t sp, int32_t ep, uint32_t times, const
}
void
-strbuf_replace_substring_n(StrBuf *sb, int32_t sp, int32_t ep, uint32_t times, const char *substr, int32_t subsp, int32_t subep)
+strbuf_replace_substring_n(StrBuf *sb, ssize_t sp, ssize_t ep, size_t times, const char *substr, ssize_t subsp, ssize_t subep)
{
subsp = normalize_str_pos(substr, subsp);
subep = normalize_str_pos(substr, subep);
if (subsp > subep)
- SWAP_INT32(subsp, subep);
+ SWAP_SSIZE_T(subsp, subep);
strbuf_replace_data_n(sb, sp, ep, times, substr+subsp, subep-subsp);
}
int
-strbuf_replacef_n(StrBuf *sb, int32_t sp, int32_t ep, uint32_t times, const char *fmt, ...)
+strbuf_replacef_n(StrBuf *sb, ssize_t sp, ssize_t ep, size_t times, const char *fmt, ...)
{
va_list ap;
int len;
@@ -247,7 +247,7 @@ strbuf_replacef_n(StrBuf *sb, int32_t sp, int32_t ep, uint32_t times, const char
}
int
-strbuf_vreplacef_n(StrBuf *sb, int32_t sp, int32_t ep, uint32_t times, const char *fmt, va_list ap)
+strbuf_vreplacef_n(StrBuf *sb, ssize_t sp, ssize_t ep, size_t times, const char *fmt, va_list ap)
{
char *str;
int len;
@@ -255,7 +255,7 @@ strbuf_vreplacef_n(StrBuf *sb, int32_t sp, int32_t ep, uint32_t times, const cha
sp = normalize_strbuf_pos(sb, sp);
ep = normalize_strbuf_pos(sb, ep);
if (sp > ep)
- SWAP_INT32(sp, ep);
+ SWAP_SSIZE_T(sp, ep);
len = vasprintf(&str, fmt, ap);
if (len < 0)
@@ -267,29 +267,29 @@ strbuf_vreplacef_n(StrBuf *sb, int32_t sp, int32_t ep, uint32_t times, const cha
}
void
-strbuf_reverse_substring(StrBuf *sb, int32_t sp, int32_t ep)
+strbuf_reverse_substring(StrBuf *sb, ssize_t sp, ssize_t ep)
{
sp = normalize_strbuf_pos(sb, sp);
ep = normalize_strbuf_pos(sb, ep);
while (sp < ep) {
- SWAP_INT32(sb->buf[sp], sb->buf[ep]);
+ SWAP_SSIZE_T(sb->buf[sp], sb->buf[ep]);
sp++;
ep--;
}
}
void
-strbuf_repeat_substring(StrBuf *sb, int32_t sp, int32_t ep, uint32_t times)
+strbuf_repeat_substring(StrBuf *sb, ssize_t sp, ssize_t ep, size_t times)
{
- int32_t addlen;
+ ssize_t addlen;
sp = normalize_strbuf_pos(sb, sp);
ep = normalize_strbuf_pos(sb, ep);
addlen = (ep-sp) * (times - 1);
if (addlen != 0) {
- uint32_t p;
+ size_t p;
strbuf_ensure_capacity(sb, sb->len+1+addlen);
memmove(sb->buf+sp+addlen, sb->buf+ep, sb->len+1-ep);
@@ -304,7 +304,7 @@ strbuf_repeat_substring(StrBuf *sb, int32_t sp, int32_t ep, uint32_t times)
}
void
-strbuf_set_length(StrBuf *sb, uint32_t new_length)
+strbuf_set_length(StrBuf *sb, size_t new_length)
{
strbuf_ensure_capacity(sb, new_length+1);
sb->buf[new_length] = '\0';
@@ -313,7 +313,7 @@ strbuf_set_length(StrBuf *sb, uint32_t new_length)
/* Note: The terminating null-byte counts as 1 in min_capacity */
void
-strbuf_ensure_capacity(StrBuf *sb, uint32_t min_capacity)
+strbuf_ensure_capacity(StrBuf *sb, size_t min_capacity)
{
if (min_capacity > sb->capacity) {
sb->capacity = MAX(min_capacity, sb->len*2+2); /* MAX -> max */
@@ -324,14 +324,14 @@ strbuf_ensure_capacity(StrBuf *sb, uint32_t min_capacity)
}
char *
-strbuf_substring(StrBuf *sb, int32_t sp, int32_t ep)
+strbuf_substring(StrBuf *sb, ssize_t sp, ssize_t ep)
{
char *str;
sp = normalize_strbuf_pos(sb, sp);
ep = normalize_strbuf_pos(sb, ep);
if (sp > ep)
- SWAP_INT32(sp, ep);
+ SWAP_SSIZE_T(sp, ep);
str = xmalloc((ep-sp+1) * sizeof(char));
memcpy(str, sb->buf+sp, (ep-sp+1) * sizeof(char));
@@ -341,14 +341,14 @@ strbuf_substring(StrBuf *sb, int32_t sp, int32_t ep)
}
char
-strbuf_char_at(StrBuf *sb, int32_t sp)
+strbuf_char_at(StrBuf *sb, ssize_t sp)
{
return sb->buf[normalize_strbuf_pos(sb, sp)];
}
#if 0
char
-strbuf_set_char_at(StrBuf *sb, int32_t sp, char chr)
+strbuf_set_char_at(StrBuf *sb, ssize_t sp, char chr)
{
char old;
@@ -364,13 +364,13 @@ strbuf_set_char_at(StrBuf *sb, int32_t sp, char chr)
}
void
-strbuf_replace_strbuf(StrBuf *sb, int32_t sp, int32_t ep, StrBuf *strbuf)
+strbuf_replace_strbuf(StrBuf *sb, ssize_t sp, ssize_t ep, StrBuf *strbuf)
{
strbuf_replace_data_n(sb,sp,ep,1,strbuf->buf,strbuf->len);
}
char
-strbuf_delete_char(StrBuf *sb, int32_t sp)
+strbuf_delete_char(StrBuf *sb, ssize_t sp)
{
}
diff --git a/common/strbuf.h b/common/strbuf.h
index 67a523d..6cd0d58 100644
--- a/common/strbuf.h
+++ b/common/strbuf.h
@@ -28,16 +28,16 @@ typedef struct _StrBuf StrBuf;
struct _StrBuf {
char *buf;
- uint32_t len;
- uint32_t capacity;
+ size_t len;
+ size_t capacity;
};
void strbuf_free(StrBuf *sb);
#define strbuf_free_to_string(sb) strbuf_free_to_substring(sb,0,-1)
-char *strbuf_free_to_substring(StrBuf *sb, int32_t sp, int32_t ep);
+char *strbuf_free_to_substring(StrBuf *sb, ssize_t sp, ssize_t ep);
StrBuf *strbuf_new(void);
-StrBuf *strbuf_new_with_capacity(uint32_t capacity);
+StrBuf *strbuf_new_with_capacity(size_t capacity);
#define strbuf_new_from_char(chr) strbuf_new_from_char_n(1,chr)
#define strbuf_new_from_string(str) strbuf_new_from_substring(str,0,-1)
@@ -46,12 +46,12 @@ StrBuf *strbuf_new_with_capacity(uint32_t capacity);
#define strbuf_new_from_substring(str,ssp,sep) strbuf_new_from_substring_n(1,str,ssp,sep)
#define strbuf_newf(fmt...) strbuf_newf_n(1,fmt)
#define strbuf_vnewf(fmt,ap) strbuf_vnewf_n(1,fmt,ap)
-StrBuf *strbuf_new_from_char_n(uint32_t times, char ch);
+StrBuf *strbuf_new_from_char_n(size_t times, char ch);
#define strbuf_new_from_string_n(n,str) strbuf_new_from_substring_n(n,str,0,-1)
-StrBuf *strbuf_new_from_substring_n(uint32_t times, const char *str, int32_t sp, int32_t ep);
-StrBuf *strbuf_new_from_data_n(uint32_t times, const void *mem, uint32_t len);
-StrBuf *strbuf_newf_n(uint32_t times, const char *fmt, ...) __attribute__ ((format (printf, 2, 3)));
-StrBuf *strbuf_vnewf_n(uint32_t times, const char *fmt, va_list ap) __attribute__ ((format (printf, 2, 0)));
+StrBuf *strbuf_new_from_substring_n(size_t times, const char *str, ssize_t sp, ssize_t ep);
+StrBuf *strbuf_new_from_data_n(size_t times, const void *mem, size_t len);
+StrBuf *strbuf_newf_n(size_t times, const char *fmt, ...) __attribute__ ((format (printf, 2, 3)));
+StrBuf *strbuf_vnewf_n(size_t times, const char *fmt, va_list ap) __attribute__ ((format (printf, 2, 0)));
#define strbuf_append_char(sb,chr) strbuf_append_char_n(sb,1,chr)
#define strbuf_append(sb,str) strbuf_append_n(sb,1,str)
@@ -116,38 +116,38 @@ StrBuf *strbuf_vnewf_n(uint32_t times, const char *fmt, va_list ap) __attribute_
#define strbuf_replace_char(sb,sp,ep,ch) strbuf_replace_char_n(sb,sp,ep,1,ch)
#define strbuf_replace(sb,sp,ep,str) strbuf_replace_n(sb,sp,ep,1,str)
#define strbuf_replace_data(sb,sp,ep,mem,len) strbuf_replace_data_n(sb,sp,ep,1,mem,len)
-void strbuf_replace_strbuf(StrBuf *sb, int32_t sp, int32_t ep, StrBuf *strbuf); /* or strbuf_replace_data_n(sb,sp,ep,1,strbuf_buffer(strbuf),strbuf_length(strbuf))*/
+void strbuf_replace_strbuf(StrBuf *sb, ssize_t sp, ssize_t ep, StrBuf *strbuf); /* or strbuf_replace_data_n(sb,sp,ep,1,strbuf_buffer(strbuf),strbuf_length(strbuf))*/
#define strbuf_replace_substring(sb,sp,ep,str,ssp,sep) strbuf_replace_substring_n(sb,sp,ep,1,str,ssp,sep)
#define strbuf_replacef(sb,sp,ep,fmt...) strbuf_replacef_n(sb,sp,ep,1,fmt)
#define strbuf_vreplacef(sb,sp,ep,fmt,ap) strbuf_vreplacef_n(sb,sp,ep,1,fmt,ap)
-void strbuf_replace_char_n(StrBuf *sb, int32_t sp, int32_t ep, uint32_t times, char ch);
+void strbuf_replace_char_n(StrBuf *sb, ssize_t sp, ssize_t ep, size_t times, char ch);
#define strbuf_replace_n(sb,sp,ep,n,str) strbuf_replace_substring_n(sb,sp,ep,n,str,0,-1)
-void strbuf_replace_substring_n(StrBuf *sb, int32_t sp, int32_t ep, uint32_t times, const char *str, int32_t ssp, int32_t sep);
-void strbuf_replace_data_n(StrBuf *sb, int32_t sp, int32_t ep, uint32_t times, const void *mem, uint32_t len);
-int strbuf_replacef_n(StrBuf *sb, int32_t sp, int32_t ep, uint32_t times, const char *fmt, ...) __attribute__ ((format (printf, 5, 6)));
-int strbuf_vreplacef_n(StrBuf *sb, int32_t sp, int32_t ep, uint32_t times, const char *fmt, va_list ap) __attribute__ ((format (printf, 5, 0)));
+void strbuf_replace_substring_n(StrBuf *sb, ssize_t sp, ssize_t ep, size_t times, const char *str, ssize_t ssp, ssize_t sep);
+void strbuf_replace_data_n(StrBuf *sb, ssize_t sp, ssize_t ep, size_t times, const void *mem, size_t len);
+int strbuf_replacef_n(StrBuf *sb, ssize_t sp, ssize_t ep, size_t times, const char *fmt, ...) __attribute__ ((format (printf, 5, 6)));
+int strbuf_vreplacef_n(StrBuf *sb, ssize_t sp, ssize_t ep, size_t times, const char *fmt, va_list ap) __attribute__ ((format (printf, 5, 0)));
-char strbuf_set_char_at(StrBuf *sb, int32_t sp, char chr); /* or strbuf_replace_char(sb,sp,strbuf_next_pos(sb,sp),chr) */
-char strbuf_delete_char(StrBuf *sb, int32_t sp); /* or strbuf_replace(sb,sp,strbuf_next_pos(sb,sp),NULL) */
+char strbuf_set_char_at(StrBuf *sb, ssize_t sp, char chr); /* or strbuf_replace_char(sb,sp,strbuf_next_pos(sb,sp),chr) */
+char strbuf_delete_char(StrBuf *sb, ssize_t sp); /* or strbuf_replace(sb,sp,strbuf_next_pos(sb,sp),NULL) */
#define strbuf_delete(sb,sp,ep) strbuf_replace(sb,sp,ep,NULL)
#define strbuf_clear(sb) strbuf_replace(sb,0,-1,NULL)
-void strbuf_reverse_substring(StrBuf *sb, int32_t sp, int32_t ep);
+void strbuf_reverse_substring(StrBuf *sb, ssize_t sp, ssize_t ep);
#define strbuf_reverse(sb) strbuf_reverse_substring(sb,0,-1)
-void strbuf_repeat_substring(StrBuf *sb, int32_t sp, int32_t ep, uint32_t times);
+void strbuf_repeat_substring(StrBuf *sb, ssize_t sp, ssize_t ep, size_t times);
#define strbuf_repeat(sb,n) strbuf_repeat_substring(sb,0,-1,n)
-uint32_t strbuf_length(StrBuf *sb);
-uint32_t strbuf_capacity(StrBuf *sb);
+size_t strbuf_length(StrBuf *sb);
+size_t strbuf_capacity(StrBuf *sb);
char *strbuf_buffer(StrBuf *sb);
#define strbuf_is_empty(sb) (strbuf_length(sb) == 0)
-void strbuf_set_length(StrBuf *sb, uint32_t new_length);
-void strbuf_ensure_capacity(StrBuf *sb, uint32_t minimum_capacity); /* minimum_capacity should account for null-byte */
+void strbuf_set_length(StrBuf *sb, size_t new_length);
+void strbuf_ensure_capacity(StrBuf *sb, size_t minimum_capacity); /* minimum_capacity should account for null-byte */
-char *strbuf_substring(StrBuf *sb, int32_t sp, int32_t ep);
+char *strbuf_substring(StrBuf *sb, ssize_t sp, ssize_t ep);
#define strbuf_to_string(sb) strbuf_substring(sb,0,-1)
-char strbuf_char_at(StrBuf *sb, int32_t index);
+char strbuf_char_at(StrBuf *sb, ssize_t index);
#endif
--
2.10.2