diff --git a/gcc/testsuite/gfortran.dg/caf-shared/sync_images_1.f90 b/gcc/testsuite/gfortran.dg/caf-shared/sync_images_1.f90
new file mode 100644
index 00000000000..c06b7631cdd
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/caf-shared/sync_images_1.f90
@@ -0,0 +1,13 @@
+! { dg-do run }
+! { dg-set-target-env-var GFORTRAN_NUM_IMAGES "4" }
+! { dg-output "1234" }
+program main
+  implicit none
+  integer :: n, me
+  n = num_images()
+  me = this_image()
+  if (me /= 1) sync images (me - 1)
+  write (*,'(I0)',advance="no") me
+  if (me /= n)  sync images (me+1)
+  sync all
+end program main
diff --git a/gcc/testsuite/gfortran.dg/caf-shared/sync_images_2.f90 b/gcc/testsuite/gfortran.dg/caf-shared/sync_images_2.f90
new file mode 100644
index 00000000000..f1d63347291
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/caf-shared/sync_images_2.f90
@@ -0,0 +1,5 @@
+! { dg-do run }
+! { dg-set-target-env-var GFORTRAN_NUM_IMAGES "4" }
+program main
+  sync images(*)
+end program main
diff --git a/libgfortran/caf_shared/sync.c b/libgfortran/caf_shared/sync.c
index 76612dc477d..71f7a34497a 100644
--- a/libgfortran/caf_shared/sync.c
+++ b/libgfortran/caf_shared/sync.c
@@ -84,7 +84,7 @@ sync_iface_init (sync_iface *si, alloc_iface *ai, shared_memory *sm)
 
 /* TODO: Maybe check whether synchronizing image is still alive.  */
 void
-sync_table (sync_iface *si, int *images, size_t size)
+sync_table (sync_iface *si, int *images, int size)
 {
 #if defined(DEBUG_NATIVE_COARRAY) && DEBUG_NATIVE_COARRAY
   dprintf (2,
@@ -94,25 +94,48 @@ sync_table (sync_iface *si, int *images, size_t size)
     dprintf (2, "%d ", images[d_i]);
   dprintf (2, "\n");
 #endif
-  size_t i;
+  int i;
   int done;
   int *table = get_locked_table (si);
-  for (i = 0; i < size; i++)
+  if (size > 0)
     {
-      table[images[i] - 1 + local->total_num_images * this_image.image_num]++;
-      pthread_cond_signal (&si->triggers[images[i] - 1]);
+      for (i = 0; i < size; i++)
+	{
+	  table[images[i] - 1 + local->total_num_images * this_image.image_num]++;
+	  pthread_cond_signal (&si->triggers[images[i] - 1]);
+	}
+      for (;;)
+	{
+	  done = 1;
+	  for (i = 0; i < size; i++)
+	    done &= si->table[images[i] - 1
+			      + this_image.image_num * local->total_num_images]
+	      == si->table[this_image.image_num
+			   + (images[i] - 1) * local->total_num_images];
+	  if (done)
+	    break;
+	  wait_table_cond (si, &si->triggers[this_image.image_num]);
+	}
     }
-  for (;;)
+  else
     {
-      done = 1;
+      size = local->total_num_images;
       for (i = 0; i < size; i++)
-	done &= si->table[images[i] - 1
-			  + this_image.image_num * local->total_num_images]
-		== si->table[this_image.image_num
-			     + (images[i] - 1) * local->total_num_images];
-      if (done)
-	break;
-      wait_table_cond (si, &si->triggers[this_image.image_num]);
+	{
+	  table[i + local->total_num_images * this_image.image_num]++;
+	  pthread_cond_signal (&si->triggers[i]);
+	}
+      for (;;)
+	{
+	  done = 1;
+	  for (i = 0; i < size; i++)
+	    done &= si->table[i + this_image.image_num * local->total_num_images]
+	      == si->table[this_image.image_num
+			   + i * local->total_num_images];
+	  if (done)
+	    break;
+	  wait_table_cond (si, &si->triggers[this_image.image_num]);
+	}
     }
   unlock_table (si);
 }
diff --git a/libgfortran/caf_shared/sync.h b/libgfortran/caf_shared/sync.h
index 59946434da5..6b46a3c51b1 100644
--- a/libgfortran/caf_shared/sync.h
+++ b/libgfortran/caf_shared/sync.h
@@ -51,7 +51,7 @@ internal_proto (sync_iface_init);
 void sync_all (sync_iface *);
 internal_proto (sync_all);
 
-void sync_table (sync_iface *, int *, size_t);
+void sync_table (sync_iface *, int *, int);
 internal_proto (sync_table);
 
 #endif
diff --git a/libgfortran/caf_shared/wrapper.c b/libgfortran/caf_shared/wrapper.c
index 05ee838c243..e92f617260c 100644
--- a/libgfortran/caf_shared/wrapper.c
+++ b/libgfortran/caf_shared/wrapper.c
@@ -63,7 +63,7 @@ export_proto (cas_coarray_num_images);
 void cas_coarray_sync_all (int *);
 export_proto (cas_coarray_sync_all);
 
-void cas_sync_images (size_t, int *, int *, char *, size_t);
+void cas_sync_images (int, int *, int *, char *, size_t);
 export_proto (cas_sync_images);
 
 void cas_lock (void *);
@@ -250,7 +250,7 @@ cas_coarray_sync_all (int *stat)
 }
 
 void
-cas_sync_images (size_t s, int *images, int *stat, char *error,
+cas_sync_images (int s, int *images, int *stat, char *error,
 		 size_t err_size)
 {
   STAT_ERRMSG_ENTRY_CHECK (stat, error, err_size);
