scc

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

newmap.c (592B)


      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 siz, vsiz;
     15 	struct mapsec *p;
     16 	Map *map;
     17 
     18 	siz = sizeof(*map);
     19 	if (n > SIZE_MAX/sizeof(struct mapsec))
     20 		goto out_range;
     21 
     22 	vsiz = n * sizeof(struct mapsec);
     23 	if (vsiz > SIZE_MAX - siz)
     24 		goto out_range;
     25 
     26 	if ((map = malloc(siz + vsiz)) == NULL)
     27 		return NULL;
     28 
     29 	map->n = n;
     30 	memset(map->sec, 0, vsiz);
     31 
     32 	for (p = map->sec; n--; ++p)
     33 		p->fp = fp;
     34 	return map;
     35 
     36 out_range:
     37 		errno = ERANGE;
     38 		return NULL;
     39 }