@@ -705,14 +705,6 @@ GMT_LOCAL inline struct GMT_POSTSCRIPT  * gmtapi_get_postscript_data (struct GMT
705705GMT_LOCAL inline struct GMT_PALETTE  * gmtapi_get_palette_data (struct GMT_PALETTE *ptr) {return (ptr);}
706706GMT_LOCAL inline char ** gmtapi_get_char_char_ptr     (char **ptr)  {return (ptr);}
707707
708- /*! If API is not set or do_not_exit is false then we call system exit, else we move along.
709-  * This is required for some external interfaces where calling exit would bring down the
710-  * external environment (this is true for MATLAB, for instance). */
711- GMT_LOCAL inline void gmtapi_exit (struct GMTAPI_CTRL *API, int code) {
712- 	if (API->do_not_exit == false)
713- 		GMT_exit (API->GMT, code);
714- }
715- 
716708/*! gmtapi_return_address is a convenience function that, given type, calls the correct converter */
717709GMT_LOCAL void * gmtapi_return_address (void *data, unsigned int type) {
718710	void *p = NULL;
@@ -1023,7 +1015,7 @@ GMT_LOCAL int gmtapi_init_sharedlibs (struct GMTAPI_CTRL *API) {
10231015		GMT_Report (API, GMT_MSG_DEBUG, "Loading core GMT shared library: %s\n", API->lib[0].path);
10241016		if ((API->lib[0].handle = dlopen_special (API->lib[0].path)) == NULL) {
10251017			GMT_Report (API, GMT_MSG_ERROR, "Failure while loading core GMT shared library: %s\n", dlerror());
1026- 			gmtapi_exit (API, GMT_RUNTIME_ERROR);  return GMT_RUNTIME_ERROR;
1018+ 			return - GMT_RUNTIME_ERROR;
10271019		}
10281020		dlerror (); /* Clear any existing error */
10291021	}
@@ -1816,8 +1808,8 @@ GMT_LOCAL p_func_uint64_t gmtapi_get_2d_to_index (struct GMTAPI_CTRL *API, enum
18161808			p = (shape == GMT_IS_ROW_FORMAT) ? gmtapi_2d_to_index_c_cplx_imag : gmtapi_2d_to_index_f_cplx_imag;
18171809			break;
18181810		default:
1819- 			GMT_Report (API, GMT_MSG_ERROR, "gmtapi_get_2d_to_index: Illegal mode passed - aborting \n");
1820- 			gmtapi_exit (API, GMT_RUNTIME_ERROR);  return (NULL);
1811+ 			GMT_Report (API, GMT_MSG_ERROR, "gmtapi_get_2d_to_index: Illegal mode passed\n");
1812+ 			return (NULL);
18211813	}
18221814	return (p);
18231815}
@@ -3507,7 +3499,8 @@ GMT_LOCAL struct GMT_DATASET * gmtapi_import_dataset (struct GMTAPI_CTRL *API, i
35073499				D_obj->table[D_obj->n_tables] = gmt_get_table (GMT);
35083500				D_obj->table[D_obj->n_tables]->segment = gmt_M_memory (GMT, NULL, s_alloc, struct GMT_DATASEGMENT *);
35093501				S = D_obj->table[D_obj->n_tables]->segment[0] = GMT_Alloc_Segment (API, smode, M_obj->n_rows, n_columns, NULL, NULL);
3510- 				GMT_2D_to_index = gmtapi_get_2d_to_index (API, M_obj->shape, GMT_GRID_IS_REAL);
3502+ 				if ((GMT_2D_to_index = gmtapi_get_2d_to_index (API, M_obj->shape, GMT_GRID_IS_REAL)) == NULL)
3503+ 					return_null (API, GMT_WRONG_MATRIX_SHAPE);
35113504				if ((api_get_val = gmtapi_select_get_function (API, M_obj->type)) == NULL)
35123505					return_null (API, GMT_NOT_A_VALID_TYPE);
35133506
@@ -3908,7 +3901,8 @@ GMT_LOCAL int gmtapi_export_dataset (struct GMTAPI_CTRL *API, int object_ID, uns
39083901			}
39093902
39103903			/* Set up index and put-value functions for this matrix */
3911- 			GMT_2D_to_index = gmtapi_get_2d_to_index (API, M_obj->shape, GMT_GRID_IS_REAL);
3904+ 			if ((GMT_2D_to_index = gmtapi_get_2d_to_index (API, M_obj->shape, GMT_GRID_IS_REAL)) == NULL)
3905+ 				return (gmtlib_report_error (API, GMT_WRONG_MATRIX_SHAPE));
39123906			if ((api_put_val = gmtapi_select_put_function (API, M_obj->type)) == NULL)
39133907				return (gmtlib_report_error (API, GMT_NOT_A_VALID_TYPE));
39143908
@@ -4409,7 +4403,8 @@ GMT_LOCAL struct GMT_IMAGE * gmtapi_import_image (struct GMTAPI_CTRL *API, int o
44094403			GMT_Report (API, GMT_MSG_INFORMATION, "Importing image data from user memory location\n");
44104404			gmt_set_grddim (GMT, I_obj->header);	/* Set all dimensions */
44114405			I_obj->data = gmt_M_memory (GMT, NULL, I_obj->header->size, unsigned char);
4412- 			GMT_2D_to_index = gmtapi_get_2d_to_index (API, M_obj->shape, GMT_GRID_IS_REAL);
4406+ 			if ((GMT_2D_to_index = gmtapi_get_2d_to_index (API, M_obj->shape, GMT_GRID_IS_REAL)) == NULL)
4407+ 				return_null (API, GMT_WRONG_MATRIX_SHAPE);
44134408			if ((api_get_val = gmtapi_select_get_function (API, M_obj->type)) == NULL)
44144409				return_null (API, GMT_NOT_A_VALID_TYPE);
44154410			gmt_M_grd_loop (GMT, I_obj, row, col, ij) {
@@ -4854,7 +4849,8 @@ GMT_LOCAL struct GMT_GRID * gmtapi_import_grid (struct GMTAPI_CTRL *API, int obj
48544849			G_obj->header->complex_mode = (mode & GMT_GRID_IS_COMPLEX_MASK);	/* Set the complex mode */
48554850			GH->alloc_mode = GMT_ALLOC_INTERNALLY;
48564851			done = (mode & GMT_CONTAINER_ONLY) ? false : true;	/* Not done until we read grid */
4857- 			GMT_2D_to_index = gmtapi_get_2d_to_index (API, M_obj->shape, GMT_GRID_IS_REAL);
4852+ 			if ((GMT_2D_to_index = gmtapi_get_2d_to_index (API, M_obj->shape, GMT_GRID_IS_REAL)) == NULL)
4853+ 				return_null (API, GMT_WRONG_MATRIX_SHAPE);
48584854			if ((api_get_val = gmtapi_select_get_function (API, M_obj->type)) == NULL)
48594855				return_null (API, GMT_NOT_A_VALID_TYPE);
48604856			HH = gmt_get_H_hidden (G_obj->header);
@@ -5177,7 +5173,8 @@ GMT_LOCAL int gmtapi_export_grid (struct GMTAPI_CTRL *API, int object_ID, unsign
51775173				if ((error = gmtlib_alloc_univector (GMT, &(M_obj->data), M_obj->type, size)) != GMT_NOERROR) return (error);
51785174				MH->alloc_mode = GMT_ALLOC_INTERNALLY;
51795175			}
5180- 			GMT_2D_to_index = gmtapi_get_2d_to_index (API, M_obj->shape, GMT_GRID_IS_REAL);
5176+ 			if ((GMT_2D_to_index = gmtapi_get_2d_to_index (API, M_obj->shape, GMT_GRID_IS_REAL)) == NULL)
5177+ 				return (gmtlib_report_error (API, GMT_WRONG_MATRIX_SHAPE));
51815178			if ((api_put_val = gmtapi_select_put_function (API, M_obj->type)) == NULL)
51825179				return (gmtlib_report_error (API, GMT_NOT_A_VALID_TYPE));
51835180			gmt_M_grd_loop (GMT, G_obj, row, col, ijp) {
@@ -5340,7 +5337,8 @@ GMT_LOCAL struct GMT_MATRIX * gmtapi_read_matrix (struct GMT_CTRL *GMT, void *so
53405337	}
53415338	if ((api_put_val = gmtapi_select_put_function (GMT->parent, M->type)) == NULL)	/* Get correct put function given data type */
53425339		return_null (GMT->parent, GMT_NOT_A_VALID_TYPE);
5343- 	GMT_2D_to_index = gmtapi_get_2d_to_index (GMT->parent, M->shape, GMT_GRID_IS_REAL);	/* Get ij index function */
5340+ 	if ((GMT_2D_to_index = gmtapi_get_2d_to_index (GMT->parent, M->shape, GMT_GRID_IS_REAL)) == NULL)	/* Get ij index function */
5341+ 		return_null (GMT->parent, GMT_WRONG_MATRIX_SHAPE);
53445342	for (col = 0; col < M->n_columns; col++) {
53455343		for (row = 0; row < M->n_rows; row++) {
53465344			ij = GMT_2D_to_index (row, col, M->dim);	/* Index into the user data matrix depends on layout (M->shape) */
@@ -5389,7 +5387,8 @@ GMT_LOCAL void * gmtapi_grid2matrix (struct GMTAPI_CTRL *API, struct GMT_GRID *I
53895387		if ((error = gmtlib_alloc_univector (API->GMT, &(Out->data), Out->type, Out->n_rows * Out->n_columns)) != GMT_NOERROR)
53905388			return_null (API, error);
53915389	}
5392- 	GMT_2D_to_index = gmtapi_get_2d_to_index (API, Out->shape, GMT_GRID_IS_REAL);
5390+ 	if ((GMT_2D_to_index = gmtapi_get_2d_to_index (API, Out->shape, GMT_GRID_IS_REAL)) == NULL)
5391+ 		return_null (API, GMT_WRONG_MATRIX_SHAPE);
53935392	if ((api_put_val = gmtapi_select_put_function (API, Out->type)) == NULL)
53945393		return_null (API, GMT_NOT_A_VALID_TYPE);
53955394
@@ -5421,7 +5420,8 @@ GMT_LOCAL void * gmtapi_matrix2grid (struct GMTAPI_CTRL *API, struct GMT_MATRIX
54215420		if ((error = gmtapi_alloc_grid_xy (API, Out)) != GMT_NOERROR)
54225421			return_null (API, error);	/* Allocation error */
54235422	}
5424- 	GMT_2D_to_index = gmtapi_get_2d_to_index (API, In->shape, GMT_GRID_IS_REAL);
5423+ 	if ((GMT_2D_to_index = gmtapi_get_2d_to_index (API, In->shape, GMT_GRID_IS_REAL)) == NULL)
5424+ 		return_null (API, GMT_WRONG_MATRIX_SHAPE);
54255425	if ((api_get_val = gmtapi_select_get_function (API, In->type)) == NULL)
54265426		return_null (API, GMT_NOT_A_VALID_TYPE);
54275427
@@ -5575,7 +5575,8 @@ GMT_LOCAL int gmtapi_write_matrix (struct GMT_CTRL *GMT, void *dest, unsigned in
55755575	GMT_Report (GMT->parent, GMT_MSG_DEBUG, "%s Matrix to %s\n", msg1[append], &M_file[append]);
55765576
55775577	/* Set index and put-value functions */
5578- 	GMT_2D_to_index = gmtapi_get_2d_to_index (GMT->parent, M->shape, GMT_GRID_IS_REAL);
5578+ 	if ((GMT_2D_to_index = gmtapi_get_2d_to_index (GMT->parent, M->shape, GMT_GRID_IS_REAL)) == NULL)
5579+ 		return (GMT_WRONG_MATRIX_SHAPE);
55795580	if ((api_get_val = gmtapi_select_get_function (GMT->parent, M->type)) == NULL)
55805581		return (GMT_NOT_A_VALID_TYPE);
55815582
@@ -6830,7 +6831,7 @@ void * GMT_Create_Session (const char *session, unsigned int pad, unsigned int m
68306831	API->verbose = (mode >> 16);	/* Pick up any -V settings from gmt.c */
68316832	API->pad = pad;		/* Preserve the default pad value for this session */
68326833	API->print_func = (print_func == NULL) ? gmtapi_print_func : print_func;	/* Pointer to the print function to use in GMT_Message|Report */
6833- 	API->do_not_exit = mode & GMT_SESSION_NOEXIT;	/* if set, then gmtapi_exit & GMT_exit are simply a return; otherwise they call exit  */
6834+ 	API->do_not_exit = mode & GMT_SESSION_NOEXIT;	/* Deprecated, we no longer call exit anywhere in the API (gmt_api.c)  */
68346835	API->external = mode & GMT_SESSION_EXTERNAL;	/* if false|0 then we don't list read and write as modules */
68356836	API->shape = (mode & GMT_SESSION_COLMAJOR) ? GMT_IS_COL_FORMAT : GMT_IS_ROW_FORMAT;		/* if set then we must use column-major format [row-major] */
68366837	API->runmode = mode & GMT_SESSION_RUNMODE;		/* If nonzero we set up modern GMT run-mode, else classic */
@@ -6900,7 +6901,8 @@ void * GMT_Create_Session (const char *session, unsigned int pad, unsigned int m
69006901	else
69016902		API->GMT->init.module_name = unknown; /* or unknown */
69026903
6903- 	gmtapi_init_sharedlibs (API);				/* Count how many shared libraries we should know about, and get their names and paths */
6904+ 	if (gmtapi_init_sharedlibs (API) < 0)				/* Count how many shared libraries we should know about, and get their names and paths */
6905+ 		return_null (API, GMT_RUNTIME_ERROR);
69046906
69056907	return (API);	/* Pass the structure back out */
69066908}
@@ -7377,7 +7379,8 @@ GMT_LOCAL int gmtapi_end_io_matrix (struct GMTAPI_CTRL *API, struct GMTAPI_DATA_
73777379		if (api_put_val == NULL) return_error (API, GMT_NOT_A_VALID_TYPE);
73787380		p_func_uint64_t GMT_2D_to_index = NULL;
73797381		uint64_t col, ij;
7380- 		GMT_2D_to_index = gmtapi_get_2d_to_index (API, GMT_IS_ROW_FORMAT, GMT_GRID_IS_REAL);	/* Can only do row-format until end of this function */
7382+ 		if ((GMT_2D_to_index = gmtapi_get_2d_to_index (API, GMT_IS_ROW_FORMAT, GMT_GRID_IS_REAL)) == NULL)	/* Can only do row-format until end of this function */
7383+ 			return_error (API, GMT_WRONG_MATRIX_SHAPE);
73817384		while (S->delay) {	/* Place delayed NaN-rows(s) up front */
73827385			S->delay--;
73837386			for (col = 0; col < M->n_columns; col++) {	/* Place the output items */
@@ -8471,7 +8474,8 @@ struct GMT_RECORD *api_get_record_matrix (struct GMTAPI_CTRL *API, unsigned int
84718474		}
84728475		API->current_get_M = gmtapi_get_matrix_data (S->resource);
84738476		API->current_get_n_columns = (GMT->common.i.select) ? GMT->common.i.n_cols : S->n_columns;
8474- 		API->current_get_M_index = gmtapi_get_2d_to_index (API, API->current_get_M->shape, GMT_GRID_IS_REAL);
8477+ 		if ((API->current_get_M_index = gmtapi_get_2d_to_index (API, API->current_get_M->shape, GMT_GRID_IS_REAL)) == NULL)
8478+ 			return NULL;
84758479		if ((API->current_get_M_val = gmtapi_select_get_function (API, API->current_get_M->type)) == NULL)
84768480			return NULL;
84778481		record = NULL;
@@ -8653,7 +8657,9 @@ GMT_LOCAL void gmtapi_get_record_init (struct GMTAPI_CTRL *API) {
86538657		case GMT_IS_REFERENCE|GMT_VIA_MATRIX:
86548658			API->current_get_M = gmtapi_get_matrix_data (S->resource);
86558659			API->current_get_n_columns = (GMT->common.i.select) ? GMT->common.i.n_cols : S->n_columns;
8656- 			API->current_get_M_index = gmtapi_get_2d_to_index (API, API->current_get_M->shape, GMT_GRID_IS_REAL);
8660+ 			if ((API->current_get_M_index = gmtapi_get_2d_to_index (API, API->current_get_M->shape, GMT_GRID_IS_REAL)) == NULL) {
8661+ 				GMT_Report (API, GMT_MSG_ERROR, "GMTAPI: Internal error: gmtapi_get_record_init called gmtapi_get_2d_to_index with wring shape\n");
8662+ 			}
86578663			API->current_get_M_val = gmtapi_select_get_function (API, API->current_get_M->type);
86588664			if (API->current_get_M->text == NULL) GMT->current.io.record.text = NULL;
86598665			API->api_get_record = api_get_record_matrix;
@@ -9106,7 +9112,8 @@ GMT_LOCAL int gmtapi_put_record_init (struct GMTAPI_CTRL *API, unsigned int mode
91069112			}
91079113			/* Place current matrix parameters in API */
91089114			API->current_put_M = M_obj;
9109- 			API->current_put_M_index = gmtapi_get_2d_to_index (API, GMT_IS_ROW_FORMAT, GMT_GRID_IS_REAL);	/* Since we cannot do col_format without knowing dimension - see end_io_matrix */
9115+ 			if ((API->current_put_M_index = gmtapi_get_2d_to_index (API, GMT_IS_ROW_FORMAT, GMT_GRID_IS_REAL)) == NULL)	/* Since we cannot do col_format without knowing dimension - see end_io_matrix */
9116+ 				return GMT_WRONG_MATRIX_SHAPE;
91109117			if ((API->current_put_M_val = gmtapi_select_put_function (API, M_obj->type)) == NULL)
91119118				return GMT_NOT_A_VALID_TYPE;
91129119			API->api_put_record = gmtapi_put_record_matrix;
@@ -12546,7 +12553,7 @@ GMT_LOCAL void * gmtapi_dataset2matrix (struct GMTAPI_CTRL *API, struct GMT_DATA
1254612553		MH->alloc_mode = GMT_ALLOC_INTERNALLY;
1254712554	}
1254812555	if ((api_put_val = gmtapi_select_put_function (API, Out->type)) == NULL) return (NULL);
12549- 	GMT_2D_to_index = gmtapi_get_2d_to_index (API, Out->shape, GMT_GRID_IS_REAL);
12556+ 	if (( GMT_2D_to_index = gmtapi_get_2d_to_index (API, Out->shape, GMT_GRID_IS_REAL)) == NULL) return (NULL );
1255012557	for (tbl = row_out = 0; tbl < In->n_tables; tbl++) {
1255112558		D = In->table[tbl];	/* Shorthand to current input data table */
1255212559		for (seg = 0; seg < D->n_segments; seg++) {
@@ -12630,7 +12637,8 @@ GMT_LOCAL void * gmtapi_matrix2dataset (struct GMTAPI_CTRL *API, struct GMT_MATR
1263012637	SD = Out->table[0]->segment[0];	/* Shorthand to only segment in the dataset */
1263112638	if ((api_get_val = gmtapi_select_get_function (API, In->type)) == NULL)
1263212639		return_null (API, GMT_NOT_A_VALID_TYPE);
12633- 	GMT_2D_to_index = gmtapi_get_2d_to_index (API, In->shape, GMT_GRID_IS_REAL);
12640+ 	if ((GMT_2D_to_index = gmtapi_get_2d_to_index (API, In->shape, GMT_GRID_IS_REAL)) == NULL)
12641+ 		return_null (API, GMT_WRONG_MATRIX_SHAPE);
1263412642	for (row = 0; row < In->n_rows; row++) {
1263512643		for (col = 0; col < In->n_columns; col++) {
1263612644			ij = GMT_2D_to_index (row, col, In->dim);	/* Index into the user data matrix depends on layout (M->shape) */
@@ -12671,7 +12679,8 @@ GMT_LOCAL void * gmtapi_matrix2vector (struct GMTAPI_CTRL *API, struct GMT_MATRI
1267112679		return_null (API, GMT_NOT_A_VALID_TYPE);
1267212680	if ((api_put_val_v = gmtapi_select_put_function (API, GMT->current.setting.export_type)) == NULL)	/* Since all columns are of same type we get the pointer here */
1267312681		return_null (API, GMT_NOT_A_VALID_TYPE);
12674- 	GMT_2D_to_index = gmtapi_get_2d_to_index (API, In->shape, GMT_GRID_IS_REAL);
12682+ 	if ((GMT_2D_to_index = gmtapi_get_2d_to_index (API, In->shape, GMT_GRID_IS_REAL)) == NULL)
12683+ 		return_null (API, GMT_WRONG_MATRIX_SHAPE);
1267512684	for (row = 0; row < In->n_rows; row++) {
1267612685		for (col = 0; col < In->n_columns; col++) {
1267712686			ij = GMT_2D_to_index (row, col, In->dim);	/* Index into the user data matrix depends on layout (M->shape) */
@@ -12740,7 +12749,8 @@ GMT_LOCAL void * gmtapi_vector2matrix (struct GMTAPI_CTRL *API, struct GMT_VECTO
1274012749		MH->alloc_mode = GMT_ALLOC_INTERNALLY;
1274112750	}
1274212751
12743- 	GMT_2D_to_index = gmtapi_get_2d_to_index (API, Out->shape, GMT_GRID_IS_REAL);
12752+ 	if ((GMT_2D_to_index = gmtapi_get_2d_to_index (API, Out->shape, GMT_GRID_IS_REAL)) == NULL)
12753+ 		return (NULL);
1274412754	if ((api_put_val = gmtapi_select_put_function (API, Out->type)) == NULL)	/* Since all columns are of same type we get the pointer here */
1274512755		return (NULL);
1274612756	for (col = 0; col < In->n_columns; col++) {
0 commit comments