Skip to content

Commit

Permalink
ccdpack: Fix some pointer comparisons
Browse files Browse the repository at this point in the history
It seems that on my Mac (at least):

    if ( ++c >= retbuf + BUFLENG ) {

comes out true even when `c` and `retbuf` differ by 1 with `c`
less than `retbuf`. It looks like this is some issues with
64-bit unsigned types and signed ints. Recasting it explicitly
to use a ptrdiff_t type fixes the problem:

    c++;
    if ( (ptrdiff_t)(c - retbuf) >= BUFLENG ) {

Before this fix pairndf was failing even before it started with
a buffer overflow in the Tcl communication system.

After this patch pairndf at least throws up a couple of images
although I'm not sure what I'm supposed to do with them once
they are up.
  • Loading branch information
timj committed Aug 29, 2014
1 parent b0e0370 commit 66f524c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
5 changes: 4 additions & 1 deletion applications/ccdpack/ccdwish.c
Expand Up @@ -91,6 +91,8 @@
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <stddef.h>

#include "tcl.h"
#include "tk.h"
#include "mers.h"
Expand Down Expand Up @@ -422,7 +424,8 @@ extern F77_SUBROUTINE(ccd1_linflt)();
c = buffer - 1;
do {
int bytes;
if ( ++c >= buffer + BUFLENG ) {
c++;
if ( (ptrdiff_t)(c - buffer) >= BUFLENG ) {
strcpy( buffer, "Buffer overflow in Tcl process\n" );
write( ofd, buffer, strlen( buffer ) );
exit( 1 );
Expand Down
6 changes: 4 additions & 2 deletions applications/ccdpack/main/tcltalk.c
Expand Up @@ -81,7 +81,8 @@
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <errno.h>
#include <stddef.h>

#include "tcl.h"
#include "sae_par.h"
#include "tcltalk.h"
Expand Down Expand Up @@ -369,7 +370,8 @@
c = retbuf - 1;
*retbuf = '\0';
do {
if ( ++c >= retbuf + BUFLENG ) {
c++;
if ( (ptrdiff_t)(c - retbuf) >= BUFLENG ) {
*status = SAI__ERROR;
errRep( "CCD_TCL_BUF", "Buffer overflow", status );
return NULL;
Expand Down

0 comments on commit 66f524c

Please sign in to comment.