Prefer other types to int in misc.c

* src/misc.c (quote_copy_string, tar_savedir):
Use bool for booleans.  All uses changed.
(quote_copy_string): Use char for chars.
(unquote_string): Return void, since nobody uses return value.
(unquote_string): Check for overflow in escapes like \777.
(wdcache): Now array of idx_t not int, since in theory it
might contain values greater than INT_MAX.  All uses changed.
This commit is contained in:
Paul Eggert
2024-11-01 19:09:44 -07:00
parent 53a3691092
commit f96aff3ce9
4 changed files with 24 additions and 27 deletions

View File

@@ -644,13 +644,13 @@ void assign_string (char **dest, const char *src) ATTRIBUTE_NONNULL ((1, 2));
void assign_null (char **dest) ATTRIBUTE_NONNULL ((1));
void assign_string_n (char **string, const char *value, idx_t n);
#define ASSIGN_STRING_N(s,v) assign_string_n (s, v, sizeof (v))
int unquote_string (char *str);
void unquote_string (char *str);
char *zap_slashes (char *name);
char *normalize_filename (idx_t, char const *);
void normalize_filename_x (char *name);
void replace_prefix (char **pname, const char *samp, idx_t slen,
const char *repl, idx_t rlen);
char *tar_savedir (const char *name, int must_exist);
char *tar_savedir (const char *name, bool must_exist);
typedef struct namebuf *namebuf_t;
namebuf_t namebuf_create (const char *dir);
@@ -779,9 +779,9 @@ extern struct argp names_argp;
extern struct name *gnu_list_name;
void gid_to_gname (gid_t gid, char **gname);
int gname_to_gid (char const *gname, gid_t *pgid);
bool gname_to_gid (char const *gname, gid_t *pgid);
void uid_to_uname (uid_t uid, char **uname);
int uname_to_uid (char const *uname, uid_t *puid);
bool uname_to_uid (char const *uname, uid_t *puid);
void name_init (void);
void name_add_name (const char *name);

View File

@@ -1645,7 +1645,7 @@ try_purge_directory (char const *directory_name)
if (!is_dumpdir (&current_stat_info))
return false;
current_dir = tar_savedir (directory_name, 0);
current_dir = tar_savedir (directory_name, false);
if (!current_dir)
/* The directory doesn't exist now. It'll be created. In any

View File

@@ -104,11 +104,11 @@ quote_copy_string (const char *string)
const char *source = string;
char *destination = 0;
char *buffer = 0;
int copying = 0;
bool copying = false;
while (*source)
{
int character = *source++;
char character = *source++;
switch (character)
{
@@ -117,7 +117,7 @@ quote_copy_string (const char *string)
{
idx_t length = (source - string) - 1;
copying = 1;
copying = true;
buffer = xmalloc (length + 2 + 2 * strlen (source) + 1);
memcpy (buffer, string, length);
destination = buffer + length;
@@ -141,20 +141,18 @@ quote_copy_string (const char *string)
}
#endif
/* Takes a quoted C string (like those produced by quote_copy_string)
and turns it back into the un-quoted original. This is done in
place. Returns 0 only if the string was not properly quoted, but
completes the unquoting anyway.
/* Take a quoted C string (like those produced by quote_copy_string)
and turn it back into the un-quoted original, in place.
Complete the unquoting even if the string was not properly quoted.
This is used for reading the saved directory file in incremental
dumps. It is used for decoding old 'N' records (demangling names).
But also, it is used for decoding file arguments, would they come
from the shell or a -T file, and for decoding the --exclude
argument. */
int
void
unquote_string (char *string)
{
int result = 1;
char *source = string;
char *destination = string;
@@ -221,26 +219,24 @@ unquote_string (char *string)
case '6':
case '7':
{
int value = *source++ - '0';
unsigned char value = *source++ - '0';
if (*source < '0' || *source > '7')
{
*destination++ = value;
break;
}
value = value * 8 + *source++ - '0';
if (*source < '0' || *source > '7')
unsigned char val1 = value * 8 + (*source++ - '0'), val2;
if (*source < '0' || *source > '7' || ckd_mul (&val2, val1, 8))
{
*destination++ = value;
break;
}
value = value * 8 + *source++ - '0';
*destination++ = value;
*destination++ = val2 + (*source++ - '0');
break;
}
default:
result = 0;
*destination++ = '\\';
if (*source)
*destination++ = *source++;
@@ -253,7 +249,6 @@ unquote_string (char *string)
if (source != destination)
*destination = '\0';
return result;
}
/* Zap trailing slashes. */
@@ -707,7 +702,7 @@ remove_any_file (const char *file_name, enum remove_option option)
case RECURSIVE_REMOVE_OPTION:
{
char *directory = tar_savedir (file_name, 0);
char *directory = tar_savedir (file_name, false);
char const *entry;
idx_t entrylen;
@@ -937,7 +932,7 @@ enum { CHDIR_CACHE_SIZE = 16 };
/* Indexes into WD of chdir targets with open file descriptors, sorted
most-recently used first. Zero indexes are unused. */
static int wdcache[CHDIR_CACHE_SIZE];
static idx_t wdcache[CHDIR_CACHE_SIZE];
/* Number of nonzero entries in WDCACHE. */
static idx_t wdcache_count;
@@ -1035,10 +1030,10 @@ chdir_do (idx_t i)
/* Move the i value to the front of the cache. This is
O(CHDIR_CACHE_SIZE), but the cache is small. */
idx_t ci;
int prev = wdcache[0];
idx_t prev = wdcache[0];
for (ci = 1; prev != i; ci++)
{
int cur = wdcache[ci];
idx_t cur = wdcache[ci];
wdcache[ci] = prev;
if (cur == i)
break;
@@ -1300,7 +1295,7 @@ namebuf_finish (namebuf_t buf)
Return NULL on errors.
*/
char *
tar_savedir (const char *name, int must_exist)
tar_savedir (const char *name, bool must_exist)
{
char *ret = NULL;
DIR *dir = NULL;

View File

@@ -137,7 +137,9 @@ update_archive (void)
{
if (S_ISDIR (s.st_mode))
{
char *p, *dirp = tar_savedir (current_stat_info.file_name, 1);
char *p;
char *dirp = tar_savedir (current_stat_info.file_name,
true);
if (dirp)
{
namebuf_t nbuf = namebuf_create (current_stat_info.file_name);