@matheusbsbs / matheus-bagliano / commits / a4c0d463c0
Fork 0

Make section_name_match start on '[', and return the length on success

Signed-off-by: Alex Vandiver <alex@chmrr.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Alex Vandiver committed Jul 24, 2009 at 17:21 UTC a4c0d463c0a1b38b7af45c1e7dd5608860eb1e28
1 file changed +16 -3
config.c
+16 -3
@@ -1174,7 +1174,9 @@ write_err_out:
1174 static int section_name_match (const char *buf, const char *name)
1175 {
1176 int i = 0, j = 0, dot = 0;
1177 - for (; buf[i] && buf[i] != ']'; i++) {
1177 + if (buf[i] != '[')
1178 + return 0;
1179 + for (i = 1; buf[i] && buf[i] != ']'; i++) {
1180 if (!dot && isspace(buf[i])) {
1181 dot = 1;
1182 if (name[j++] != '.')
@@ -1195,7 +1197,17 @@ static int section_name_match (const char *buf, const char *name)
1197 if (buf[i] != name[j++])
1198 break;
1199 }
1198 - return (buf[i] == ']' && name[j] == 0);
1200 + if (buf[i] == ']' && name[j] == 0) {
1201 + /*
1202 + * We match, now just find the right length offset by
1203 + * gobbling up any whitespace after it, as well
1204 + */
1205 + i++;
1206 + for (; buf[i] && isspace(buf[i]); i++)
1207 + ; /* do nothing */
1208 + return i;
1209 + }
1210 + return 0;
1211 }
1212
1213 /* if new_name == NULL, the section is removed instead */
@@ -1229,7 +1241,8 @@ int git_config_rename_section(const char *old_name, const char *new_name)
1241 ; /* do nothing */
1242 if (buf[i] == '[') {
1243 /* it's a section */
1232 - if (section_name_match (&buf[i+1], old_name)) {
1244 + int offset = section_name_match(&buf[i], old_name);
1245 + if (offset > 0) {
1246 ret++;
1247 if (new_name == NULL) {
1248 remove = 1;