prevent crash on start if shared or private anonymous memory cannot be allocated
Costa Tsaousis (ktsaou) committed
Sep 30, 2016 at 01:47 UTC
736937b3926814f792373885a9a0a4519bae7787
1 file changed
+19
-8
src/common.c
+19
-8
@@ -770,7 +770,11 @@ void *mymmap(const char *filename, size_t size, int flags, int ksm) {
770
if (flags & MAP_SHARED || !enable_ksm || !ksm) {
771
#endif
772
mem = mmap(NULL, size, PROT_READ | PROT_WRITE, flags, fd, 0);
773
- if (mem != MAP_FAILED) {
773
+ if (mem == MAP_FAILED) {
774
+ error("Cannot allocate SHARED memory for file '%s'.", filename);
775
+ mem = NULL;
776
+ }
777
+ else {
778
int advise = MADV_SEQUENTIAL | MADV_DONTFORK;
779
if (flags & MAP_SHARED) advise |= MADV_WILLNEED;
780
@@ -780,7 +784,8 @@ void *mymmap(const char *filename, size_t size, int flags, int ksm) {
784
}
785
}
786
#ifdef MADV_MERGEABLE
783
- } else {
787
+ }
788
+ else {
789
/*
790
// test - load the file into memory
791
mem = calloc(1, size);
@@ -794,7 +799,11 @@ void *mymmap(const char *filename, size_t size, int flags, int ksm) {
799
}
800
*/
801
mem = mmap(NULL, size, PROT_READ | PROT_WRITE, flags | MAP_ANONYMOUS, -1, 0);
797
- if (mem != MAP_FAILED) {
802
+ if (mem == MAP_FAILED) {
803
+ error("Cannot allocate PRIVATE ANONYMOUS memory for KSM for file '%s'.", filename);
804
+ mem = NULL;
805
+ }
806
+ else {
807
if (lseek(fd, 0, SEEK_SET) == 0) {
808
if (read(fd, mem, size) != (ssize_t) size)
809
error("Cannot read from file '%s'", filename);
@@ -813,17 +822,19 @@ void *mymmap(const char *filename, size_t size, int flags, int ksm) {
822
filename);
823
log_madvise_3--;
824
}
816
- } else
817
- error("Cannot allocate PRIVATE ANONYMOUS memory for KSM for file '%s'.", filename);
825
+ }
826
}
827
#endif
820
- } else
828
+ }
829
+ else
830
error("Cannot write to file '%s' at position %zu.", filename, size);
822
- } else
831
+ }
832
+ else
833
error("Cannot seek file '%s' to size %zu.", filename, size);
834
835
close(fd);
826
- } else
836
+ }
837
+ else
838
error("Cannot create/open file '%s'.", filename);
839
840
return mem;