Commit edc121a5e9dfe22cf299a1458ecb386249d08431

Authored by perry.werneck@gmail.com
1 parent 0050d757

Corrigindo segfault em windows

Showing 1 changed file with 15 additions and 4 deletions   Show diff stats
selection.c
... ... @@ -317,12 +317,13 @@ static char * get_text(H3270 *hSession,unsigned char all)
317 317 {
318 318 int row, col, baddr;
319 319 char *ret;
320   - size_t sz = 0;
  320 + size_t buflen = (hSession->rows * (hSession->cols+1))+1;
  321 + size_t sz = 0;
321 322  
322 323 if(!lib3270_connected(hSession))
323 324 return NULL;
324 325  
325   - ret = malloc(hSession->rows * (hSession->cols+1));
  326 + ret = malloc(buflen);
326 327  
327 328 baddr = 0;
328 329 for(row=0;row < hSession->rows;row++)
... ... @@ -342,9 +343,19 @@ static char * get_text(H3270 *hSession,unsigned char all)
342 343 if(cr)
343 344 ret[sz++] = '\n';
344 345 }
345   - ret[sz] = 0;
346 346  
347   - return realloc(ret,sz+1);
  347 + if(!sz)
  348 + {
  349 + free(ret);
  350 + return NULL;
  351 + }
  352 +
  353 + ret[sz++] = 0;
  354 +
  355 + if(sz != buflen)
  356 + ret = realloc(ret,sz);
  357 +
  358 + return ret;
348 359 }
349 360  
350 361 LIB3270_EXPORT char * lib3270_get_text(H3270 *hSession)
... ...