@matheusbsbs / matheus-bagliano / commits / 0111ea38cb
Fork 0

cache-tree: replace a sscanf() by two strtol() calls

On one of my systems, sscanf() first calls strlen() on the buffer. But this buffer is not terminated by NUL. So git crashed. strtol() does not share that problem, as it stops reading after the first non-digit. [jc: original patch was wrong and did not read the cache-tree structure correctly; this has been fixed up and tested minimally with fsck-objects. ] Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de> Signed-off-by: Junio C Hamano <junkio@cox.net>

Johannes Schindelin committed May 2, 2006 at 03:31 UTC 0111ea38cbb9db0e4e245dcd5c86198186bab197
1 file changed +10 -1
cache-tree.c
+10 -1
@@ -440,6 +440,8 @@ static struct cache_tree *read_one(const char **buffer, unsigned long *size_p)
440 {
441 const char *buf = *buffer;
442 unsigned long size = *size_p;
443 + const char *cp;
444 + char *ep;
445 struct cache_tree *it;
446 int i, subtree_nr;
447
@@ -453,7 +455,14 @@ static struct cache_tree *read_one(const char **buffer, unsigned long *size_p)
455 goto free_return;
456 buf++; size--;
457 it = cache_tree();
456 - if (sscanf(buf, "%d %d\n", &it->entry_count, &subtree_nr) != 2)
458 +
459 + cp = buf;
460 + it->entry_count = strtol(cp, &ep, 10);
461 + if (cp == ep)
462 + goto free_return;
463 + cp = ep;
464 + subtree_nr = strtol(cp, &ep, 10);
465 + if (cp == ep)
466 goto free_return;
467 while (size && *buf && *buf != '\n') {
468 size--;