billiob pushed a commit to branch master.

commit 034d4cf9deb1a65a13485247f61725f3c255cd82
Author: Boris Faure <[email protected]>
Date:   Mon May 20 17:39:25 2013 +0200

    termio_selection_get() set length of the string returned
    
    Also fix possible segfault in _termio_link_find().
---
 src/bin/termio.c     | 27 ++++++++++++++++-----------
 src/bin/termio.h     |  4 +++-
 src/bin/termiolink.c | 24 +++++++++++++-----------
 3 files changed, 32 insertions(+), 23 deletions(-)

diff --git a/src/bin/termio.c b/src/bin/termio.c
index 27d42ba..9c48c3e 100644
--- a/src/bin/termio.c
+++ b/src/bin/termio.c
@@ -1630,6 +1630,7 @@ _take_selection(Evas_Object *obj, Elm_Sel_Type type)
    Termio *sd = evas_object_smart_data_get(obj);
    int start_x, start_y, end_x, end_y;
    char *s;
+   size_t len;
 
    if (!sd) return;
    start_x = sd->cur.sel1.x;
@@ -1650,14 +1651,15 @@ _take_selection(Evas_Object *obj, Elm_Sel_Type type)
         sb = eina_strbuf_new();
         for (i = start_y; i <= end_y; i++)
           {
-             char *tmp = termio_selection_get(obj, start_x, i, end_x, i);
-             size_t len = strlen(tmp);
+             char *tmp = termio_selection_get(obj, start_x, i, end_x, i,
+                                              &len);
 
              eina_strbuf_append_length(sb, tmp, len);
              if (len && tmp[len - 1] != '\n')
                eina_strbuf_append_char(sb, '\n');
              free(tmp);
           }
+        len = eina_strbuf_length_get(sb);
         s = eina_strbuf_string_steal(sb);
         eina_strbuf_free(sb);
      }
@@ -1668,7 +1670,7 @@ _take_selection(Evas_Object *obj, Elm_Sel_Type type)
              INT_SWAP(start_y, end_y);
              INT_SWAP(start_x, end_x);
           }
-        s = termio_selection_get(obj, start_x, start_y, end_x, end_y);
+        s = termio_selection_get(obj, start_x, start_y, end_x, end_y, &len);
      }
 
    if (s)
@@ -1680,7 +1682,7 @@ _take_selection(Evas_Object *obj, Elm_Sel_Type type)
              sd->set_sel_at = ecore_time_get(); // hack
              sd->sel_type = type;
              elm_cnp_selection_set(sd->win, type,
-                                   ELM_SEL_FORMAT_TEXT, s, strlen(s));
+                                   ELM_SEL_FORMAT_TEXT, s, len);
              elm_cnp_selection_loss_callback_set(sd->win, type,
                                                  _lost_selection, obj);
              sd->have_sel = EINA_TRUE;
