From b3f358f8390b17aa45549cdf634615e63c98a11b Mon Sep 17 00:00:00 2001
From: unknown <jkovacic@.(none)>
Date: Tue, 25 Oct 2011 16:22:45 +0200
Subject: [PATCH 2/2] _libssh2_error, tabs, etc.

---
 docs/libssh2_session_supported_algs.3 |    4 +
 include/libssh2.h                     |    4 +-
 src/comp.c                            |   10 ---
 src/kex.c                             |  131 +++++++++++++++++----------------
 4 files changed, 72 insertions(+), 77 deletions(-)

diff --git a/docs/libssh2_session_supported_algs.3 b/docs/libssh2_session_supported_algs.3
index d661492..a5719a2 100644
--- a/docs/libssh2_session_supported_algs.3
+++ b/docs/libssh2_session_supported_algs.3
@@ -15,6 +15,10 @@ to method_type in \fIlibssh2_session_method_pref(3)\fP. If successful, the funct
 amount of memory. When not needed anymore, it must be deallocated by calling .BR libssh2_free(3). When the function
 is unsuccessful, this must not be done.
 
+NOTE: In order to get a list of all supported compression algorithms, 
+libssh2_session_flag(session, LIBSSH2_FLAG_COMPRESS, 1) 
+must be called before calling this function, otherwise only "none" will be returned.
+
 If successful, the function will allocate and fill the array with supported algorithms (the same names as defined in RFC 4253).
 The array is not NULL terminated.
 .SH RETURN VALUE
diff --git a/include/libssh2.h b/include/libssh2.h
index dc14c87..6d48f34 100644
--- a/include/libssh2.h
+++ b/include/libssh2.h
@@ -451,8 +451,8 @@ LIBSSH2_API void libssh2_free(LIBSSH2_SESSION *session, void *ptr);
  * NOTE: on success, algs must be deallocated (by calling libssh2_free) when not needed anymore
  */
 LIBSSH2_API int libssh2_session_supported_algs(LIBSSH2_SESSION* session, 
-											   int method_type, 
-											   const char*** algs); 
+                                               int method_type, 
+                                               const char*** algs); 
 
 /* Session API */
 LIBSSH2_API LIBSSH2_SESSION *
