Parse one downloaded checksum manifest without trusting line truncation or * substring matches. This non-header symbol is intentionally exercised by the * focused CLI regression tests. Duplicate entries are accepted only when they * name the exact artifact and normalize to the same SHA-256 digest. */
| 7993 | * focused CLI regression tests. Duplicate entries are accepted only when they |
| 7994 | * name the exact artifact and normalize to the same SHA-256 digest. */ |
| 7995 | int cbm_cli_checksum_manifest_digest(const char *manifest_path, const char *archive_name, char *out, |
| 7996 | size_t out_size) { |
| 7997 | if (out && out_size > 0) { |
| 7998 | out[0] = '\0'; |
| 7999 | } |
| 8000 | if (!manifest_path || !archive_name || !archive_name[0] || !out || out_size < SHA256_BUF_SIZE || |
| 8001 | strchr(archive_name, '\n') || strchr(archive_name, '\r')) { |
| 8002 | return CLI_ERR; |
| 8003 | } |
| 8004 | |
| 8005 | FILE *fp = cbm_fopen(manifest_path, "rb"); |
| 8006 | if (!fp) { |
| 8007 | return CLI_ERR; |
| 8008 | } |
| 8009 | unsigned char *manifest = malloc(CHECKSUM_MANIFEST_MAX_BYTES + 1U); |
| 8010 | if (!manifest) { |
| 8011 | (void)fclose(fp); |
| 8012 | return CLI_ERR; |
| 8013 | } |
| 8014 | size_t manifest_length = 0; |
| 8015 | bool read_ok = true; |
| 8016 | while (manifest_length <= CHECKSUM_MANIFEST_MAX_BYTES) { |
| 8017 | size_t capacity = CHECKSUM_MANIFEST_MAX_BYTES + 1U - manifest_length; |
| 8018 | size_t count = fread(manifest + manifest_length, 1, capacity, fp); |
| 8019 | manifest_length += count; |
| 8020 | if (ferror(fp)) { |
| 8021 | read_ok = false; |
| 8022 | break; |
| 8023 | } |
| 8024 | if (feof(fp)) { |
| 8025 | break; |
| 8026 | } |
| 8027 | if (count == 0) { |
| 8028 | read_ok = false; |
| 8029 | break; |
| 8030 | } |
| 8031 | } |
| 8032 | if (fclose(fp) != 0) { |
| 8033 | read_ok = false; |
| 8034 | } |
| 8035 | if (!read_ok || manifest_length == 0 || manifest_length > CHECKSUM_MANIFEST_MAX_BYTES || |
| 8036 | memchr(manifest, '\0', manifest_length)) { |
| 8037 | free(manifest); |
| 8038 | return CLI_ERR; |
| 8039 | } |
| 8040 | |
| 8041 | size_t archive_name_length = strlen(archive_name); |
| 8042 | char selected[SHA256_BUF_SIZE] = {0}; |
| 8043 | bool found = false; |
| 8044 | const unsigned char *cursor = manifest; |
| 8045 | const unsigned char *end = manifest + manifest_length; |
| 8046 | while (cursor < end) { |
| 8047 | const unsigned char *newline = memchr(cursor, '\n', (size_t)(end - cursor)); |
| 8048 | const unsigned char *line_end = newline ? newline : end; |
| 8049 | size_t line_length = (size_t)(line_end - cursor); |
| 8050 | if (line_length > 0 && cursor[line_length - 1U] == (unsigned char)'\r') { |
| 8051 | line_length--; |
| 8052 | } |
no test coverage detected