Add UiaFlowsFree() function, and clean up some memory issues.

This commit is contained in:
Jordan Bancino 2023-02-24 14:40:21 +00:00
parent 26b0f33f96
commit adea499813
3 changed files with 28 additions and 22 deletions

View file

@ -55,7 +55,7 @@ ROUTE_IMPL(RouteRegister, args)
User *user = NULL; User *user = NULL;
Array *uiaFlows; Array *uiaFlows = NULL;
int uiaResult; int uiaResult;
if (MATRIX_PATH_PARTS(args->path) == 0) if (MATRIX_PATH_PARTS(args->path) == 0)
@ -241,6 +241,7 @@ ROUTE_IMPL(RouteRegister, args)
UserUnlock(user); UserUnlock(user);
finish: finish:
UiaFlowsFree(uiaFlows);
Free(username); Free(username);
Free(password); Free(password);
Free(deviceId); Free(deviceId);

View file

@ -135,6 +135,8 @@ BuildResponse(Array * flows, char *session, Db * db, HashMap ** response)
DbUnlock(db, ref); DbUnlock(db, ref);
HashMapSet(*response, "completed", JsonValueArray(ArrayCreate())); HashMapSet(*response, "completed", JsonValueArray(ArrayCreate()));
HashMapSet(*response, "session", JsonValueString(session));
Free(session);
} }
else else
{ {
@ -167,11 +169,11 @@ BuildResponse(Array * flows, char *session, Db * db, HashMap ** response)
} }
HashMapSet(*response, "completed", JsonValueArray(completed)); HashMapSet(*response, "completed", JsonValueArray(completed));
HashMapSet(*response, "session", JsonValueString(session));
DbUnlock(db, ref); DbUnlock(db, ref);
} }
HashMapSet(*response, "session", JsonValueString(session));
return 0; return 0;
} }
@ -200,7 +202,7 @@ UiaBuildStage(char *type, HashMap * params)
return NULL; return NULL;
} }
stage->type = type; stage->type = StrDuplicate(type);
stage->params = params; stage->params = params;
return stage; return stage;
@ -218,9 +220,6 @@ UiaComplete(Array * flows, HttpServerContext * context, Db * db,
DbRef *dbRef; DbRef *dbRef;
HashMap *dbJson; HashMap *dbJson;
size_t i, j;
int ret = 0;
if (!flows) if (!flows)
{ {
return -1; return -1;
@ -228,8 +227,7 @@ UiaComplete(Array * flows, HttpServerContext * context, Db * db,
if (!context || !db || !request || !response) if (!context || !db || !request || !response)
{ {
ret = -1; return -1;
goto finish;
} }
val = HashMapGet(request, "auth"); val = HashMapGet(request, "auth");
@ -237,16 +235,14 @@ UiaComplete(Array * flows, HttpServerContext * context, Db * db,
if (!val) if (!val)
{ {
HttpResponseStatus(context, HTTP_UNAUTHORIZED); HttpResponseStatus(context, HTTP_UNAUTHORIZED);
ret = BuildResponse(flows, NULL, db, response); return BuildResponse(flows, NULL, db, response);
goto finish;
} }
if (JsonValueType(val) != JSON_OBJECT) if (JsonValueType(val) != JSON_OBJECT)
{ {
HttpResponseStatus(context, HTTP_BAD_REQUEST); HttpResponseStatus(context, HTTP_BAD_REQUEST);
*response = MatrixErrorCreate(M_BAD_JSON); *response = MatrixErrorCreate(M_BAD_JSON);
ret = 0; return 0;
goto finish;
} }
auth = JsonValueAsObject(val); auth = JsonValueAsObject(val);
@ -256,8 +252,7 @@ UiaComplete(Array * flows, HttpServerContext * context, Db * db,
{ {
HttpResponseStatus(context, HTTP_BAD_REQUEST); HttpResponseStatus(context, HTTP_BAD_REQUEST);
*response = MatrixErrorCreate(M_BAD_JSON); *response = MatrixErrorCreate(M_BAD_JSON);
ret = 0; return 0;
goto finish;
} }
session = JsonValueAsString(val); session = JsonValueAsString(val);
@ -267,8 +262,7 @@ UiaComplete(Array * flows, HttpServerContext * context, Db * db,
{ {
HttpResponseStatus(context, HTTP_BAD_REQUEST); HttpResponseStatus(context, HTTP_BAD_REQUEST);
*response = MatrixErrorCreate(M_BAD_JSON); *response = MatrixErrorCreate(M_BAD_JSON);
ret = 0; return 0;
goto finish;
} }
authType = JsonValueAsString(val); authType = JsonValueAsString(val);
@ -277,17 +271,26 @@ UiaComplete(Array * flows, HttpServerContext * context, Db * db,
if (!dbRef) if (!dbRef)
{ {
HttpResponseStatus(context, HTTP_UNAUTHORIZED); HttpResponseStatus(context, HTTP_UNAUTHORIZED);
ret = BuildResponse(flows, StrDuplicate(session), db, response); return BuildResponse(flows, StrDuplicate(session), db, response);
goto finish;
} }
dbJson = DbJson(dbRef); dbJson = DbJson(dbRef);
DbUnlock(db, dbRef); DbUnlock(db, dbRef);
ret = 1; return 1;
}
void
UiaFlowsFree(Array *flows)
{
size_t i, j;
if (!flows)
{
return;
}
finish:
for (i = 0; i < ArraySize(flows); i++) for (i = 0; i < ArraySize(flows); i++)
{ {
Array *stages = ArrayGet(flows, i); Array *stages = ArrayGet(flows, i);
@ -304,7 +307,6 @@ finish:
ArrayFree(stages); ArrayFree(stages);
} }
ArrayFree(flows); ArrayFree(flows);
return ret;
} }
void void

View file

@ -41,6 +41,9 @@ extern void
UiaCleanup(MatrixHttpHandlerArgs *); UiaCleanup(MatrixHttpHandlerArgs *);
extern int extern int
UiaComplete(Array * stages, HttpServerContext *, Db *, HashMap *, HashMap **); UiaComplete(Array *, HttpServerContext *, Db *, HashMap *, HashMap **);
extern void
UiaFlowsFree(Array *);
#endif #endif