diff --git a/src/comp.c b/src/comp.c
index 216d861..0296f62 100644
--- a/src/comp.c
+++ b/src/comp.c
@@ -383,16 +383,6 @@ static const LIBSSH2_COMP_METHOD *no_comp_methods[] = {
 const LIBSSH2_COMP_METHOD **
 _libssh2_comp_methods(LIBSSH2_SESSION *session)
 {
-	/* 
-       Looks like the original implementation expects a non-NULL session only.
-       When asking for supported algorithms, however, session will typically be NULL,
-       so the following is necessary to avoid NULL dereferrencing caused core dumps.
-    */
-    if ( NULL==session )
-    {
-        return comp_methods;
-    }
-
     if(session->flag.compress)
         return comp_methods;
     else
diff --git a/src/kex.c b/src/kex.c
index 70ac481..3a870cc 100644
--- a/src/kex.c
+++ b/src/kex.c
@@ -1908,15 +1908,15 @@ LIBSSH2_API int libssh2_session_supported_algs(LIBSSH2_SESSION* session, int met
         TODO: do more appropriate error codes exist?
     */
     unsigned int i;
-	unsigned int j;
+    unsigned int j;
     unsigned int ialg;
     const LIBSSH2_COMMON_METHOD **mlist;
 
-	/* to prevent coredumps due to dereferencing of NULL */
-	if ( NULL == algs )
-	{
-		return LIBSSH2_ERROR_BAD_USE;  /* invalid algs */
-	}
+    /* to prevent coredumps due to dereferencing of NULL */
+    if ( NULL == algs )
+    {
+        return _libssh2_error(session, LIBSSH2_ERROR_BAD_USE, "algs must not be NULL");
+    }
 
     switch (method_type)
     {
@@ -1940,75 +1940,76 @@ LIBSSH2_API int libssh2_session_supported_algs(LIBSSH2_SESSION* session, int met
 
         case LIBSSH2_METHOD_COMP_CS:
         case LIBSSH2_METHOD_COMP_SC:
-            mlist = (const LIBSSH2_COMMON_METHOD **) _libssh2_comp_methods(NULL);
+            mlist = (const LIBSSH2_COMMON_METHOD **) _libssh2_comp_methods(session);
             break;
 
         default:
-            return LIBSSH2_ERROR_METHOD_NOT_SUPPORTED;  /* unknown method type */
+            return _libssh2_error(session, LIBSSH2_ERROR_METHOD_NOT_SUPPORTED, "Unknown method type");
     }  /* switch */
 
+    /* weird situation */
     if ( NULL==mlist )
     {
-        return LIBSSH2_ERROR_INVAL; /* weird situation */
+        return _libssh2_error(session, LIBSSH2_ERROR_INVAL, "No algorithm found");
+    }
+
+    /*
+        mlist is looped through twice. The first time to find the number od supported algorithms 
+        (needed to allocate the proper size of array) and the second time to actually copy the pointers.
+        Typically this function will not be called often (typically at the beginning of a session) and the
+        number of algorithms (i.e. niumber of iterations in one loop) will not be high 
+        (typically it will not exceed 20) for quite a long time.
+
+        So double looping really shouldn't be an issue and it is definitely a better solution than 
+        reallocation several times.
+    */
+
+    /* count the number of supported algorithms */
+    for ( i=0, ialg=0; NULL!=mlist[i]; i++)
+    {
+        /* do not count fields with NULL name */
+        if ( NULL!=mlist[i]->name )
+        {
+            ialg++;
+        }
+    }
+
+    /* weird situation, no algorithm found */
+    if ( 0==ialg )
+    {
+        return _libssh2_error(session, LIBSSH2_ERROR_INVAL, "No algorithm found");
     }
 
-	/*
-		mlist is looped through twice. The first time to find the number od supported algorithms 
-		(needed to allocate the proper size of array) and the second time to actually copy the pointers.
-		Typically this function will not be called often (typically at the beginning of a session) and the
-		number of algorithms (i.e. niumber of iterations in one loop) will not be high 
-		(typically it will not exceed 20) for quite a long time.
-
-		So double looping really shouldn't be an issue and it is definitely a better solution than 
-		reallocation several times.
-	*/
-
-	/* count the number of supported algorithms */
-	for ( i=0, ialg=0; NULL!=mlist[i]; i++)
-	{
-		/* do not count fields with NULL name */
-		if ( NULL!=mlist[i]->name )
-		{
-			ialg++;
-		}
-	}
-
-	/* weird situation, no algorithm found */
-	if ( 0==ialg )
-	{
-		return LIBSSH2_ERROR_INVAL;
-	}
-
-	/* allocate buffer */
-	*algs = (const char**) LIBSSH2_ALLOC(session, ialg*sizeof(const char*));
-	if ( NULL==*algs )
-	{
-		return LIBSSH2_ERROR_ALLOC;
-	}
-	/* Past this point *algs must be deallocated in case of an error!! */
+    /* allocate buffer */
+    *algs = (const char**) LIBSSH2_ALLOC(session, ialg*sizeof(const char*));
+    if ( NULL==*algs )
+    {
+        return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, "Memory allocation failed");
+    }
+    /* Past this point *algs must be deallocated in case of an error!! */
 	
-	/* copy non-NULL pointers*/
-	for ( i=0, j=0; NULL!=mlist[i] && j<ialg; i++ )
-	{
-		if ( NULL==mlist[i]->name )
-		{
-			/* maybe a weird situation but if it occurs, do not include NULL pointers */
-			continue;
-		}
-
-		/* note that [] has higher priority than * (dereferencing) */
-		(*algs)[j++] = mlist[i]->name;
-	}
-
-	/* proper number of pointers copied? (test the code above) */
-	if ( j!=ialg )
-	{
-		/* deallocate buffer */
-		LIBSSH2_FREE(session, *algs);
-		*algs = NULL;
-
-		return LIBSSH2_ERROR_BAD_USE;
-	}
+    /* copy non-NULL pointers only */
+    for ( i=0, j=0; NULL!=mlist[i] && j<ialg; i++ )
+    {
+        if ( NULL==mlist[i]->name )
+        {
+            /* maybe a weird situation but if it occurs, do not include NULL pointers */
+            continue;
+        }
+
+        /* note that [] has higher priority than * (dereferencing) */
+        (*algs)[j++] = mlist[i]->name;
+    }
+
+    /* correct number of pointers copied? (test the code above) */
+    if ( j!=ialg )
+    {
+        /* deallocate buffer */
+        LIBSSH2_FREE(session, *algs);
+        *algs = NULL;
+
+        return _libssh2_error(session, LIBSSH2_ERROR_BAD_USE, "Internal error");
+    }
 
     return ialg;
 }
-- 
1.7.6.msysgit.0

