Index: trunk/src/kmk/kbuild.c
===================================================================
--- trunk/src/kmk/kbuild.c	(revision 1923)
+++ trunk/src/kmk/kbuild.c	(revision 1924)
@@ -354,5 +354,5 @@
  */
 static void
-kbuild_apply_defpath(struct variable *pDefPath, char **ppsz, int *pcch, int *pcchAlloc, int fCanFree)
+kbuild_apply_defpath(struct variable *pDefPath, char **ppsz, unsigned int *pcch, int *pcchAlloc, int fCanFree)
 {
     const char *pszIterator;
@@ -643,5 +643,5 @@
     struct variable *pVar = kbuild_lookup_variable_n(pszName, cchName);
     if (pVar && pDefPath)
-        kbuild_apply_defpath(pDefPath, &pVar->value, &pVar->value_length, &pVar->value_alloc_len, 1);
+        kbuild_apply_defpath(pDefPath, &pVar->value, (unsigned int *)&pVar->value_length, &pVar->value_alloc_len, 1);
     return pVar;
 }
@@ -661,5 +661,5 @@
     struct variable *pVar = kbuild_lookup_variable(pszName);
     if (pVar && pDefPath)
-        kbuild_apply_defpath(pDefPath, &pVar->value, &pVar->value_length, &pVar->value_alloc_len, 1);
+        kbuild_apply_defpath(pDefPath, &pVar->value, (unsigned int *)&pVar->value_length, &pVar->value_alloc_len, 1);
     return pVar;
 }
@@ -1530,5 +1530,5 @@
     struct variable *pVar;
     unsigned iSdk, iSdkEnd;
-    int cVars, iVar, iVarEnd;
+    int cVars, iVar;
     size_t cchTotal, cchBuf;
     char *pszResult, *pszBuf, *psz, *psz2, *psz3;
@@ -1536,5 +1536,5 @@
     {
         struct variable    *pVar;
-        int                 cchExp;
+        unsigned int        cchExp;
         char               *pszExp;
     } *paVars;
@@ -1559,4 +1559,6 @@
     /*
      * Get the variables.
+     *
+     * The compiler will get a heart attack when it sees this code ... ;-)
      */
     cVars = 12 * (pSdks->c + 5);
@@ -1564,4 +1566,5 @@
 
     iVar = 0;
+    cchTotal = 0;
 
 #define ADD_VAR(pVar)           do { my_memcpy(psz, (pVar)->value, (pVar)->value_length); psz += (pVar)->value_length; } while (0)
@@ -1569,9 +1572,37 @@
 #define ADD_CSTR(pszStr)        do { my_memcpy(psz, pszStr, sizeof(pszStr) - 1); psz += sizeof(pszStr) - 1; } while (0)
 #define ADD_CH(ch)              do { *psz++ = (ch); } while (0)
-# define DO_VAR_LOOKUP() \
+#define DO_VAR_LOOKUP() \
     do { \
         pVar = kbuild_lookup_variable_n(pszBuf, psz - pszBuf); \
-        /*if (pVar)*/ \
-            paVars[iVar++].pVar = pVar; \
+        if (pVar) \
+        { \
+            paVars[iVar].pVar = pVar; \
+            if (    !pVar->recursive \
+                ||  !memchr(pVar->value, '$', pVar->value_length)) \
+            { \
+                paVars[iVar].pszExp = pVar->value; \
+                paVars[iVar].cchExp = pVar->value_length; \
+                if (pDefPath && paVars[iVar].cchExp) \
+                    kbuild_apply_defpath(pDefPath, &paVars[iVar].pszExp, &paVars[iVar].cchExp, NULL, 0); \
+                if (paVars[iVar].cchExp) \
+                { \
+                    cchTotal += paVars[iVar].cchExp + 1; \
+                    iVar++; \
+                } \
+            } \
+            else \
+            { \
+                paVars[iVar].pszExp = allocated_variable_expand_2(pVar->value, pVar->value_length, &paVars[iVar].cchExp); \
+                if (pDefPath && paVars[iVar].cchExp) \
+                    kbuild_apply_defpath(pDefPath, &paVars[iVar].pszExp, &paVars[iVar].cchExp, NULL, 1); \
+                if (paVars[iVar].cchExp) \
+                { \
+                    cchTotal += paVars[iVar].cchExp + 1; \
+                    iVar++; \
+                } \
+                else \
+                    free(paVars[iVar].pszExp); \
+            } \
+        } \
     } while (0)
 #define DO_SINGLE_PSZ3_VARIATION() \
