Skip to content

Commit 3def93a

Browse files
committed
Merge pull request #1169 from igor-ivanov/pr/oshmem-fix-scan-coverity
oshmem: Fix scan coverity issues
2 parents 351bd03 + 5c061ab commit 3def93a

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

oshmem/info/info.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "orte/runtime/orte_globals.h"
1818
#include "orte/util/show_help.h"
1919

20+
#include "opal/util/opal_environ.h"
2021
#include "opal/util/output.h"
2122

2223
#include "oshmem/version.h"
@@ -220,6 +221,17 @@ static int oshmem_info_get_heap_size(char *value, size_t *interp)
220221
long long size = 0;
221222

222223
p = value;
224+
*interp = 0;
225+
226+
if (!p) {
227+
return OSHMEM_ERR_BAD_PARAM;
228+
}
229+
230+
/* Sanity check for buffer overflow attack (coverity issue: Use of untrusted string value) */
231+
if (16 < strlen(p)) {
232+
return OSHMEM_ERR_BAD_PARAM;
233+
}
234+
223235
if (1 == sscanf(p, "%lld%n", &size, &idx)) {
224236
if (p[idx] != '\0') {
225237
if (p[idx + 1] == '\0') {
@@ -243,20 +255,25 @@ static int oshmem_info_get_heap_size(char *value, size_t *interp)
243255
if (size <= 0) {
244256
return OSHMEM_ERR_BAD_PARAM;
245257
} else {
258+
opal_setenv(OSHMEM_ENV_SYMMETRIC_SIZE, p, true, &environ);
259+
opal_setenv(SHMEM_HEAP_SIZE, p, true, &environ);
260+
/* Probably needless code */
261+
#if 0
246262
char *tmp = p;
247263

248264
if(!p) {
249265
asprintf(&tmp, "%lld", size * factor);
250266
}
251267

252268
if (tmp) {
253-
setenv(OSHMEM_ENV_SYMMETRIC_SIZE, tmp, 1);
254-
setenv(SHMEM_HEAP_SIZE, tmp, 1);
269+
opal_setenv(OSHMEM_ENV_SYMMETRIC_SIZE, p, true, &environ);
270+
opal_setenv(SHMEM_HEAP_SIZE, p, true, &environ);
255271
}
256272

257273
if (!p && tmp) {
258274
free(tmp);
259275
}
276+
#endif
260277
}
261278

262279
*interp = size * factor;

oshmem/mca/memheap/base/memheap_base_static.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,15 +183,19 @@ static int _load_segments(void)
183183

184184
while (NULL != fgets(line, sizeof(line), fp)) {
185185
memset(&seg, 0, sizeof(seg));
186-
sscanf(line,
186+
if (3 > sscanf(line,
187187
"%llx-%llx %s %llx %s %llx %s",
188188
(unsigned long long *) &seg.start,
189189
(unsigned long long *) &seg.end,
190190
seg.perms,
191191
(unsigned long long *) &seg.offset,
192192
seg.dev,
193193
(unsigned long long *) &seg.inode,
194-
seg.pathname);
194+
seg.pathname)) {
195+
MEMHEAP_ERROR("Failed to sscanf /proc/self/maps output %s", line);
196+
fclose(fp);
197+
return OSHMEM_ERROR;
198+
}
195199

196200
if (OSHMEM_ERROR == _check_address(&seg))
197201
continue;

oshmem/proc/proc.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ oshmem_group_t* oshmem_proc_group_create(int pe_start,
151151
/* allocate an array */
152152
proc_array = (oshmem_proc_t**) malloc(pe_size * sizeof(oshmem_proc_t*));
153153
if (NULL == proc_array) {
154+
OBJ_RELEASE(group);
154155
OPAL_THREAD_UNLOCK(&oshmem_proc_lock);
155156
return NULL ;
156157
}
@@ -162,6 +163,9 @@ oshmem_group_t* oshmem_proc_group_create(int pe_start,
162163
if (NULL == proc) {
163164
opal_output(0,
164165
"Error: Can not find proc object for pe = %d", i);
166+
free(proc_array);
167+
OBJ_RELEASE(group);
168+
OPAL_THREAD_UNLOCK(&oshmem_proc_lock);
165169
return NULL;
166170
}
167171
if (count_pe >= (int) pe_size) {
@@ -200,8 +204,9 @@ oshmem_group_t* oshmem_proc_group_create(int pe_start,
200204
"Error: No collective modules are available: group is not created, returning NULL");
201205
oshmem_proc_group_destroy(group);
202206
OPAL_THREAD_UNLOCK(&oshmem_proc_lock);
203-
return NULL ;
204-
} OPAL_THREAD_UNLOCK(&oshmem_proc_lock);
207+
return NULL;
208+
}
209+
OPAL_THREAD_UNLOCK(&oshmem_proc_lock);
205210
}
206211

207212
return group;

0 commit comments

Comments
 (0)