static int read_checksum_for(const char *checksum_path, const char *file_name, char *sha1_hex, size_t sha1_hex_len)
{
int result = -1;
- if (sha1_hex_len != (2 * SHA1_LEN + 1)) {
+
+ if (sha1_hex_len < (2 * SHA1_LEN + 1)) {
_CLOGE("Checksum buffer too small");
return result;
}
}
char *line = NULL;
- size_t line_len;
+ size_t line_len = 0;
while (getline(&line, &line_len, checksum_fp) != -1) {
- char *file = rindex(line, ' ');
- if (!file || ! ++file)
+ char *saveptr = NULL;
+ char *checksum = NULL;
+ char *checksum_fname = NULL;
+
+ checksum = strtok_r(line, " \n", &saveptr);
+
+ if (checksum == NULL)
continue;
- if (strncmp(file, file_name, strlen(file_name)) == 0) {
- char *saveptr;
- char *checksum = strtok_r(line, " ", &saveptr);
- if (!checksum)
- continue;
+ if (strlen(checksum) != (2 * SHA1_LEN))
+ continue;
+
+ checksum_fname = strtok_r(NULL, " \n", &saveptr);
- if (strlen(checksum) != 2 * SHA1_LEN)
- continue; // the read checksum has an inappropriate size
+ if (checksum_fname == NULL)
+ continue;
- strncpy(sha1_hex, checksum, sha1_hex_len);
+ if (strncmp(checksum_fname, file_name, strlen(file_name) + 1) == 0) {
+ strncpy(sha1_hex, checksum, sha1_hex_len - 1);
+ sha1_hex[sha1_hex_len - 1] = '\0';
result = 0;
break;
}