chiark
/
gitweb
/
~mdw
/
tig
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
0001fc3
)
Improve string buffer copy safety
author
Jonas Fonseca
<fonseca@diku.dk>
Mon, 11 Sep 2006 01:12:15 +0000
(
03:12
+0200)
committer
Jonas Fonseca
<fonseca@antimatter.localdomain>
Mon, 11 Sep 2006 01:39:36 +0000
(
03:39
+0200)
tig.c
patch
|
blob
|
blame
|
history
diff --git
a/tig.c
b/tig.c
index d3702c4cb210e53429566ada6b7de1b49d3e5676..44fcb1b4f7a69db6054760c8fcc08dd47a410060 100644
(file)
--- a/
tig.c
+++ b/
tig.c
@@
-145,16
+145,22
@@
set_from_int_map(struct int_map *map, size_t map_size,
*/
static inline void
*/
static inline void
-string_ncopy
(char *dst, const char *src, size_t dst
len)
+string_ncopy
_do(char *dst, size_t dstlen, const char *src, size_t src
len)
{
{
- strncpy(dst, src, dstlen - 1);
-
dst[dstlen - 1] = 0
;
+ if (srclen > dstlen - 1)
+
srclen = dstlen - 1
;
+ strncpy(dst, src, srclen);
+ dst[srclen] = 0;
}
}
-/* Shorthand for safely copying into a fixed buffer. */
+/* Shorthands for safely copying into a fixed buffer. */
+
#define string_copy(dst, src) \
#define string_copy(dst, src) \
- string_ncopy(dst, src, sizeof(dst))
+ string_ncopy_do(dst, sizeof(dst), src, sizeof(dst))
+
+#define string_ncopy(dst, src, srclen) \
+ string_ncopy_do(dst, sizeof(dst), src, srclen)
static char *
chomp_string(char *name)
static char *
chomp_string(char *name)
@@
-2204,8
+2210,7
@@
pager_draw(struct view *view, struct line *line, unsigned int lineno)
string_copy(ref_commit, view->ref);
} else if (type == LINE_TREE_DIR || type == LINE_TREE_FILE) {
string_copy(ref_commit, view->ref);
} else if (type == LINE_TREE_DIR || type == LINE_TREE_FILE) {
- strncpy(view->ref, text + STRING_SIZE("100644 blob "), 41);
- view->ref[40] = 0;
+ string_ncopy(view->ref, text + STRING_SIZE("100644 blob "), 40);
string_copy(ref_blob, view->ref);
}
string_copy(ref_blob, view->ref);
}