Changeset b164303 in rtems


Ignore:
Timestamp:
11/25/14 21:55:49 (9 years ago)
Author:
Josh Oguin <josh.oguin@…>
Branches:
4.11, 5, master
Children:
038faca1
Parents:
d4ec0a2
git-author:
Josh Oguin <josh.oguin@…> (11/25/14 21:55:49)
git-committer:
Joel Sherrill <joel.sherrill@…> (11/26/14 13:52:00)
Message:

tools/build/*.c: Clean up issues reported by CodeSonar?

This code is built without warnings and ignored by Coverity Scan.
CodeSonar? found a wide range of issues including buffer overruns,
buffer underruns, questionable type conversions, leaks, etc. This
set of patches addresses all reported issues.

Location:
tools/build
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • tools/build/binpatch.c

    rd4ec0a2 rb164303  
    1 
    2 #include <stdio.h>
    3 #include <stdlib.h>
    4 
    51/*
    62 * This function will patch binary file
    73 */
    84
     5#include <stdio.h>
     6#include <stdlib.h>
     7#include <assert.h>
    98
    109static char buf[512];
     
    2625  printf("<byte2>  - byte 1 of patch\n");
    2726  printf("<byte3>  - byte 1 of patch\n");
    28 
    29   return;
    3027}
    3128
     
    3330main(int argc, char **argv)
    3431{
    35   int   c;
     32  int    c;
    3633  FILE  *ofp, *ifp;
    37   char  patch[4], *end;
    38   int   patchLen, tmp, i, off, cnt, patched, len, reloc;
    39 
     34  char  *patch, *end;
     35  int    patchLen, tmp, i, off, cnt, patched, len, reloc;
    4036
    4137  /* parse command line options */
    42   while ((c = getopt(argc, argv, "h")) >= 0)
    43     {
    44       switch (c)
    45         {
    46         case 'h':
    47           usage();
    48           return 0;
    49         default:
    50           usage();
    51           return 1;
    52         }
    53     }
     38  while ((c = getopt(argc, argv, "h")) >= 0) {
     39      switch (c) {
     40        case 'h':
     41          usage();
     42          return 0;
     43        default:
     44          usage();
     45          return 1;
     46        }
     47  }
    5448
    55   if(argc < 6)
    56     {
     49  if(argc < 6) {
    5750      usage();
    5851      return 1;
    59     }
     52  }
    6053
    6154  /* Let us get offset in file */
    6255  reloc = strtol(argv[3], &end, 0);
    63   if(end == argv[3] || off < 0)
    64     {
     56  if (end == argv[3] || reloc < 0) {
    6557      fprintf(stderr, "bad reloc value %s\n", argv[3]);
    6658      return 1;
    67     }
     59  }
    6860
    6961  off = strtol(argv[4], &end, 0);
    70   if(end == argv[4] || off < 0 || off < reloc)
    71     {
     62  if (end == argv[4] || off < 0 || off < reloc) {
    7263      fprintf(stderr, "bad offset value %s\n", argv[4]);
    7364      return 1;
    74     }
     65  }
    7566
    7667  off -= reloc;
     
    7869  /* Let us get patch  */
    7970  patchLen = argc - 5;
     71  patch = calloc( patchLen, sizeof(char) );
     72  assert( patch );
    8073
    81   for(i=0; i<patchLen; i++)
    82     {
     74  for (i=0; i<patchLen; i++) {
    8375      tmp = strtol(argv[5+i], &end, 0);
    8476
    85       if(end == argv[4+i] || tmp < 0 || tmp > 0xff)
    86         {
    87           fprintf(stderr, "bad byte value %s\n", argv[5+i]);
    88           return 1;
    89         }
    90       patch[i] = tmp;
    91     }
     77      if (end == argv[4+i]) {
     78          fprintf(stderr, "bad byte value %s\n", argv[5+i]);
     79          free(patch);
     80          return 1;
     81      }
     82      if (tmp < 0 || tmp > 0xff) {
     83          fprintf(stderr, "not a byte value %s\n", argv[5+i]);
     84          free(patch);
     85          return 1;
     86      }
     87      patch[i] = (tmp & 0xff);
     88  }
    9289
    9390  ifp = fopen(argv[2], "r");
    94   if(ifp == NULL)
    95     {
     91  if (ifp == NULL) {
    9692      fprintf(stderr, "unable to open file %s\n", argv[2]);
     93      free(patch);
    9794      return 1;
    98     }
     95  }
    9996
    10097  ofp = fopen(argv[1], "w");
    101   if(ofp == NULL)
    102     {
     98  if (ofp == NULL) {
    10399      fprintf(stderr, "unable to open file %s\n", argv[1]);
     100      fclose(ifp);
     101      free(patch);
    104102      return 1;
    105     }
     103  }
    106104
    107105  cnt     = 0;
    108106  patched = 0;
    109   for(;;)
    110     {
     107  for(;;) {
    111108      len = fread(buf, 1, sizeof(buf), ifp);
    112109
    113       if(len == 0)
    114         {
    115           break;
    116         }
     110      if(len == 0) {
     111          break;
     112      }
    117113
    118       if(cnt <= off && (cnt + len) > off)
    119         {
    120           /* Perform patch */
    121           for(i=0; i<patchLen && (off+i)<(cnt+len); i++)
    122             {
    123               buf[off-cnt+i] = patch[i];
    124             }
    125           patched = 1;
    126         }
    127       else if(cnt > off && cnt < (off + patchLen))
    128         {
    129           /* Perform patch */
    130           for(i=cnt-off; i<patchLen; i++)
    131             {
    132               buf[off-cnt+i] = patch[i];
    133             }
    134           patched = 1;
    135         }
     114      if (cnt <= off && (cnt + len) > off) {
     115          /* Perform patch */
     116          for(i=0; i<patchLen && (off+i)<(cnt+len); i++)
     117            {
     118              buf[off-cnt+i] = patch[i];
     119            }
     120          patched = 1;
     121      } else if(cnt > off && cnt < (off + patchLen)) {
     122          /* Perform patch */
     123          for(i=cnt-off; i<patchLen; i++) {
     124              buf[off-cnt+i] = patch[i];
     125          }
     126          patched = 1;
     127      }
    136128
    137129      fwrite(buf, 1, len, ofp);
    138130
    139131      cnt += len;
    140     }
     132  }
    141133
    142134  fclose(ifp);
    143135  fclose(ofp);
     136  free(patch);
    144137
    145   if(!patched)
    146     {
     138  if (!patched) {
    147139      fprintf(stderr, "warning: offset is beyond input file length\n");
    148140      fprintf(stderr, "         no patch is performed\n");
    149     }
     141  }
    150142
    151143  return 0;
  • tools/build/cklength.c

    rd4ec0a2 rb164303  
    126126    opterr = 0;                                 /* we'll report all errors */
    127127    while ((c = getopt(argc, argv, GETOPTARGS)) != EOF)
    128         switch (c)
    129         {
     128        switch (c) {
    130129            case 'l':                            /* line length */
    131130                line_length = atoi( optarg );
     
    151150        }
    152151
    153     if (showusage)
    154     {
     152    if (showusage) {
    155153        (void) fprintf(stderr, "%s", USAGE);
    156154        exit(1);
     
    161159     */
    162160
    163     for ( ; argv[optind]; optind++)
     161    for ( ; optind < argc; optind++) {
    164162        if (Failed(process(argv[optind])))
    165163            rc = FAILURE;
     164    }
    166165
    167166    return rc;
     
    269268    (void) fflush(stderr);
    270269
    271     if (error_flag & (ERR_FATAL | ERR_ABORT))
    272     {
    273         if (error_flag & ERR_FATAL)
    274         {
     270    if (error_flag & (ERR_FATAL | ERR_ABORT)) {
     271        if (error_flag & ERR_FATAL) {
    275272            error(0, "fatal error, exiting");
    276273            exit(local_errno ? local_errno : 1);
    277         }
    278         else
    279         {
     274        } else {
    280275            error(0, "fatal error, aborting");
    281276            abort();
     
    292287    long val;
    293288
    294     if ( ! strchr("0123456789-", *s))
    295     {
     289    if ( !strchr("0123456789-", *s) ) {
    296290        error(ERR_FATAL, "'%s' is not a number", s);
    297         return min;
     291        /* does not return */
    298292    }
    299293
    300294    val = strtol(s, (char **) NULL, 0);
    301     if ((val < min) || (val > max))
    302     {
     295    if ((val < min) || (val > max)) {
    303296        if (min == max)
    304297            error(ERR_FATAL, "%s can only be %ld", s, min);
    305298        else
    306299            error(ERR_FATAL, "%s must be between %ld and %ld", msg, min, max);
     300        /* does not return */
    307301    }
    308302
  • tools/build/eolstrip.c

    rd4ec0a2 rb164303  
    190190     *  Don't count the carriage return.
    191191     */
    192 
    193     length = strlen( buffer ) - 1;
     192    length = 0;
     193    if ( *buffer != '\0' )
     194      length = strnlen( buffer, BUFFER_SIZE ) - 1;
    194195
    195196    if ( buffer[ length ] != '\n' )
    196197      error(ERR_ERRNO|ERR_FATAL, "Line %d too long in %s\n", line_number, arg);
    197198
    198     while ( isspace( (unsigned char) buffer[ length ] ) )
     199    while ( length && isspace( (unsigned char) buffer[ length ] ) )
    199200      buffer[ length-- ] = '\0';
    200201
     
    209210  fclose( in );
    210211  if ( !test_only ) {
    211     fclose( out );
    212     rename( outname, arg );
     212    if (out) fclose( out );
     213    rc = rename( outname, arg );
     214    if ( rc != 0 ) {
     215      fprintf( stderr, "Unable to rename %s to %s\n", outname, arg );
     216    }
    213217  }
    214218  return rc;
  • tools/build/rtems-bin2c.c

    rd4ec0a2 rb164303  
    5656{
    5757  FILE *ifile, *ocfile, *ohfile;
    58   char buf[PATH_MAX], *p;
    59   char obasename[PATH_MAX];
    60   char ocname[PATH_MAX];
    61   char ohname[PATH_MAX];
     58  char buf[PATH_MAX+1], *p;
     59  char obasename[PATH_MAX+1];
     60  char ocname[PATH_MAX+1];
     61  char ohname[PATH_MAX+1];
    6262  const char *cp;
    6363  size_t len;
     64
     65  ocfile = NULL;
     66  ohfile = NULL;
    6467
    6568  /* Error check */
     
    101104
    102105  if ( createC ) {
    103   ocfile = fopen(ocname, "wb");
    104   if (ocfile == NULL) {
    105     fprintf(stderr, "cannot open %s for writing\n", ocname);
    106     exit(1);
    107   }
     106    ocfile = fopen(ocname, "wb");
     107    if (ocfile == NULL) {
     108      fprintf(stderr, "cannot open %s for writing\n", ocname);
     109      exit(1);
     110    }
    108111  }
    109112
    110113  if ( createH ) {
    111   ohfile = fopen(ohname, "wb");
    112   if (ohfile == NULL) {
    113     fprintf(stderr, "cannot open %s for writing\n", ohname);
    114     exit(1);
    115   }
     114    ohfile = fopen(ohname, "wb");
     115    if (ohfile == NULL) {
     116      fprintf(stderr, "cannot open %s for writing\n", ohname);
     117      exit(1);
     118    }
    116119  }
    117120
    118121  /* find basename */
    119122  char *ifbasename = strdup(ifname);
     123  if ( ifbasename == NULL ) {
     124    fprintf(stderr, "cannot allocate memory\n" );
     125    fclose(ifile);
     126    if ( createC ) { fclose(ocfile); }
     127    if ( createH ) { fclose(ohfile); }
     128    exit(1);
     129  }
     130
    120131  ifbasename = basename(ifbasename);
    121132
    122133  strcpy(buf, ifbasename);
    123   for (p = buf; *p != '\0'; ++p)
    124     if (!isalnum(*p))
     134  for (p = buf; *p != '\0'; ++p) {
     135    if (!isalnum((unsigned char)*p)) /* cast to avoid negative indexing */
    125136      *p = '_';
     137  }
    126138
    127139  if ( createC ) {
    128   /* print C file header */
    129   fprintf(
    130     ocfile,
    131     "/*\n"
    132     " *  Declarations for C structure representing binary file %s\n"
    133     " *\n"
    134     " *  WARNING: Automatically generated -- do not edit!\n"
    135     " */\n"
    136     "\n"
    137     "#include <sys/types.h>\n"
    138     "\n",
    139     ifbasename
    140   );
    141 
    142   /* print structure */
    143   fprintf(
    144     ocfile,
    145     "%s%sunsigned char %s[] = {\n  ",
    146     ((usestatic) ? "static " : ""),
    147     ((useconst) ? "const " : ""),
    148     buf
    149   );
    150   int c, col = 1;
    151   while ((c = myfgetc(ifile)) != EOF) {
    152     if (col >= 78 - 6) {
    153       fprintf(ocfile, "\n  ");
    154       col = 1;
    155     }
    156     fprintf(ocfile, "0x%.2x, ", c);
    157     col += 6;
    158 
    159   }
    160   fprintf(ocfile, "\n};\n");
    161 
    162   /* print sizeof */
    163   fprintf(
    164     ocfile,
    165     "\n"
    166     "%s%ssize_t %s_size = sizeof(%s);\n",
    167     ((usestatic) ? "static " : ""),
    168     ((useconst) ? "const " : ""),
    169     buf,
    170     buf
    171   );
     140    /* print C file header */
     141    fprintf(
     142      ocfile,
     143      "/*\n"
     144      " *  Declarations for C structure representing binary file %s\n"
     145      " *\n"
     146      " *  WARNING: Automatically generated -- do not edit!\n"
     147      " */\n"
     148      "\n"
     149      "#include <sys/types.h>\n"
     150      "\n",
     151      ifbasename
     152    );
     153
     154    /* print structure */
     155    fprintf(
     156      ocfile,
     157      "%s%sunsigned char %s[] = {\n  ",
     158      ((usestatic) ? "static " : ""),
     159      ((useconst) ? "const " : ""),
     160      buf
     161    );
     162    int c, col = 1;
     163    while ((c = myfgetc(ifile)) != EOF) {
     164      if (col >= 78 - 6) {
     165        fprintf(ocfile, "\n  ");
     166        col = 1;
     167      }
     168      fprintf(ocfile, "0x%.2x, ", c);
     169      col += 6;
     170
     171    }
     172    fprintf(ocfile, "\n};\n");
     173
     174    /* print sizeof */
     175    fprintf(
     176      ocfile,
     177      "\n"
     178      "%s%ssize_t %s_size = sizeof(%s);\n",
     179      ((usestatic) ? "static " : ""),
     180      ((useconst) ? "const " : ""),
     181      buf,
     182      buf
     183    );
    172184  } /* createC */
    173185
     
    177189
    178190  if ( createH ) {
    179   /* print H file header */
    180   char hbasename[PATH_MAX];
    181   char* p;
    182   /* Clean up the file name if it is an abs path */
    183   strcpy(
    184     hbasename,
    185     obasename
    186   );
    187   p = hbasename;
    188   while (*p != '\0') {
    189     if (*p < '0' || *p > 'z')
    190       *p = '_';
    191     ++p;
    192   }
    193   fprintf(
    194     ohfile,
    195     "/*\n"
    196     " *  Extern declarations for C structure representing binary file %s\n"
    197     " *\n"
    198     " *  WARNING: Automatically generated -- do not edit!\n"
    199     " */\n"
    200     "\n"
    201     "#ifndef __%s_h\n"
    202     "#define __%s_h\n"
    203     "\n"
    204     "#include <sys/types.h>\n"
    205     "\n",
    206     ifbasename,  /* header */
    207     hbasename,  /* ifndef */
    208     hbasename   /* define */
    209   );
    210 
    211   /* print structure */
    212   fprintf(
    213     ohfile,
    214     "extern %s%sunsigned char %s[];",
    215     ((usestatic) ? "static " : ""),
    216     ((useconst) ? "const " : ""),
    217     buf
    218   );
    219   /* print sizeof */
    220   fprintf(
    221     ohfile,
    222     "\n"
    223     "extern %s%ssize_t %s_size;\n",
    224     ((usestatic) ? "static " : ""),
    225     ((useconst) ? "const " : ""),
    226     buf
    227   );
    228 
    229   fprintf(
    230     ohfile,
    231     "\n"
    232     "#endif\n"
    233   );
     191    /* print H file header */
     192    char hbasename[PATH_MAX];
     193    char* p;
     194    /* Clean up the file name if it is an abs path */
     195    strcpy(
     196      hbasename,
     197      obasename
     198    );
     199    p = hbasename;
     200    while (*p != '\0') {
     201      if (*p < '0' || *p > 'z')
     202        *p = '_';
     203      ++p;
     204    }
     205    fprintf(
     206      ohfile,
     207      "/*\n"
     208      " *  Extern declarations for C structure representing binary file %s\n"
     209      " *\n"
     210      " *  WARNING: Automatically generated -- do not edit!\n"
     211      " */\n"
     212      "\n"
     213      "#ifndef __%s_h\n"
     214      "#define __%s_h\n"
     215      "\n"
     216      "#include <sys/types.h>\n"
     217      "\n",
     218      ifbasename,  /* header */
     219      hbasename,  /* ifndef */
     220      hbasename   /* define */
     221    );
     222
     223    /* print structure */
     224    fprintf(
     225      ohfile,
     226      "extern %s%sunsigned char %s[];",
     227      ((usestatic) ? "static " : ""),
     228      ((useconst) ? "const " : ""),
     229      buf
     230    );
     231    /* print sizeof */
     232    fprintf(
     233      ohfile,
     234      "\n"
     235      "extern %s%ssize_t %s_size;\n",
     236      ((usestatic) ? "static " : ""),
     237      ((useconst) ? "const " : ""),
     238      buf
     239    );
     240
     241    fprintf(
     242      ohfile,
     243      "\n"
     244      "#endif\n"
     245    );
    234246  } /* createH */
    235247
     
    241253  if ( createC ) { fclose(ocfile); }
    242254  if ( createH ) { fclose(ohfile); }
     255  free(ifbasename);
    243256}
    244257
  • tools/build/unhex.c

    rd4ec0a2 rb164303  
    3737#include <stdarg.h>
    3838#include <errno.h>
     39#include <assert.h>
    3940
    4041#include "config.h"
     
    7273 */
    7374
    74 #define B0(x)       ((x) & 0xff)
    75 #define B1(x)       B0((x) >> 8)
    76 #define B2(x)       B0((x) >> 16)
    77 #define B3(x)       B0((x) >> 24)
     75#define B0(x)       (((u32)x) & 0xff)
     76#define B1(x)       B0(((u32)x) >> 8)
     77#define B2(x)       B0(((u32)x) >> 16)
     78#define B3(x)       B0(((u32)x) >> 24)
    7879
    7980typedef struct buffer_rec {
     
    120121long  getNbytes(char **p, int n);
    121122void  badformat(char *s, char *fname, char *msg);
    122 
    123 #define get1bytes(p)    ((int) getbyte(p))
    124 #define get2bytes(p)    ((int) getNbytes(p, 2))
     123void  fix_string_from_gets(char *s, size_t max);
     124
     125#define get1bytes(p)    (getbyte(p))
     126#define get2bytes(p)    getNbytes(p, 2)
    125127#define get3bytes(p)    getNbytes(p, 3)
    126128#define get4bytes(p)    getNbytes(p, 4)
     
    141143)
    142144{
    143     register int c;
     145    int c;
    144146    bool showusage = FALSE;                     /* usage error? */
    145147    int rc = 0;
    146148    FILE *outfp, *infp;
     149    long base_long = 0;
    147150
    148151    /*
    149152     * figure out invocation leaf-name
    150153     */
    151 
    152154    if ((progname = strrchr(argv[0], '/')) == (char *) NULL)
    153155        progname = argv[0];
     
    160162     *  Check options and arguments.
    161163     */
    162 
    163164    progname = argv[0];
    164     while ((c = getopt(argc, argv, "F:a:o:vl")) != EOF)
    165         switch (c)
    166         {
     165    while ((c = getopt(argc, argv, "F:a:o:vl")) != EOF) {
     166        switch (c) {
    167167            case 'a':                           /* base address */
    168                 base = stol(optarg);
     168                base_long = stol(optarg);
     169                if (base_long < 0) {
     170                    (void) fprintf(stderr, "%s", USAGE);
     171                    (void) fprintf(stderr, "\n*** base is an illegal value\n" );
     172                    exit(1);
     173                }
     174                base = (u32) base_long;
     175
    169176                break;
    170177
     
    188195                showusage = TRUE;
    189196        }
    190 
    191     if (showusage)
    192     {
     197    }
     198
     199    if (showusage) {
    193200        (void) fprintf(stderr, "%s", USAGE);
    194201        exit(1);
    195202    }
    196203
    197     if (linear && (base != 0))
    198     {
     204    if (linear && (base != 0)) {
    199205        error(0, "-l and -a may not be specified in combination");
    200206        exit(1);
    201207    }
    202208
    203     if (STREQ(outfilename, "-"))
    204     {
     209    if (STREQ(outfilename, "-")) {
    205210        outfp = stdout;
    206211        outfilename = "stdout";
    207     }
    208     else
    209         if ((outfp = fopen(outfilename, "w")) == (FILE *) NULL)
    210         {
    211             error(-1, "couldn't open '%s' for output", outfilename);
    212             exit(1);
    213         }
     212    } else if ((outfp = fopen(outfilename, "w")) == (FILE *) NULL) {
     213        error(-1, "couldn't open '%s' for output", outfilename);
     214        exit(1);
     215    }
    214216
    215217    /*
    216218     * Now process the input files (or stdin, if none specified)
    217219     */
    218 
    219     if (argv[optind] == (char *) NULL)          /* just stdin */
     220    if (argc == optind)          /* just stdin */
    220221        exit(unhex(stdin, "stdin", outfp, outfilename));
    221222    else
    222         for (; (optarg = argv[optind]); optind++)
    223         {
    224             if (STREQ(optarg, "-"))
     223        for (; optind < argc ; optind++) {
     224            optarg = argv[optind];
     225            if (STREQ(optarg, "-")) {
    225226                rc += unhex(stdin, "stdin", outfp, outfilename);
    226             else
    227             {
    228                 if ((infp = fopen(optarg, "r")) == (FILE *) NULL)
    229                 {
     227            } else {
     228                if ((infp = fopen(optarg, "r")) == (FILE *) NULL) {
    230229                    error(-1, "couldn't open '%s' for input", optarg);
    231230                    exit(1);
    232231                }
    233232                rc += unhex(infp, optarg, outfp, outfilename);
     233                fclose(infp);
    234234            }
    235235        }
    236236
     237    fclose(outfp);
    237238    return(rc);
    238239}
     
    261262     */
    262263
    263     if (FFfill)
    264     {
     264    if (FFfill) {
    265265        (void) fseek(ofp, 0, 0);
    266266        for (c = FFfill; c > 0; c--)
     
    272272     */
    273273
    274     if ((c = getc(ifp)) != EOF)
    275     {
     274    if ((c = getc(ifp)) != EOF) {
    276275        ungetc(c, ifp);
    277         switch(c)
    278         {
     276        switch(c) {
    279277            case 'S':
    280278                convert_S_records(ifp, inm, ofp, onm);
     
    293291            {
    294292                char tmp[2];
    295                 tmp[0] = c; tmp[1] = 0;
     293                tmp[0] = (char) (c & 0x7f);
     294                tmp[1] = 0;
    296295                badformat(tmp, inm, BADFMT);
    297296            }
     
    318317    int c;
    319318    int rectype;                    /* record type */
    320     int len;                        /* data length of current line */
     319    long len;                        /* data length of current line */
    321320    u32 addr;
    322321    u32 base_address = 0;
    323322    bool endrecord = FALSE;
    324323    buffer_rec tb;
    325 
    326     while ( ! endrecord && (fgets(buff, sizeof(buff), ifp)))
    327     {
     324    long getBytesStatus;
     325
     326    while ( !endrecord && (fgets(buff, sizeof(buff), ifp)) ) {
    328327        p = &buff[0];
    329328
    330         if (p[strlen(p)-1] == '\n')                 /* get rid of newline */
    331             p[strlen(p)-1] = '\0';
    332 
    333         if (p[strlen(p)-1] == '\r')                 /* get rid of any CR */
    334             p[strlen(p)-1] = '\0';
     329        fix_string_from_gets(p, sizeof(buff));
    335330
    336331        tb.dl_count = 0;
     
    343338            badformat(buff, inm, BADLEN);
    344339
    345         if ((addr = get2bytes(&p)) == -1L)          /* record addr */
     340        if ((getBytesStatus = get2bytes(&p)) == -1L)          /* record addr */
    346341            badformat(buff, inm, BADADDR);
     342        addr = (u32) getBytesStatus;
    347343
    348344        rectype = getbyte(&p);
     
    350346        cksum = len + B0(addr) + B1(addr) + rectype;
    351347
    352         switch (rectype)
    353         {
     348        switch (rectype) {
    354349            case 0x00:                  /* normal data record */
    355                 tb.dl_destaddr = base_address + addr;
     350                tb.dl_destaddr = (u32) base_address + addr;
    356351                while (len--)
    357352                {
     
    370365
    371366            case 0x02:                  /* new base */
    372                 if ((base_address = get2bytes(&p)) == -1L)
     367            {
     368                long blong;
     369                if ((blong = get2bytes(&p)) == -1L)
    373370                    badformat(buff, inm, BADBASE);
     371                base_address = (u32) blong;
    374372                cksum += B0(base_address) + B1(base_address);
    375373                base_address <<= 4;
    376374                break;
    377 
     375            }
    378376            case 0x03:                  /* seg/off execution start address */
    379377            {
    380                 u32 seg, off;
     378                long seg, off;
    381379
    382380                seg = get2bytes(&p);
     
    424422    int incksum;
    425423    int c;
    426     int len;                        /* data length of current line */
     424    long len;                        /* data length of current line */
    427425    int rectype;                    /* record type */
    428     u32 addr;
     426    long addr;
    429427    bool endrecord = FALSE;
    430428    buffer_rec tb;
    431429
    432     while ( ! endrecord && (fgets(buff, sizeof(buff), ifp)))
    433     {
     430    while ( !endrecord && (fgets(buff, sizeof(buff), ifp)) ) {
    434431        p = &buff[0];
    435432
    436         if (p[strlen(p)-1] == '\n')                 /* get rid of newline */
    437             p[strlen(p)-1] = '\0';
    438 
    439         if (p[strlen(p)-1] == '\r')                 /* get rid of any CR */
    440             p[strlen(p)-1] = '\0';
     433        fix_string_from_gets(p, sizeof(buff));
    441434
    442435        tb.dl_count = 0;
     
    453446        cksum = len;
    454447
    455         switch (rectype)
    456         {
     448        switch (rectype) {
    457449            case 0x00:                  /* comment field, ignored */
    458450                goto write_it;
     
    477469                cksum += B0(addr) + B1(addr) + B2(addr) + B3(addr);
    478470
    479                 tb.dl_destaddr = addr;
    480                 while (len--)
    481                 {
     471                tb.dl_destaddr = (u32) addr;
     472                while (len--) {
    482473                    if ((c = getbyte(&p)) == -1)
    483474                        badformat(buff, inm, BADDATA);
     
    544535    buffer_rec tb;
    545536
    546     while ( ! endrecord && (fgets(buff, sizeof(buff), ifp)))
    547     {
     537    while ( ! endrecord && (fgets(buff, sizeof(buff), ifp))) {
    548538        p = &buff[0];
    549         if (p[strlen(p)-1] == '\n')                 /* get rid of newline */
    550             p[strlen(p)-1] = '\0';
    551 
    552         if (p[strlen(p)-1] == '\r')                 /* get rid of any CR */
    553             p[strlen(p)-1] = '\0';
     539        fix_string_from_gets(p, sizeof(buff));
    554540
    555541        tb.dl_count = 0;
    556542
    557543        eol = FALSE;
    558         while ( ! eol && ! endrecord)
    559         {
    560             switch (*p++)
    561             {
     544        while ( ! eol && ! endrecord) {
     545            switch (*p++) {
    562546                case '9':
    563547                    if (tb.dl_count)
    564548                        write_record(&tb, ofp);
    565                     tb.dl_destaddr = get2bytes(&p);
     549                    tb.dl_destaddr = (u32) get2bytes(&p);
    566550                    break;
    567551
    568552                case 'B':
    569553                    c = getbyte(&p);
     554                    assert( c != -1 );
    570555                    filesum += c;
    571556                    tb.dl_buf[tb.dl_count++] = c;
     557
    572558                    c = getbyte(&p);
     559                    assert( c != -1 );
    573560                    filesum += c;
    574561                    tb.dl_buf[tb.dl_count++] = c;
     
    597584             FILE *fp)
    598585{
    599     if ( ! linear)
    600     {
     586    if ( ! linear) {
    601587        if (tb->dl_destaddr < base)
    602588            error(ERR_FATAL, "record at address 0x%x precedes base of 0x%x",
     
    616602
    617603    **p = toupper(**p);
    618     switch (**p)
    619     {
     604    switch (**p) {
    620605        case '0': case '1': case '2': case '3': case '4':
    621606        case '5': case '6': case '7': case '8': case '9':
     
    652637          int n)
    653638{
    654     int t;
     639    long t;
    655640    u32 val = 0;
    656641
    657     while (n--)
    658     {
     642    while (n--) {
    659643        if ((t = getbyte(p)) == -1)
    660644            return(-1L);
    661645        val <<= 8;
    662         val += t;
     646        val += (u32) t;
    663647    }
    664648
     
    671655          char *msg)
    672656{
    673     if (s[strlen(s)-1] == '\n')             /* get rid of newline */
     657    if ( *s != '\0' ) {
     658      if (s[strlen(s)-1] == '\n')             /* get rid of newline */
    674659        s[strlen(s)-1] = '\0';
     660    }
    675661    error(0, "line '%s'::\n\tfrom file '%s'; %s", s, fname, msg);
    676662    exit(1);
     
    720706    (void) fflush(stderr);
    721707
    722     if (error_flag & (ERR_FATAL | ERR_ABORT))
    723     {
    724         if (error_flag & ERR_FATAL)
    725         {
     708    if (error_flag & (ERR_FATAL | ERR_ABORT)) {
     709        if (error_flag & ERR_FATAL) {
    726710            error(0, "fatal error, exiting");
    727711            exit(local_errno ? local_errno : 1);
    728         }
    729         else
    730         {
     712        } else {
    731713            error(0, "fatal error, aborting");
    732714            abort();
     
    734716    }
    735717}
     718
     719void  fix_string_from_gets(char *s, size_t max)
     720{
     721  size_t len;
     722  assert( s );
     723
     724  if ( *s == '\0' )
     725    return;
     726
     727  len = strnlen( s, max );
     728
     729  if ( s[len - 1] == '\n')                 /* get rid of newline */
     730      s[len - 1] = '\0';
     731
     732  if ( s[len - 1] == '\r')                 /* get rid of any CR */
     733      s[len - 1] = '\0';
     734
     735}
Note: See TracChangeset for help on using the changeset viewer.