[slang-users] intrinsic curl_easy_escape sample

John E. Davis davis at space.mit.edu
Wed Mar 7 22:06:31 EST 2007


Brian McQueen <mcqueenorama at gmail.com> wrote:
>Also I don't know what the hashed strings are.  Could you say a word
>about those?

The function 

    char *SLang_create_slstring (char *str)

returns a copy of "str".  It will create a new copy if it does not
already have a copy of the string in an internal hash table.  If a
copy is in the hash table, it just returns a pointer to it and updates
a reference count.  Such strings have the property that they can be
tested for equality by simply comparing the pointers:

    if (str0 == str1) ....

That is, if str0 and str1 are known to be strings created in this
manner, there is no need to call strcmp to test for equality.  Also,
such strings should be regarded as read-only--- do not modify the
their contents.  The function

    SLang_free_slstring (char *str)

must be used to free the string.

>static void escape_intrin (void)
>{
>   SLang_MMT_Type *mmt;
>   Easy_Type *ez;
>   char *unescaped_string = NULL;
>   char *esc_string = NULL;
>
>   if (-1 == SLpop_string (&unescaped_string))
>        return;

  Here, SLpop_string will return a malloced string--- not a hashed
  one.  This is useful if you intend to modify the bytes of the
  string.  From the docs, it seems that curl_easy_escape does not
  modify the string.  If this is the case, it would be better to use
  
     if (-1 == SLang_pop_slstring (&unescaped_string))
       return;

>
>   if (NULL == (mmt = pop_easy_type (&ez, 0)))
>     return;

  There is a memory leak here.  You want:
  
   if (NULL == (mmt = pop_easy_type (&ez, 0)))
     {
        SLfree (unescaped_string);  /* if SLpop_string is used */
	/* SLang_free_slstring (unescaped_string); */
	return;
     }

>   esc_string = curl_easy_escape(ez->handle, unescaped_string, 0);
>
>   if (esc_string == NULL)
>        return;

   This too exhibits the same memory leak.  Also, you should generate
   an error.  I would use:

    if (esc_string == NULL)
      {
         SLang_free_mmt (mmt);
	 SLfree (unescaped_string);
	 SLang_set_error (Curl_Error);
	 return;
      }

>   (void) SLang_push_string (esc_string);
>   SLfree(unescaped_string);
>   SLang_free_mmt (mmt);

   Since esc_string returned by curl_easy_escape is malloced, you can
   use
   
      (void) SLang_push_malloced_string (esc_string);
      SLang_free_mmt (mmt);
      
   Here, SLang_push_malloced_string will also free the string.

I hope this helps.
Feel free to submit a patch for the curl-module once you have these
functions working.

Thanks,
--John


More information about the slang-users-l mailing list