@@ -2708,16 +2710,15 @@ _selection_newline_extend_fix(Evas_Object *obj)
           {
              char *lastline;
              int x1, y1, x2, y2;
-             
+             size_t len;
+
              if (sd->cur.sel1.y == sd->cur.sel2.y) x1 = sd->cur.sel1.x;
              else x1 = 0;
              x2 = sd->cur.sel2.x;
              y1 = y2 = sd->cur.sel2.y;
-             lastline = termio_selection_get(obj, x1, y1, x2, y2);
+             lastline = termio_selection_get(obj, x1, y1, x2, y2, &len);
              if (lastline)
                {
-                  int len = strlen(lastline);
-                  
                   if ((len > 0) && (lastline[len - 1] == '\n'))
                     {
                        sd->cur.sel2.x = sd->grid.w - 1;
@@ -4049,12 +4050,14 @@ termio_theme_get(Evas_Object *obj)
 }
 
 char *
-termio_selection_get(Evas_Object *obj, int c1x, int c1y, int c2x, int c2y)
+termio_selection_get(Evas_Object *obj, int c1x, int c1y, int c2x, int c2y,
+                     size_t *len)
 {
    Termio *sd = evas_object_smart_data_get(obj);
    Eina_Strbuf *sb;
    char *s;
    int x, y;
+   size_t len_backup;
 
    if (!sd) return NULL;
    sb = eina_strbuf_new();
@@ -4139,7 +4142,7 @@ termio_selection_get(Evas_Object *obj, int c1x, int c1y, 
int c2x, int c2y)
              if (y == c2y)
                {
                   Eina_Bool have_more = EINA_FALSE;
-                  
+
                   for (x = end_x + 1; x < w; x++)
                     {
 #if defined(SUPPORT_DBLWIDTH)
@@ -4182,7 +4185,9 @@ termio_selection_get(Evas_Object *obj, int c1x, int c1y, 
int c2x, int c2y)
      }
    termpty_cellcomp_thaw(sd->pty);
 
-   if (eina_strbuf_length_get(sb) == 0)
+   if (!len) len = &len_backup;
+   *len = eina_strbuf_length_get(sb);
+   if (!*len)
      {
         eina_strbuf_free(sb);
         return NULL;
diff --git a/src/bin/termio.h b/src/bin/termio.h
index 946dc6d..0cf7572 100644
--- a/src/bin/termio.h
+++ b/src/bin/termio.h
@@ -8,7 +8,9 @@ Evas_Object *termio_add(Evas_Object *parent, Config *config, 
const char *cmd, Ei
 void         termio_win_set(Evas_Object *obj, Evas_Object *win);
 void         termio_theme_set(Evas_Object *obj, Evas_Object *theme);
 Evas_Object *termio_theme_get(Evas_Object *obj);
-char        *termio_selection_get(Evas_Object *obj, int c1x, int c1y, int c2x, 
int c2y);
+char        *termio_selection_get(Evas_Object *obj,
+                                  int c1x, int c1y, int c2x, int c2y,
+                                  size_t *len);
 void         termio_config_update(Evas_Object *obj);
 Config      *termio_config_get(const Evas_Object *obj);
 void         termio_copy_clipboard(Evas_Object *obj);
diff --git a/src/bin/termiolink.c b/src/bin/termiolink.c
index 8443153..e8c4445 100644
--- a/src/bin/termiolink.c
+++ b/src/bin/termiolink.c
@@ -95,13 +95,15 @@ _is_file(const char *str)
 }
 
 char *
-_termio_link_find(Evas_Object *obj, int cx, int cy, int *x1r, int *y1r, int 
*x2r, int *y2r)
+_termio_link_find(Evas_Object *obj, int cx, int cy,
+                  int *x1r, int *y1r, int *x2r, int *y2r)
 {
    char *s;
    char endmatch = 0;
-   int x1, x2, y1, y2, len, w = 0, h = 0, sc;
-   Eina_Bool goback = EINA_TRUE, goforward = EINA_FALSE, extend = EINA_FALSE;
-   
+   int x1, x2, y1, y2, w = 0, h = 0, sc;
+   size_t len;
+   Eina_Bool goback = EINA_TRUE, goforward = EINA_TRUE, extend = EINA_FALSE;
+
    x1 = x2 = cx;
    y1 = y2 = cy;
    termio_size_get(obj, &w, &h);
@@ -110,7 +112,7 @@ _termio_link_find(Evas_Object *obj, int cx, int cy, int 
*x1r, int *y1r, int *x2r
    if (!coord_back(&x1, &y1, w, h)) goback = EINA_FALSE;
    for (;;)
      {
-        s = termio_selection_get(obj, x1, y1 - sc, x2, y2 - sc);
+        s = termio_selection_get(obj, x1, y1 - sc, x2, y2 - sc, &len);
         if (!s) break;
         if (goback)
           {
@@ -119,14 +121,16 @@ _termio_link_find(Evas_Object *obj, int cx, int cy, int 
*x1r, int *y1r, int *x2r
                   goback = EINA_FALSE;
                   coord_back(&x1, &y1, w, h);
                   free(s);
-                  s = termio_selection_get(obj, x1, y1 - sc, x2, y2 - sc);
+                  s = termio_selection_get(obj, x1, y1 - sc, x2, y2 - sc,
+                                           &len);
                   if (!s) break;
                   if (s[0] == '"') endmatch = '"';
                   else if (s[0] == '\'') endmatch = '\'';
                   else if (s[0] == '<') endmatch = '>';
                   coord_forward(&x1, &y1, w, h);
                   free(s);
-                  s = termio_selection_get(obj, x1, y1 - sc, x2, y2 - sc);
+                  s = termio_selection_get(obj, x1, y1 - sc, x2, y2 - sc,
+                                           &len);
                   if (!s) break;
                }
              else if ((isspace(s[0])) ||
@@ -147,7 +151,7 @@ _termio_link_find(Evas_Object *obj, int cx, int cy, int 
*x1r, int *y1r, int *x2r
                        goback = EINA_FALSE;
                        coord_forward(&x1, &y1, w, h);
                     }
-                  else if (strchr((s + 2), '@'))
+                  else if (len > 2 && strchr((s + 2), '@'))
                     {
                        goback = EINA_FALSE;
                        coord_forward(&x1, &y1, w, h);
@@ -165,7 +169,6 @@ _termio_link_find(Evas_Object *obj, int cx, int cy, int 
*x1r, int *y1r, int *x2r
           }
         if (goforward)
           {
-             len = strlen(s);
              if (len > 1)
                {
                   if (((endmatch) && (s[len - 1] == endmatch)) ||
@@ -195,7 +198,7 @@ _termio_link_find(Evas_Object *obj, int cx, int cy, int 
*x1r, int *y1r, int *x2r
         if ((!goback) && (!goforward))
           {
              free(s);
-             s = termio_selection_get(obj, x1, y1 - sc, x2, y2 - sc);
+             s = termio_selection_get(obj, x1, y1 - sc, x2, y2 - sc, &len);
              break;
           }
         free(s);
@@ -203,7 +206,6 @@ _termio_link_find(Evas_Object *obj, int cx, int cy, int 
*x1r, int *y1r, int *x2r
      }
    if (s)
      {
-        len = strlen(s);
         while (len > 1)
           {
              if (isspace(s[len - 1]))

-- 

------------------------------------------------------------------------------
Try New Relic Now & We'll Send You this Cool Shirt
New Relic is the only SaaS-based application performance monitoring service 
that delivers powerful full stack analytics. Optimize and monitor your
browser, app, & servers with just a few lines of code. Try New Relic
and get this awesome Nerd Life shirt! http://p.sf.net/sfu/newrelic_d2d_may

Reply via email to