Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
git2
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
lvzhengyang
git2
Commits
f1ad004c
Unverified
Commit
f1ad004c
authored
Feb 18, 2018
by
Edward Thomson
Committed by
GitHub
Feb 18, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #4529 from libgit2/ethomson/index_add_requires_files
git_index_add_frombuffer: only accept files/links
parents
574671ba
5f774dbf
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
70 additions
and
5 deletions
+70
-5
src/index.c
+9
-5
tests/index/filemodes.c
+61
-0
No files found.
src/index.c
View file @
f1ad004c
...
...
@@ -1396,12 +1396,16 @@ static int index_conflict_to_reuc(git_index *index, const char *path)
return
ret
;
}
static
bool
valid_filemode
(
const
int
filemode
)
GIT_INLINE
(
bool
)
is_file_or_link
(
const
int
filemode
)
{
return
(
filemode
==
GIT_FILEMODE_BLOB
||
filemode
==
GIT_FILEMODE_BLOB_EXECUTABLE
||
filemode
==
GIT_FILEMODE_LINK
||
filemode
==
GIT_FILEMODE_COMMIT
);
filemode
==
GIT_FILEMODE_LINK
);
}
GIT_INLINE
(
bool
)
valid_filemode
(
const
int
filemode
)
{
return
(
is_file_or_link
(
filemode
)
||
filemode
==
GIT_FILEMODE_COMMIT
);
}
int
git_index_add_frombuffer
(
...
...
@@ -1419,7 +1423,7 @@ int git_index_add_frombuffer(
"could not initialize index entry. "
"Index is not backed up by an existing repository."
);
if
(
!
valid_filemode
(
source_entry
->
mode
))
{
if
(
!
is_file_or_link
(
source_entry
->
mode
))
{
giterr_set
(
GITERR_INDEX
,
"invalid filemode"
);
return
-
1
;
}
...
...
@@ -1605,7 +1609,7 @@ int git_index_add(git_index *index, const git_index_entry *source_entry)
assert
(
index
&&
source_entry
&&
source_entry
->
path
);
if
(
!
valid_filemode
(
source_entry
->
mode
))
{
giterr_set
(
GITERR_INDEX
,
"invalid
file
mode"
);
giterr_set
(
GITERR_INDEX
,
"invalid
entry
mode"
);
return
-
1
;
}
...
...
tests/index/filemodes.c
View file @
f1ad004c
...
...
@@ -256,3 +256,64 @@ void test_index_filemodes__invalid(void)
git_index_free
(
index
);
}
void
test_index_filemodes__frombuffer_requires_files
(
void
)
{
git_index
*
index
;
git_index_entry
new_entry
;
const
git_index_entry
*
ret_entry
;
const
char
*
content
=
"hey there
\n
"
;
cl_git_pass
(
git_repository_index
(
&
index
,
g_repo
));
/* regular blob */
new_entry
.
path
=
"dummy-file.txt"
;
new_entry
.
mode
=
GIT_FILEMODE_BLOB
;
cl_git_pass
(
git_index_add_frombuffer
(
index
,
&
new_entry
,
content
,
strlen
(
content
)));
cl_assert
((
ret_entry
=
git_index_get_bypath
(
index
,
"dummy-file.txt"
,
0
)));
cl_assert_equal_s
(
"dummy-file.txt"
,
ret_entry
->
path
);
cl_assert_equal_i
(
GIT_FILEMODE_BLOB
,
ret_entry
->
mode
);
/* executable blob */
new_entry
.
path
=
"dummy-file.txt"
;
new_entry
.
mode
=
GIT_FILEMODE_BLOB_EXECUTABLE
;
cl_git_pass
(
git_index_add_frombuffer
(
index
,
&
new_entry
,
content
,
strlen
(
content
)));
cl_assert
((
ret_entry
=
git_index_get_bypath
(
index
,
"dummy-file.txt"
,
0
)));
cl_assert_equal_s
(
"dummy-file.txt"
,
ret_entry
->
path
);
cl_assert_equal_i
(
GIT_FILEMODE_BLOB_EXECUTABLE
,
ret_entry
->
mode
);
/* links are also acceptable */
new_entry
.
path
=
"dummy-link.txt"
;
new_entry
.
mode
=
GIT_FILEMODE_LINK
;
cl_git_pass
(
git_index_add_frombuffer
(
index
,
&
new_entry
,
content
,
strlen
(
content
)));
cl_assert
((
ret_entry
=
git_index_get_bypath
(
index
,
"dummy-link.txt"
,
0
)));
cl_assert_equal_s
(
"dummy-link.txt"
,
ret_entry
->
path
);
cl_assert_equal_i
(
GIT_FILEMODE_LINK
,
ret_entry
->
mode
);
/* trees are rejected */
new_entry
.
path
=
"invalid_mode.txt"
;
new_entry
.
mode
=
GIT_FILEMODE_TREE
;
cl_git_fail
(
git_index_add_frombuffer
(
index
,
&
new_entry
,
content
,
strlen
(
content
)));
cl_assert_equal_p
(
NULL
,
git_index_get_bypath
(
index
,
"invalid_mode.txt"
,
0
));
/* submodules are rejected */
new_entry
.
path
=
"invalid_mode.txt"
;
new_entry
.
mode
=
GIT_FILEMODE_COMMIT
;
cl_git_fail
(
git_index_add_frombuffer
(
index
,
&
new_entry
,
content
,
strlen
(
content
)));
cl_assert_equal_p
(
NULL
,
git_index_get_bypath
(
index
,
"invalid_mode.txt"
,
0
));
git_index_free
(
index
);
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment