Skip to content

Commit 41c8fb8

Browse files
committed
Fix more warnings
1 parent 3a32376 commit 41c8fb8

File tree

5 files changed

+45
-22
lines changed

5 files changed

+45
-22
lines changed

src/access-daemon/accessDaemon.c

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2120,8 +2120,7 @@ static int getBusFromSocketByNameDevid(const uint32_t socket, PciDevice* pcidev,
21202120
struct dirent *pDirent, *pDirentInner;
21212121
DIR *pDir, *pDirInner;
21222122
FILE* fp = NULL;
2123-
int tmplen = 200;
2124-
char tmpPath[200], buff[200];
2123+
char tmpPath[512], buff[512];
21252124
size_t ret = 0;
21262125
int bus_id = -1;
21272126
int numa_ctr = 0;
@@ -2137,12 +2136,10 @@ static int getBusFromSocketByNameDevid(const uint32_t socket, PciDevice* pcidev,
21372136
{
21382137
if (strncmp(pDirent->d_name, "pci0", 4) == 0)
21392138
{
2140-
memset(tmpPath, '\0', tmplen*sizeof(char));
2141-
sprintf(tmpPath, "/sys/devices/%s", pDirent->d_name);
2139+
snprintf(tmpPath, sizeof(tmpPath), "/sys/devices/%s", pDirent->d_name);
21422140

21432141
char bus[4];
2144-
strncpy(bus, &(pDirent->d_name[strlen(pDirent->d_name)-2]), 2);
2145-
bus[2] = '\0';
2142+
snprintf(bus, sizeof(bus), "%s", &pDirent->d_name[strlen(pDirent->d_name)-2]);
21462143

21472144
pDirInner = opendir (tmpPath);
21482145
if (pDir == NULL)
@@ -2156,33 +2153,33 @@ static int getBusFromSocketByNameDevid(const uint32_t socket, PciDevice* pcidev,
21562153
{
21572154
uint32_t dev_id = 0x0;
21582155
int numa_node = 0;
2159-
memset(tmpPath, '\0', tmplen*sizeof(char));
2160-
sprintf(tmpPath, "/sys/devices/%s/%s/device", pDirent->d_name, pDirentInner->d_name);
2156+
snprintf(tmpPath, sizeof(tmpPath), "/sys/devices/%s/%s/device", pDirent->d_name, pDirentInner->d_name);
21612157
if (pcidev->path && strcmp(&(pDirentInner->d_name[strlen(pDirentInner->d_name)-4]), pcidev->path) != 0)
21622158
{
21632159
continue;
21642160
}
21652161
fp = fopen(tmpPath,"r");
21662162
if( fp != NULL )
21672163
{
2168-
memset(buff, '\0', tmplen*sizeof(char));
2169-
ret = fread(buff, sizeof(char), tmplen-1, fp);
2164+
char *buffRead = fgets(buff, sizeof(buff), fp);
21702165
fclose(fp);
2171-
if (ret > 0)
2166+
if (buffRead)
21722167
{
21732168
dev_id = strtoul(buff, NULL, 16);
21742169

21752170
if (dev_id == pcidev->devid)
21762171
{
2177-
memset(tmpPath, '\0', tmplen*sizeof(char));
2178-
sprintf(tmpPath, "/sys/devices/%s/%s/numa_node", pDirent->d_name, pDirentInner->d_name);
2172+
snprintf(tmpPath, sizeof(tmpPath), "/sys/devices/%s/%s/numa_node", pDirent->d_name, pDirentInner->d_name);
21792173
fp = fopen(tmpPath,"r");
21802174
if( fp != NULL )
21812175
{
2182-
memset(buff, '\0', tmplen*sizeof(char));
2183-
ret = fread(buff, sizeof(char), tmplen-1, fp);
2176+
buffRead = fgets(buff, sizeof(buff), fp);
21842177
fclose(fp);
2185-
numa_node = atoi(buff);
2178+
if (buffRead)
2179+
numa_node = atoi(buff);
2180+
else
2181+
numa_node = -1;
2182+
21862183
if (numa_node < 0)
21872184
{
21882185
numa_node = numa_ctr;

src/affinity.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,7 @@ static int affinity_addRocmDomain(int nodeId, AffinityDomain* domain, int offset
778778
err = _affinity_addMemoryDomain(rocmdev->numaNode, domain, help);
779779
if (err == 0)
780780
{
781+
// TODO where does 9 come from? Please replace this with a meaningful constant
781782
snprintf(domain->tag, 9, "G%d", nodeId+offset);
782783
DEBUG_PRINT(DEBUGLEV_DEVELOP, "Affinity domain %s: %d HW threads on %d cores", domain->tag, domain->numberOfProcessors, domain->numberOfCores);
783784
return 0;

src/pci_hwloc.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,11 @@ int sysfs_pci_init(uint16_t testDevice, char** socket_bus, int* nrSockets)
143143
{
144144
continue;
145145
}
146-
fread(buff, sizeof(char), 99, fp);
146+
147+
if (fgets(buff, sizeof(buff), fp) == NULL) {
148+
fclose(fp);
149+
continue;
150+
}
147151
dev_id = strtoul(buff, NULL, 16);
148152
if (dev_id == SKYLAKE_SERVER_SOCKETID_MBOX_DID)
149153
{
@@ -155,7 +159,10 @@ int sysfs_pci_init(uint16_t testDevice, char** socket_bus, int* nrSockets)
155159
{
156160
continue;
157161
}
158-
fread(buff, sizeof(char), 99, fp);
162+
if (fgets(buff, sizeof(char), fp) == NULL) {
163+
fclose(fp);
164+
continue;
165+
}
159166
numa_node = atoi(buff);
160167
socket_bus[numa_node] = lw_strndup(bus, 3);
161168
nrSocks++;

src/pthread-overload/pthread-overload.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,13 @@ pthread_create(pthread_t* thread,
220220
}
221221
else
222222
{
223-
fgets(buff, 512, fpipe);
223+
if (fgets(buff, sizeof(buff), fpipe) == NULL) {
224+
// TODO, how should we handle this error correctly?
225+
fprintf(stderr, "Problems reading symbols for shepard thread detection\n");
226+
fclose(fpipe);
227+
return -1;
228+
}
229+
224230
char* tmp = strstr(buff, "monitor");
225231
if (tmp != NULL)
226232
{

src/topology.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,14 +221,26 @@ initTopologyFile(FILE* file)
221221
CacheLevel* cacheLevels;
222222
TreeNode* currentNode;
223223

224-
fread((void*) &cpuid_topology, sizeof(CpuTopology), 1, file);
224+
// TODO, may raise errors. Though, the signature currently does not allow to
225+
// return them. So let's abort for now.
226+
227+
if (fread(&cpuid_topology, sizeof(cpuid_topology), 1, file) != sizeof(cpuid_topology)) {
228+
ERROR_PRINT("Cannot read cpuid topology, not enough bytes");
229+
abort();
230+
}
225231

226232
hwThreadPool = lw_calloc(cpuid_topology.numHWThreads, sizeof(*hwThreadPool));
227-
fread((void*) hwThreadPool, sizeof(HWThread), cpuid_topology.numHWThreads, file);
233+
if (fread(hwThreadPool, sizeof(*hwThreadPool), cpuid_topology.numHWThreads, file) != sizeof(sizeof(*hwThreadPool))) {
234+
ERROR_PRINT("Cannot read thread pool, not enough bytes");
235+
abort();
236+
}
228237
cpuid_topology.threadPool = hwThreadPool;
229238

230239
cacheLevels = lw_calloc(cpuid_topology.numCacheLevels, sizeof(*cacheLevels));
231-
fread((void*) cacheLevels, sizeof(CacheLevel), cpuid_topology.numCacheLevels, file);
240+
if (fread(cacheLevels, sizeof(*cacheLevels), cpuid_topology.numCacheLevels, file) != sizeof(*cacheLevels)) {
241+
ERROR_PRINT("Cannot read cache levels, not enough bytes");
242+
abort();
243+
}
232244
cpuid_topology.cacheLevels = cacheLevels;
233245
cpuid_topology.topologyTree = NULL;
234246

0 commit comments

Comments
 (0)