From 7887ec31b59b40dcfa7eca225fbc788e2c961727 Mon Sep 17 00:00:00 2001 From: Jaakko Keränen Date: Sun, 25 Apr 2021 08:57:59 +0300 Subject: GmRequest: Use unique IDs to avoid confusion It is conceivable that a newly created GmRequest gets the same memory location than the one just destroyed. IssueID #148 --- src/gmrequest.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'src/gmrequest.c') diff --git a/src/gmrequest.c b/src/gmrequest.c index 8b87a638..8ab37c61 100644 --- a/src/gmrequest.c +++ b/src/gmrequest.c @@ -118,6 +118,8 @@ void deserialize_GmResponse(iGmResponse *d, iStream *ins) { /*----------------------------------------------------------------------------------------------*/ +static iAtomicInt idGen_; + enum iGmRequestState { initialized_GmRequestState, receivingHeader_GmRequestState, @@ -128,6 +130,7 @@ enum iGmRequestState { struct Impl_GmRequest { iObject object; + uint32_t id; iMutex * mtx; iGmCerts * certs; /* not owned */ enum iGmRequestState state; @@ -477,7 +480,8 @@ static void beginGopherConnection_GmRequest_(iGmRequest *d, const iString *host, /*----------------------------------------------------------------------------------------------*/ void init_GmRequest(iGmRequest *d, iGmCerts *certs) { - d->mtx = new_Mutex(); + d->mtx = new_Mutex(); + d->id = add_Atomic(&idGen_, 1) + 1; d->resp = new_GmResponse(); d->isFilterEnabled = iTrue; d->isRespLocked = iFalse; @@ -868,11 +872,18 @@ void unlockResponse_GmRequest(iGmRequest *d) { } } +uint32_t id_GmRequest(const iGmRequest *d) { + return d ? d->id : 0; +} + iBool isFinished_GmRequest(const iGmRequest *d) { - iBool done; - iGuardMutex(d->mtx, - done = (d->state == finished_GmRequestState || d->state == failure_GmRequestState)); - return done; + if (d) { + iBool done; + iGuardMutex(d->mtx, + done = (d->state == finished_GmRequestState || d->state == failure_GmRequestState)); + return done; + } + return iTrue; } enum iGmStatusCode status_GmRequest(const iGmRequest *d) { -- cgit v1.2.3