From b77b77130ad1732903236e327b6f2665c7ade200 Mon Sep 17 00:00:00 2001 From: Perry Werneck Date: Wed, 20 Nov 2019 11:38:48 -0300 Subject: [PATCH] Adjustments in selection to help the diagnose of segfaults on print dialog. --- src/include/lib3270/selection.h | 16 ++++++++++++++-- src/selection/get.c | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/src/include/lib3270/selection.h b/src/include/lib3270/selection.h index 38ddc06..55fceae 100644 --- a/src/include/lib3270/selection.h +++ b/src/include/lib3270/selection.h @@ -190,7 +190,7 @@ LIB3270_EXPORT int lib3270_get_selection_rectangle(H3270 *hSession, unsigned int *row, unsigned int *col, unsigned int *width, unsigned int *height); /** - * @brief Get selection contents. + * @brief Create a new selection block. * * @param hSession Session handle. * @param cut Non zero to clear selected contents. @@ -199,7 +199,19 @@ * @return NULL on error (sets errno), pointer to a rectangle containing the selected area (release it with lib3270_free). * */ - LIB3270_EXPORT lib3270_selection * lib3270_get_selection(H3270 *hSession, int cut, int all); + LIB3270_EXPORT lib3270_selection * lib3270_selection_new(H3270 *hSession, int cut, int all); + + LIB3270_EXPORT lib3270_selection * LIB3270_DEPRECATED(lib3270_get_selection(H3270 *hSession, int cut, int all)); + + /** + * @brief Get the length of the selection block. + * + * @param selection Selection block. + * + * @return The length of the selection block. + * + */ + LIB3270_EXPORT size_t lib3270_selection_get_length(const lib3270_selection *selection); /** * @brief Get bitmasked flag for the current selection. diff --git a/src/selection/get.c b/src/selection/get.c index b69b7c9..651e06f 100644 --- a/src/selection/get.c +++ b/src/selection/get.c @@ -31,6 +31,8 @@ #include #include #include + #include + #include #include "3270ds.h" /*--[ Implement ]------------------------------------------------------------------------------------*/ @@ -144,8 +146,23 @@ LIB3270_EXPORT char * lib3270_cut_selected(H3270 *hSession) return lib3270_get_selected_text(hSession,0,LIB3270_SELECTION_CUT); } +static size_t get_selection_length(unsigned int width, unsigned int height) +{ + return sizeof(lib3270_selection) + (sizeof(lib3270_selection_element) * ((width*height)+1)); +} + +LIB3270_EXPORT size_t lib3270_selection_get_length(const lib3270_selection *selection) +{ + return get_selection_length(selection->bounds.width,selection->bounds.height); +} + LIB3270_EXPORT lib3270_selection * lib3270_get_selection(H3270 *hSession, int cut, int all) { + return lib3270_selection_new(hSession,cut,all); +} + +LIB3270_EXPORT lib3270_selection * lib3270_selection_new(H3270 *hSession, int cut, int all) +{ if(check_online_session(hSession)) return NULL; @@ -164,7 +181,10 @@ LIB3270_EXPORT lib3270_selection * lib3270_get_selection(H3270 *hSession, int cu } // Get output buffer. - lib3270_selection * selection = lib3270_malloc(sizeof(lib3270_selection) + (sizeof(lib3270_selection_element) * (width*height))); + size_t length = get_selection_length(width, height); + lib3270_selection * selection = lib3270_malloc(length); + + memset(selection,0,length); selection->bounds.col = col; selection->bounds.row = row; @@ -172,6 +192,16 @@ LIB3270_EXPORT lib3270_selection * lib3270_get_selection(H3270 *hSession, int cu selection->bounds.width = width; selection->cursor_address = lib3270_get_cursor_address(hSession); + debug( + "width=%u height=%u length=%u (sz=%u szHeader=%u szElement=%u)", + selection->bounds.width, + selection->bounds.height, + ((selection->bounds.width * selection->bounds.height) + 1), + (unsigned int) length, + (unsigned int) sizeof(lib3270_selection), + (unsigned int) sizeof(lib3270_selection_element) + ); + unsigned int dstaddr = 0; for(row=0;row < selection->bounds.height; row++) @@ -206,5 +236,7 @@ LIB3270_EXPORT lib3270_selection * lib3270_get_selection(H3270 *hSession, int cu } + debug("dstaddr=%u length=%u",(unsigned int) dstaddr, (unsigned int) length); + return selection; } -- libgit2 0.21.2