scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | README | LICENSE

newmap.c (589B)


      1 #include <errno.h>
      2 #include <stdint.h>
      3 #include <stdlib.h>
      4 #include <stdio.h>
      5 #include <string.h>
      6 
      7 #include <scc/mach.h>
      8 
      9 #include "libmach.h"
     10 
     11 Map *
     12 newmap(int n, FILE *fp)
     13 {
     14 	size_t vsiz;
     15 	struct mapsec *p;
     16 	Map *map;
     17 
     18 	if (n > SIZE_MAX/sizeof(struct mapsec))
     19 		goto out_range;
     20 	vsiz = n * sizeof(struct mapsec);
     21 	if (vsiz > SIZE_MAX - sizeof(*map))
     22 		goto out_range;
     23 	vsiz += sizeof(*map);
     24 
     25 	if ((map = malloc(vsiz)) == NULL)
     26 		return NULL;
     27 
     28 	map->n = n;
     29 	memset(map->sec, 0, vsiz);
     30 
     31 	for (p = map->sec; n--; ++p)
     32 		p->fp = fp;
     33 	return map;
     34 
     35 out_range:
     36 	errno = ERANGE;
     37 	return NULL;
     38 }