diff --git a/lib/curl_sasl_gssapi.c b/lib/curl_sasl_gssapi.c
index d8f81196198c9f582bbced01f9233c92d9c8f086..cee00e10a162d9a1f7bd08c21b5930574748cda4 100644
--- a/lib/curl_sasl_gssapi.c
+++ b/lib/curl_sasl_gssapi.c
@@ -122,8 +122,12 @@ CURLcode Curl_sasl_create_gssapi_user_message(struct SessionHandle *data,
     if(GSS_ERROR(gss_major_status)) {
       Curl_gss_log_error(data, gss_minor_status, "gss_import_name() failed: ");
 
+      Curl_safefree(spn);
+
       return CURLE_OUT_OF_MEMORY;
     }
+
+    Curl_safefree(spn);
   }
   else {
     /* Decode the base-64 encoded challenge message */