scc

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

newmap.c (654B)


      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 remap(Map *map, int n)
     13 {
     14 	size_t vsiz;
     15 
     16 	if (n > SIZE_MAX/sizeof(struct mapsec))
     17 		goto out_range;
     18 	vsiz = n * sizeof(struct mapsec);
     19 	if (vsiz > SIZE_MAX - sizeof(*map))
     20 		goto out_range;
     21 	vsiz += sizeof(*map);
     22 
     23 	if ((map = realloc(map, vsiz)) == NULL)
     24 		return NULL;
     25 
     26 	map->n = n;
     27 	return map;
     28 
     29 	return map;
     30 
     31 out_range:
     32 	errno = ERANGE;
     33 	return NULL;
     34 }
     35 
     36 Map *
     37 newmap(Map *map, int n)
     38 {
     39 	if ((map = remap(map, n)) == NULL)
     40 		return NULL;
     41 	memset(map->sec, 0, n * sizeof(struct mapsec));
     42 
     43 	return map;
     44 }