Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5663d4f

Browse files
committed
Merge pull request libgit2#3613 from ethomson/fixups
Remove most of the silly warnings
2 parents 594a5d1 + 9ce0399 commit 5663d4f

File tree

16 files changed

+114
-48
lines changed

16 files changed

+114
-48
lines changed

src/index.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -603,14 +603,14 @@ const git_oid *git_index_checksum(git_index *index)
603603
*/
604604
static int compare_checksum(git_index *index)
605605
{
606-
int fd, error;
606+
int fd;
607607
ssize_t bytes_read;
608608
git_oid checksum = {{ 0 }};
609609

610610
if ((fd = p_open(index->index_file_path, O_RDONLY)) < 0)
611611
return fd;
612612

613-
if ((error = p_lseek(fd, -20, SEEK_END)) < 0) {
613+
if (p_lseek(fd, -20, SEEK_END) < 0) {
614614
p_close(fd);
615615
giterr_set(GITERR_OS, "failed to seek to end of file");
616616
return -1;
@@ -826,8 +826,8 @@ const git_index_entry *git_index_get_bypath(
826826
void git_index_entry__init_from_stat(
827827
git_index_entry *entry, struct stat *st, bool trust_mode)
828828
{
829-
entry->ctime.seconds = (git_time_t)st->st_ctime;
830-
entry->mtime.seconds = (git_time_t)st->st_mtime;
829+
entry->ctime.seconds = (int32_t)st->st_ctime;
830+
entry->mtime.seconds = (int32_t)st->st_mtime;
831831
#if defined(GIT_USE_NSEC)
832832
entry->mtime.nanoseconds = st->st_mtim.tv_nsec;
833833
entry->ctime.nanoseconds = st->st_ctim.tv_nsec;
@@ -838,7 +838,7 @@ void git_index_entry__init_from_stat(
838838
git_index__create_mode(0666) : git_index__create_mode(st->st_mode);
839839
entry->uid = st->st_uid;
840840
entry->gid = st->st_gid;
841-
entry->file_size = st->st_size;
841+
entry->file_size = (uint32_t)st->st_size;
842842
}
843843

844844
static void index_entry_adjust_namemask(
@@ -1529,7 +1529,7 @@ int git_index__fill(git_index *index, const git_vector *source_entries)
15291529
return 0;
15301530

15311531
git_vector_size_hint(&index->entries, source_entries->length);
1532-
git_idxmap_resize(index->entries_map, source_entries->length * 1.3);
1532+
git_idxmap_resize(index->entries_map, (khint_t)(source_entries->length * 1.3));
15331533

15341534
git_vector_foreach(source_entries, i, source_entry) {
15351535
git_index_entry *entry = NULL;

src/submodule.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -778,9 +778,9 @@ int git_submodule_add_to_index(git_submodule *sm, int write_index)
778778
if ((error = git_commit_lookup(&head, sm_repo, &sm->wd_oid)) < 0)
779779
goto cleanup;
780780

781-
entry.ctime.seconds = git_commit_time(head);
781+
entry.ctime.seconds = (int32_t)git_commit_time(head);
782782
entry.ctime.nanoseconds = 0;
783-
entry.mtime.seconds = git_commit_time(head);
783+
entry.mtime.seconds = (int32_t)git_commit_time(head);
784784
entry.mtime.nanoseconds = 0;
785785

786786
git_commit_free(head);

src/transports/winhttp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ static int winhttp_stream_connect(winhttp_stream *s)
283283
unsigned long disable_redirects = WINHTTP_DISABLE_REDIRECTS;
284284
int default_timeout = TIMEOUT_INFINITE;
285285
int default_connect_timeout = DEFAULT_CONNECT_TIMEOUT;
286-
int i;
286+
size_t i;
287287

288288
/* Prepare URL */
289289
git_buf_printf(&buf, "%s%s", t->connection_data.path, s->service_url);

src/tree.c

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
#define DEFAULT_TREE_SIZE 16
1818
#define MAX_FILEMODE_BYTES 6
1919

20+
#define TREE_ENTRY_CHECK_NAMELEN(n) \
21+
if (n > UINT16_MAX) { giterr_set(GITERR_INVALID, "tree entry path too long"); }
22+
2023
GIT__USE_STRMAP
2124

2225
static bool valid_filemode(const int filemode)
@@ -89,10 +92,7 @@ static git_tree_entry *alloc_entry_base(git_pool *pool, const char *filename, si
8992
git_tree_entry *entry = NULL;
9093
size_t tree_len;
9194

92-
if (filename_len > UINT16_MAX) {
93-
giterr_set(GITERR_INVALID, "tree entry is over UINT16_MAX in length");
94-
return NULL;
95-
}
95+
TREE_ENTRY_CHECK_NAMELEN(filename_len);
9696

9797
if (GIT_ADD_SIZET_OVERFLOW(&tree_len, sizeof(git_tree_entry), filename_len) ||
9898
GIT_ADD_SIZET_OVERFLOW(&tree_len, tree_len, 1))
@@ -106,7 +106,7 @@ static git_tree_entry *alloc_entry_base(git_pool *pool, const char *filename, si
106106
memset(entry, 0x0, sizeof(git_tree_entry));
107107
memcpy(entry->filename, filename, filename_len);
108108
entry->filename[filename_len] = 0;
109-
entry->filename_len = filename_len;
109+
entry->filename_len = (uint16_t)filename_len;
110110

111111
return entry;
112112
}
@@ -143,8 +143,8 @@ static int homing_search_cmp(const void *key, const void *array_member)
143143
const struct tree_key_search *ksearch = key;
144144
const git_tree_entry *entry = array_member;
145145

146-
const size_t len1 = ksearch->filename_len;
147-
const size_t len2 = entry->filename_len;
146+
const uint16_t len1 = ksearch->filename_len;
147+
const uint16_t len2 = entry->filename_len;
148148

149149
return memcmp(
150150
ksearch->filename,
@@ -180,8 +180,10 @@ static int tree_key_search(
180180
const git_tree_entry *entry;
181181
size_t homing, i;
182182

183+
TREE_ENTRY_CHECK_NAMELEN(filename_len);
184+
183185
ksearch.filename = filename;
184-
ksearch.filename_len = filename_len;
186+
ksearch.filename_len = (uint16_t)filename_len;
185187

186188
/* Initial homing search; find an entry on the tree with
187189
* the same prefix as the filename we're looking for */
@@ -334,6 +336,7 @@ const git_tree_entry *git_tree_entry_byname(
334336
const git_tree *tree, const char *filename)
335337
{
336338
assert(tree && filename);
339+
337340
return entry_fromname(tree, filename, strlen(filename));
338341
}
339342

@@ -364,13 +367,16 @@ int git_tree__prefix_position(const git_tree *tree, const char *path)
364367
{
365368
const git_vector *entries = &tree->entries;
366369
struct tree_key_search ksearch;
367-
size_t at_pos;
370+
size_t at_pos, path_len;
368371

369372
if (!path)
370373
return 0;
371374

375+
path_len = strlen(path);
376+
TREE_ENTRY_CHECK_NAMELEN(path_len);
377+
372378
ksearch.filename = path;
373-
ksearch.filename_len = strlen(path);
379+
ksearch.filename_len = (uint16_t)path_len;
374380

375381
/* be safe when we cast away constness - i.e. don't trigger a sort */
376382
assert(git_vector_is_sorted(&tree->entries));

src/unix/posix.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,10 @@ extern char *p_realpath(const char *, char *);
5252
#define p_localtime_r(c, r) localtime_r(c, r)
5353
#define p_gmtime_r(c, r) gmtime_r(c, r)
5454

55+
#define p_timeval timeval
56+
5557
#ifdef HAVE_FUTIMENS
56-
GIT_INLINE(int) p_futimes(int f, const struct timeval t[2])
58+
GIT_INLINE(int) p_futimes(int f, const struct p_timeval t[2])
5759
{
5860
struct timespec s[2];
5961
s[0].tv_sec = t[0].tv_sec;

src/win32/posix.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,21 @@
99

1010
#include "common.h"
1111
#include "../posix.h"
12+
#include "win32-compat.h"
1213
#include "path_w32.h"
1314
#include "utf-conv.h"
1415
#include "dir.h"
1516

1617
typedef SOCKET GIT_SOCKET;
1718

1819
#define p_lseek(f,n,w) _lseeki64(f, n, w)
19-
#define p_fstat(f,b) _fstat64(f, b)
20+
21+
extern int p_fstat(int fd, struct stat *buf);
2022
extern int p_lstat(const char *file_name, struct stat *buf);
21-
extern int p_stat(const char* path, struct stat* buf);
23+
extern int p_stat(const char* path, struct stat *buf);
2224

23-
extern int p_utimes(const char *filename, const struct timeval times[2]);
24-
extern int p_futimes(int fd, const struct timeval times[2]);
25+
extern int p_utimes(const char *filename, const struct p_timeval times[2]);
26+
extern int p_futimes(int fd, const struct p_timeval times[2]);
2527

2628
extern int p_readlink(const char *path, char *buf, size_t bufsiz);
2729
extern int p_symlink(const char *old, const char *new);

src/win32/posix_w32.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ int p_lstat_posixly(const char *filename, struct stat *buf)
210210
return do_lstat(filename, buf, true);
211211
}
212212

213-
int p_utimes(const char *filename, const struct timeval times[2])
213+
int p_utimes(const char *filename, const struct p_timeval times[2])
214214
{
215215
int fd, error;
216216

@@ -223,7 +223,7 @@ int p_utimes(const char *filename, const struct timeval times[2])
223223
return error;
224224
}
225225

226-
int p_futimes(int fd, const struct timeval times[2])
226+
int p_futimes(int fd, const struct p_timeval times[2])
227227
{
228228
HANDLE handle;
229229
FILETIME atime = {0}, mtime = {0};
@@ -398,6 +398,22 @@ static int follow_and_lstat_link(git_win32_path path, struct stat* buf)
398398
return lstat_w(target_w, buf, false);
399399
}
400400

401+
int p_fstat(int fd, struct stat *buf)
402+
{
403+
BY_HANDLE_FILE_INFORMATION fhInfo;
404+
405+
HANDLE fh = (HANDLE)_get_osfhandle(fd);
406+
407+
if (fh == INVALID_HANDLE_VALUE ||
408+
!GetFileInformationByHandle(fh, &fhInfo)) {
409+
errno = EBADF;
410+
return -1;
411+
}
412+
413+
git_win32__file_information_to_stat(buf, &fhInfo);
414+
return 0;
415+
}
416+
401417
int p_stat(const char* path, struct stat* buf)
402418
{
403419
git_win32_path path_w;

src/win32/w32_util.h

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ GIT_INLINE(void) git_win32__filetime_to_timespec(
9696
}
9797

9898
GIT_INLINE(void) git_win32__timeval_to_filetime(
99-
FILETIME *ft, const struct timeval tv)
99+
FILETIME *ft, const struct p_timeval tv)
100100
{
101101
long long ticks = (tv.tv_sec * 10000000LL) +
102102
(tv.tv_usec * 10LL) + 116444736000000000LL;
@@ -105,32 +105,65 @@ GIT_INLINE(void) git_win32__timeval_to_filetime(
105105
ft->dwLowDateTime = (ticks & 0xffffffffLL);
106106
}
107107

108-
GIT_INLINE(int) git_win32__file_attribute_to_stat(
108+
GIT_INLINE(void) git_win32__stat_init(
109109
struct stat *st,
110-
const WIN32_FILE_ATTRIBUTE_DATA *attrdata,
111-
const wchar_t *path)
110+
DWORD dwFileAttributes,
111+
DWORD nFileSizeHigh,
112+
DWORD nFileSizeLow,
113+
FILETIME ftCreationTime,
114+
FILETIME ftLastAccessTime,
115+
FILETIME ftLastWriteTime)
112116
{
113117
mode_t mode = S_IREAD;
114118

115-
if (attrdata->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
119+
memset(st, 0, sizeof(struct stat));
120+
121+
if (dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
116122
mode |= S_IFDIR;
117123
else
118124
mode |= S_IFREG;
119125

120-
if ((attrdata->dwFileAttributes & FILE_ATTRIBUTE_READONLY) == 0)
126+
if ((dwFileAttributes & FILE_ATTRIBUTE_READONLY) == 0)
121127
mode |= S_IWRITE;
122128

123129
st->st_ino = 0;
124130
st->st_gid = 0;
125131
st->st_uid = 0;
126132
st->st_nlink = 1;
127133
st->st_mode = mode;
128-
st->st_size = ((git_off_t)attrdata->nFileSizeHigh << 32) + attrdata->nFileSizeLow;
134+
st->st_size = ((git_off_t)nFileSizeHigh << 32) + nFileSizeLow;
129135
st->st_dev = _getdrive() - 1;
130136
st->st_rdev = st->st_dev;
131-
git_win32__filetime_to_timespec(&(attrdata->ftLastAccessTime), &(st->st_atim));
132-
git_win32__filetime_to_timespec(&(attrdata->ftLastWriteTime), &(st->st_mtim));
133-
git_win32__filetime_to_timespec(&(attrdata->ftCreationTime), &(st->st_ctim));
137+
git_win32__filetime_to_timespec(&ftLastAccessTime, &(st->st_atim));
138+
git_win32__filetime_to_timespec(&ftLastWriteTime, &(st->st_mtim));
139+
git_win32__filetime_to_timespec(&ftCreationTime, &(st->st_ctim));
140+
}
141+
142+
GIT_INLINE(void) git_win32__file_information_to_stat(
143+
struct stat *st,
144+
const BY_HANDLE_FILE_INFORMATION *fileinfo)
145+
{
146+
git_win32__stat_init(st,
147+
fileinfo->dwFileAttributes,
148+
fileinfo->nFileSizeHigh,
149+
fileinfo->nFileSizeLow,
150+
fileinfo->ftCreationTime,
151+
fileinfo->ftLastAccessTime,
152+
fileinfo->ftLastWriteTime);
153+
}
154+
155+
GIT_INLINE(int) git_win32__file_attribute_to_stat(
156+
struct stat *st,
157+
const WIN32_FILE_ATTRIBUTE_DATA *attrdata,
158+
const wchar_t *path)
159+
{
160+
git_win32__stat_init(st,
161+
attrdata->dwFileAttributes,
162+
attrdata->nFileSizeHigh,
163+
attrdata->nFileSizeLow,
164+
attrdata->ftCreationTime,
165+
attrdata->ftLastAccessTime,
166+
attrdata->ftLastWriteTime);
134167

135168
if (attrdata->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT && path) {
136169
git_win32_path target;
@@ -139,7 +172,7 @@ GIT_INLINE(int) git_win32__file_attribute_to_stat(
139172
st->st_mode = (st->st_mode & ~S_IFMT) | S_IFLNK;
140173

141174
/* st_size gets the UTF-8 length of the target name, in bytes,
142-
* not counting the NULL terminator */
175+
* not counting the NULL terminator */
143176
if ((st->st_size = git__utf16_to_8(NULL, 0, target)) < 0) {
144177
giterr_set(GITERR_OS, "Could not convert reparse point name for '%s'", path);
145178
return -1;

src/win32/win32-compat.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313
#include <sys/stat.h>
1414
#include <sys/types.h>
1515

16+
typedef long suseconds_t;
17+
18+
struct p_timeval {
19+
time_t tv_sec;
20+
suseconds_t tv_usec;
21+
};
22+
1623
struct p_timespec {
1724
time_t tv_sec;
1825
long tv_nsec;

tests/checkout/checkout_helpers.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ int checkout_count_callback(
133133
void tick_index(git_index *index)
134134
{
135135
struct timespec ts;
136-
struct timeval times[2];
136+
struct p_timeval times[2];
137137

138138
cl_assert(index->on_disk);
139139
cl_assert(git_index_path(index));

tests/core/posix.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ void test_core_posix__inet_pton(void)
100100

101101
void test_core_posix__utimes(void)
102102
{
103-
struct timeval times[2];
103+
struct p_timeval times[2];
104104
struct stat st;
105105
time_t curtime;
106106
int fd;

tests/diff/workdir.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1755,7 +1755,7 @@ void test_diff_workdir__with_stale_index(void)
17551755
static int touch_file(void *payload, git_buf *path)
17561756
{
17571757
struct stat st;
1758-
struct timeval times[2];
1758+
struct p_timeval times[2];
17591759

17601760
GIT_UNUSED(payload);
17611761
if (git_path_isdir(path->ptr))
@@ -2006,7 +2006,7 @@ void test_diff_workdir__only_writes_index_when_necessary(void)
20062006
git_oid initial, first, second;
20072007
git_buf path = GIT_BUF_INIT;
20082008
struct stat st;
2009-
struct timeval times[2];
2009+
struct p_timeval times[2];
20102010

20112011
opts.flags |= GIT_DIFF_INCLUDE_UNTRACKED | GIT_DIFF_UPDATE_INDEX;
20122012

tests/index/racy.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ void test_index_racy__write_index_just_after_file(void)
5454
git_index *index;
5555
git_diff *diff;
5656
git_buf path = GIT_BUF_INIT;
57-
struct timeval times[2];
57+
struct p_timeval times[2];
5858

5959
/* Make sure we do have a timestamp */
6060
cl_git_pass(git_repository_index(&index, g_repo));

0 commit comments

Comments
 (0)