@@ -1707,62 +1738,45 @@
     free(pszBuf);
 
-    /*assert(iVar <= cVars);
-    cVars = iVar;*/
-    assert(iVar == cVars);
-
-    if (!cVars)
+    assert(iVar <= cVars);
+    cVars = iVar;
+
+    /*
+     * Construct the result value.
+     */
+    if (!cVars || !cchTotal)
         pVar = define_variable_vl(pszVarName, cchVarName, "", 0,
                                   1 /* duplicate value */ , o_local, 0 /* !recursive */);
     else
     {
-        /*
-         * Expand the variables and calculate the total length.
-         */
-        cchTotal = 0;
-        iVarEnd = iDirection == 1 ? cVars : 0;
-        for (iVar = iDirection == 1 ? 0 : cVars - 1; iVar != iVarEnd; iVar += iDirection)
+        psz = pszResult = xmalloc(cchTotal + 1);
+        if (iDirection == 1)
         {
-            if (!paVars[iVar].pVar)
+            for (iVar = 0; iVar < cVars; iVar++)
             {
-                paVars[iVar].cchExp = 0;
-                continue;
+                my_memcpy(psz, paVars[iVar].pszExp, paVars[iVar].cchExp);
+                psz += paVars[iVar].cchExp;
+                *psz++ = ' ';
+                if (paVars[iVar].pszExp != paVars[iVar].pVar->value)
+                    free(paVars[iVar].pszExp);
             }
-            if (    !paVars[iVar].pVar->recursive
-                ||  !memchr(paVars[iVar].pVar->value, '$', paVars[iVar].pVar->value_length))
+        }
+        else
+        {
+            iVar = cVars;
+            while (iVar-- > 0)
             {
-                paVars[iVar].pszExp = paVars[iVar].pVar->value;
-                paVars[iVar].cchExp = paVars[iVar].pVar->value_length;
+                my_memcpy(psz, paVars[iVar].pszExp, paVars[iVar].cchExp);
+                psz += paVars[iVar].cchExp;
+                *psz++ = ' ';
+                if (paVars[iVar].pszExp != paVars[iVar].pVar->value)
+                    free(paVars[iVar].pszExp);
             }
-            else
-            {
-                unsigned int cchExp;
-                paVars[iVar].pszExp = allocated_variable_expand_2(paVars[iVar].pVar->value, paVars[iVar].pVar->value_length, &cchExp);
-                paVars[iVar].cchExp = cchExp;
-            }
-            if (pDefPath)
-                kbuild_apply_defpath(pDefPath, &paVars[iVar].pszExp, &paVars[iVar].cchExp, NULL,
-                                     paVars[iVar].pszExp != paVars[iVar].pVar->value);
-            cchTotal += paVars[iVar].cchExp + 1;
+
         }
-
-        /*
-         * Construct the result value.
-         */
-        psz = pszResult = xmalloc(cchTotal + 1);
-        iVarEnd = iDirection == 1 ? cVars : 0;
-        for (iVar = iDirection == 1 ? 0 : cVars - 1; iVar != iVarEnd; iVar += iDirection)
-        {
-            if (!paVars[iVar].cchExp)
-                continue;
-            memcpy(psz, paVars[iVar].pszExp, paVars[iVar].cchExp);
-            psz += paVars[iVar].cchExp;
-            *psz++ = ' ';
-            if (paVars[iVar].pszExp != paVars[iVar].pVar->value)
-                free(paVars[iVar].pszExp);
-        }
-        if (psz != pszResult)
-            psz--;
-        *psz = '\0';
-        cchTotal = psz - pszResult;
+        assert(psz != pszResult);
+        assert(cchTotal == (size_t)(psz - pszResult));
+        psz[-1] = '\0';
+        cchTotal--;
+
         pVar = define_variable_vl(pszVarName, cchVarName, pszResult, cchTotal,
                                   0 /* take pszResult */ , o_local, 0 /* !recursive */);
@@ -2030,5 +2044,5 @@
      */
     if (pDefPath)
-        kbuild_apply_defpath(pDefPath, &pSource->value, &pSource->value_length, &pSource->value_alloc_len, 1 /* can free */);
+        kbuild_apply_defpath(pDefPath, &pSource->value, (unsigned int *)&pSource->value_length, &pSource->value_alloc_len, 1 /* can free */);
 
     /*
