From: drafnel@gmail.com
To: git@vger.kernel.org
Cc: gitster@pobox.com, Brandon Casey <drafnel@gmail.com>
Subject: [PATCH 1/2] ident.c: New function valid_ident for checking ident string formatting
Date: Fri, 9 May 2008 00:03:35 -0500 [thread overview]
Message-ID: <56662.1210309343088.JavaMail.teamon@b304.teamon.com> (raw)
From: Brandon Casey <drafnel@gmail.com>
This function provides a centralized method for checking ident
strings which can be shared by the functions creating or parsing
author, committer, or tagger fields.
Signed-off-by: Brandon Casey <drafnel@gmail.com>
---
cache.h | 1 +
ident.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 70 insertions(+), 0 deletions(-)
diff --git a/cache.h b/cache.h
index d5d5dad..92f21e1 100644
--- a/cache.h
+++ b/cache.h
@@ -580,6 +580,7 @@ extern const char *git_author_info(int);
extern const char *git_committer_info(int);
extern const char *fmt_ident(const char *name, const char *email, const char *date_str, int);
extern const char *fmt_name(const char *name, const char *email);
+extern int valid_ident(const char *buf, size_t size, int flag);
struct checkout {
const char *base_dir;
diff --git a/ident.c b/ident.c
index ed44a53..1dc9b22 100644
--- a/ident.c
+++ b/ident.c
@@ -167,6 +167,75 @@ static int copy(char *buf, size_t size, int offset, const char *src)
return offset;
}
+int valid_ident(const char *buf, size_t size, int flag)
+{
+ const char *s, *lb, *rb;
+ int name_addr_only = (flag & IDENT_NO_DATE);
+
+ /* check for leading crud */
+ if (crud(*buf))
+ return 0;
+
+ /* Find start of email and check for disallowed chars in name */
+ if (!(lb = strstr(buf, " <")) || strpbrk(buf, "<>\n") < lb)
+ return 0;
+
+ /* Check for empty name and trailing crud in name */
+ if (lb == buf || crud(lb[-1]))
+ return 0;
+
+ /* Find end of email and check for disallowed chars in email */
+ if (!(rb = strchr(lb+2, '>')) || strpbrk(lb+2, "><\n ") < rb)
+ return 0;
+
+ /* Check for leading or trailing crud in email */
+ if (rb > lb+2 && (crud(lb[2]) || crud(rb[-1])))
+ return 0;
+
+ s = rb+1;
+ if (!name_addr_only) {
+ char tz[5];
+ size_t len;
+
+ /* a space separates email from timestamp */
+ if (*s != ' ')
+ return 0;
+ s++;
+
+ /*
+ * timestamp, 1 or more digits followed by space.
+ * fast-import at least assumes the timestamp and
+ * timezone fields will be 23 bytes or less. That
+ * leaves 17 bytes for the timestamp.
+ */
+ if (!(len = strspn(s, "0123456789")) || len > 17)
+ return 0;
+ s += len;
+
+ /* a space separates timestamp from timezone */
+ if (*s != ' ')
+ return 0;
+ s++;
+
+ /*
+ * timezone, 5 digits [+-]hhmm, max. 1400
+ * A buffer is used here to hold the timezone characters
+ * since our string may not be nul terminated immediately
+ * after the timezone field. Though it must adhere to the
+ * size parameter supplied by the caller.
+ */
+ if (!((s[0] == '+' || s[0] == '-') &&
+ strlcpy(tz, s+1, 5) >= 4 &&
+ strspn(tz, "0123456789") == 4 &&
+ atoi(tz) <= 1400))
+ return 0;
+
+ s += 5;
+ }
+
+ return (s - buf == size);
+}
+
static const char au_env[] = "GIT_AUTHOR_NAME";
static const char co_env[] = "GIT_COMMITTER_NAME";
static const char *env_hint =
--
1.5.5.67.g9a49
reply other threads:[~2008-05-09 5:19 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=56662.1210309343088.JavaMail.teamon@b304.teamon.com \
--to=drafnel@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).