mirror of
git://git.gnupg.org/gnupg.git
synced 2025-03-28 22:49:59 +01:00
gpg: Return better error codes in case of a too short trustdb.
* g10/tdbio.c (tdbio_read_record): Return GPG_ERR_EOF. (tdbio_new_recnum): Never return on error. (lookup_hashtable): Print a more descriptive error in case of !TABLE. -- Also: tdbio_new_recnum had a bug in that it returned an error code and not a record number in the error case. The function is expected to always return a valid new record number. Signed-off-by: Werner Koch <wk@gnupg.org>
This commit is contained in:
parent
456a3a8e93
commit
403aa70c52
25
g10/tdbio.c
25
g10/tdbio.c
@ -1292,6 +1292,13 @@ lookup_hashtable (ulong table, const byte *key, size_t keylen,
|
|||||||
int msb;
|
int msb;
|
||||||
int level = 0;
|
int level = 0;
|
||||||
|
|
||||||
|
if (!table)
|
||||||
|
{
|
||||||
|
rc = gpg_error (GPG_ERR_INV_RECORD);
|
||||||
|
log_error("lookup_hashtable failed: %s\n", "request for record 0");
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
hashrec = table;
|
hashrec = table;
|
||||||
next_level:
|
next_level:
|
||||||
msb = key[level];
|
msb = key[level];
|
||||||
@ -1464,7 +1471,7 @@ tdbio_dump_record (TRUSTREC *rec, estream_t fp)
|
|||||||
* EXPECTED is not 0 reading any other record type will return an
|
* EXPECTED is not 0 reading any other record type will return an
|
||||||
* error.
|
* error.
|
||||||
*
|
*
|
||||||
* Return: 0 on success, -1 on EOF, or an error code.
|
* Return: 0 on success or an error code.
|
||||||
*/
|
*/
|
||||||
int
|
int
|
||||||
tdbio_read_record (ulong recnum, TRUSTREC *rec, int expected)
|
tdbio_read_record (ulong recnum, TRUSTREC *rec, int expected)
|
||||||
@ -1489,7 +1496,7 @@ tdbio_read_record (ulong recnum, TRUSTREC *rec, int expected)
|
|||||||
n = read (db_fd, readbuf, TRUST_RECORD_LEN);
|
n = read (db_fd, readbuf, TRUST_RECORD_LEN);
|
||||||
if (!n)
|
if (!n)
|
||||||
{
|
{
|
||||||
return -1; /* eof */
|
return gpg_error (GPG_ERR_EOF);
|
||||||
}
|
}
|
||||||
else if (n != TRUST_RECORD_LEN)
|
else if (n != TRUST_RECORD_LEN)
|
||||||
{
|
{
|
||||||
@ -1769,20 +1776,14 @@ tdbio_new_recnum (ctrl_t ctrl)
|
|||||||
recnum = vr.r.ver.firstfree;
|
recnum = vr.r.ver.firstfree;
|
||||||
rc = tdbio_read_record (recnum, &rec, RECTYPE_FREE);
|
rc = tdbio_read_record (recnum, &rec, RECTYPE_FREE);
|
||||||
if (rc)
|
if (rc)
|
||||||
{
|
log_fatal (_("%s: error reading free record: %s\n"),
|
||||||
log_error (_("%s: error reading free record: %s\n"),
|
|
||||||
db_name, gpg_strerror (rc));
|
db_name, gpg_strerror (rc));
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
/* Update dir record. */
|
/* Update dir record. */
|
||||||
vr.r.ver.firstfree = rec.r.free.next;
|
vr.r.ver.firstfree = rec.r.free.next;
|
||||||
rc = tdbio_write_record (ctrl, &vr);
|
rc = tdbio_write_record (ctrl, &vr);
|
||||||
if (rc)
|
if (rc)
|
||||||
{
|
log_fatal (_("%s: error writing dir record: %s\n"),
|
||||||
log_error (_("%s: error writing dir record: %s\n"),
|
|
||||||
db_name, gpg_strerror (rc));
|
db_name, gpg_strerror (rc));
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
/* Zero out the new record. */
|
/* Zero out the new record. */
|
||||||
memset (&rec, 0, sizeof rec);
|
memset (&rec, 0, sizeof rec);
|
||||||
rec.rectype = 0; /* Mark as unused record (actually already done
|
rec.rectype = 0; /* Mark as unused record (actually already done
|
||||||
@ -1799,7 +1800,7 @@ tdbio_new_recnum (ctrl_t ctrl)
|
|||||||
if (offset == (off_t)(-1))
|
if (offset == (off_t)(-1))
|
||||||
log_fatal ("trustdb: lseek to end failed: %s\n", strerror (errno));
|
log_fatal ("trustdb: lseek to end failed: %s\n", strerror (errno));
|
||||||
recnum = offset / TRUST_RECORD_LEN;
|
recnum = offset / TRUST_RECORD_LEN;
|
||||||
log_assert (recnum); /* this is will never be the first record */
|
log_assert (recnum); /* This will never be the first record */
|
||||||
/* We must write a record, so that the next call to this
|
/* We must write a record, so that the next call to this
|
||||||
* function returns another recnum. */
|
* function returns another recnum. */
|
||||||
memset (&rec, 0, sizeof rec);
|
memset (&rec, 0, sizeof rec);
|
||||||
@ -1821,7 +1822,7 @@ tdbio_new_recnum (ctrl_t ctrl)
|
|||||||
{
|
{
|
||||||
rc = gpg_error_from_syserror ();
|
rc = gpg_error_from_syserror ();
|
||||||
log_error (_("trustdb rec %lu: write failed (n=%d): %s\n"),
|
log_error (_("trustdb rec %lu: write failed (n=%d): %s\n"),
|
||||||
recnum, n, strerror (errno));
|
recnum, n, gpg_strerror (rc));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user