mercurial/cext/manifest.c
changeset 40600 f27f8e9ef1e7
parent 40599 9eeda7199181
child 40601 da4478ca0e32
equal deleted inserted replaced
40599:9eeda7199181 40600:f27f8e9ef1e7
    37 
    37 
    38 #define MANIFEST_OOM -1
    38 #define MANIFEST_OOM -1
    39 #define MANIFEST_NOT_SORTED -2
    39 #define MANIFEST_NOT_SORTED -2
    40 #define MANIFEST_MALFORMED -3
    40 #define MANIFEST_MALFORMED -3
    41 #define MANIFEST_BOGUS_FILENAME -4
    41 #define MANIFEST_BOGUS_FILENAME -4
       
    42 #define MANIFEST_TOO_SHORT_LINE -5
    42 
    43 
    43 /* get the length of the path for a line */
    44 /* get the length of the path for a line */
    44 static size_t pathlen(line *l)
    45 static size_t pathlen(line *l)
    45 {
    46 {
    46 	const char *end = memchr(l->start, '\0', l->len);
    47 	const char *end = memchr(l->start, '\0', l->len);
   123 			return MANIFEST_BOGUS_FILENAME;
   124 			return MANIFEST_BOGUS_FILENAME;
   124 		}
   125 		}
   125 		next = memchr(data, '\n', len);
   126 		next = memchr(data, '\n', len);
   126 		if (!next) {
   127 		if (!next) {
   127 			return MANIFEST_MALFORMED;
   128 			return MANIFEST_MALFORMED;
       
   129 		}
       
   130 		if ((next - data) < 22) {
       
   131 			/* We should have at least 22 bytes in a line:
       
   132 			   1 byte filename
       
   133 			   1 NUL
       
   134 			   20 bytes of hash
       
   135 			   so we can give up here.
       
   136 			*/
       
   137 			return MANIFEST_TOO_SHORT_LINE;
   128 		}
   138 		}
   129 		next++; /* advance past newline */
   139 		next++; /* advance past newline */
   130 		if (!realloc_if_full(self)) {
   140 		if (!realloc_if_full(self)) {
   131 			return MANIFEST_OOM; /* no memory */
   141 			return MANIFEST_OOM; /* no memory */
   132 		}
   142 		}
   199 		break;
   209 		break;
   200 	case MANIFEST_BOGUS_FILENAME:
   210 	case MANIFEST_BOGUS_FILENAME:
   201 		PyErr_Format(
   211 		PyErr_Format(
   202 			PyExc_ValueError,
   212 			PyExc_ValueError,
   203 			"Manifest had an entry with a zero-length filename.");
   213 			"Manifest had an entry with a zero-length filename.");
       
   214 		break;
       
   215 	case MANIFEST_TOO_SHORT_LINE:
       
   216 		PyErr_Format(
       
   217 			PyExc_ValueError,
       
   218 			"Manifest had implausibly-short line.");
   204 		break;
   219 		break;
   205 	default:
   220 	default:
   206 		PyErr_Format(PyExc_ValueError,
   221 		PyErr_Format(PyExc_ValueError,
   207 			     "Unknown problem parsing manifest.");
   222 			     "Unknown problem parsing manifest.");
   208 	}
   223 	}