gentoo: use /var/run for new /run compatibility
[util-vserver.git] / src / h2ext.c
index d1050c8..13366df 100644 (file)
@@ -45,6 +45,8 @@
 
 #define MAX_PEEK_SIZE 4096
 #define MIN(a,b)  (((a) > (b)) ? (b) : (a))
+#define STRINGIFY_(x)  #x
+#define STRINGIFY(x)   STRINGIFY_(x)
 
 struct file_format {
   /* where the value would be in the file */
@@ -146,7 +148,7 @@ process_file(file_format_t *head, const char *file, file_format_t *ret[2])
   fd = EopenD(file, O_RDONLY, 0);
   Efstat(fd, &st);
   mapping = mmap(NULL, MIN(st.st_size, MAX_PEEK_SIZE), PROT_READ, MAP_SHARED, fd, 0);
-  if (!mapping) {
+  if (mapping == MAP_FAILED) {
     perror("mmap()");
     Eclose(fd);
     return -1;
@@ -208,17 +210,6 @@ byteswap(void *p, size_t len)
   }
 }
 
-static inline ALWAYSINLINE void
-WRITE_INT(int fd, int num)
-{
-  char   buf[sizeof(num)*3+2];
-  size_t l;
-
-  l = utilvserver_fmt_long(buf,num);
-
-  Vwrite(fd, buf, l);
-}
-
 static int
 load_description(const char *file, file_format_t **head)
 {
@@ -237,7 +228,7 @@ load_description(const char *file, file_format_t **head)
 
   *buf = '\0';
   while (1) {
-    if ((eol = strchr(buf, '\n')) == NULL && (end - buf) < (sizeof(buf) - 1)) {
+    if ((eol = strchr(buf, '\n')) == NULL && (end - buf) < (signed) (sizeof(buf) - 1)) {
       bytes_read = Eread(fd, end, sizeof(buf) - 1 - (end - buf));
       /* EOF, implicit newline */
       if (bytes_read == 0) {
@@ -273,9 +264,10 @@ load_description(const char *file, file_format_t **head)
     }
     i->next = NULL;
 
-#define get_field()  for (ptr++; *ptr == '\t' && *ptr != '\0'; ptr++); \
-      for (field = ptr; *ptr != '\t' && *ptr != '\0'; ptr++); \
-      *ptr = '\0';
+#define get_field()    if (*(ptr+1) == '\0') goto new_line_and_free; \
+                       for (ptr++; *ptr == '\t' && *ptr != '\0'; ptr++); \
+                       for (field = ptr; *ptr != '\t' && *ptr != '\0'; ptr++); \
+                       *ptr = '\0';
     field = ptr = buf;
     while (*ptr != '\t' && *ptr != '\0')
       ptr++;
@@ -361,6 +353,25 @@ load_description(const char *file, file_format_t **head)
     get_field();
     i->peek_inside = (int)strtol(field, NULL, 0);
 
+    /* sanity check the entry */
+    if (i->offset < 0) {
+      WRITE_MSG(2, ENSC_WRAPPERS_PREFIX);
+      WRITE_STR(2, file);
+      WRITE_MSG(2, ":");
+      WRITE_INT(2, line_no);
+      WRITE_MSG(2, " has an invalid offset: ");
+      WRITE_INT(2, i->offset);
+      WRITE_MSG(2, "\n");
+      goto new_line_and_free;
+    }
+    else if ((i->offset + i->len) > MAX_PEEK_SIZE) {
+      WRITE_MSG(2, ENSC_WRAPPERS_PREFIX);
+      WRITE_STR(2, file);
+      WRITE_MSG(2, ":");
+      WRITE_INT(2, line_no);
+      WRITE_MSG(2, " exceeds maximum offset (" STRINGIFY(MAX_PEEK_SIZE) ")\n");
+      goto new_line_and_free;
+    }
 #undef get_field
     goto new_line;