Skip to content

Commit 31cf553

Browse files
authored
Merge pull request #21767 from pshipton/readprops0.51
(0.51) Fix call to propsfile_read_text buffer length calculation
2 parents c40d645 + 456ecb6 commit 31cf553

File tree

1 file changed

+61
-52
lines changed

1 file changed

+61
-52
lines changed

runtime/util/propsfile.c

Lines changed: 61 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,12 @@ hashFn(void *entry, void *userData) {
103103
char* key = pair->key;
104104
UDATA length = strlen(key);
105105
UDATA i;
106-
106+
107107
/* Super-lame xor hash */
108108
for (i=0; i < length; i++) {
109109
hash ^= key[i];
110110
}
111-
111+
112112
return hash;
113113
}
114114

@@ -117,11 +117,11 @@ static UDATA
117117
equalsFn(void *leftEntry, void *rightEntry, void *userData) {
118118
KeyValuePair* lhs = leftEntry;
119119
KeyValuePair* rhs = rightEntry;
120-
120+
121121
if (strcmp(lhs->key, rhs->key) == 0) {
122122
return TRUE;
123123
}
124-
124+
125125
return FALSE;
126126
}
127127

@@ -144,30 +144,29 @@ trimTrailing(char* input)
144144
input[index] = 0;
145145
index--;
146146
break;
147-
147+
148148
default:
149149
return;
150150
}
151-
}
151+
}
152152
}
153153

154154

155-
j9props_file_t
155+
j9props_file_t
156156
props_file_open(J9PortLibrary* portLibrary, const char* file, char* errorBuf, UDATA errorBufLength)
157157
{
158158
PORT_ACCESS_FROM_PORT(portLibrary);
159-
j9props_file_t result = NULL;
160159
IDATA fileHandle = -1;
161160
UDATA lineNumber = 0;
162161
char line[1024] = "";
163-
/* points to some place in line */
164-
char * linePtr = line;
162+
UDATA lineLength = 0;
163+
char *linePtr = line;
165164

166-
/* Allocate the structure */
167-
result = (j9props_file_t) j9mem_allocate_memory(sizeof(J9PropsFile), OMRMEM_CATEGORY_VM);
165+
j9props_file_t result = (j9props_file_t)j9mem_allocate_memory(sizeof(*result), OMRMEM_CATEGORY_VM);
168166
if (NULL == result) {
169167
return NULL;
170168
}
169+
memset(result, 0, sizeof(*result));
171170
result->portLibrary = portLibrary;
172171

173172
/* Allocate the hash table */
@@ -177,84 +176,94 @@ props_file_open(J9PortLibrary* portLibrary, const char* file, char* errorBuf, UD
177176
}
178177

179178
fileHandle = j9file_open(file, EsOpenRead, 0);
180-
if (fileHandle == -1) {
179+
if (-1 == fileHandle) {
181180
goto fail;
182181
}
183-
184-
/* Read a line at a time */
182+
183+
/* Read a line at a time. */
185184
lineNumber = 0;
186-
/* previously had a j9file_read_text here, but functionality to do the charset conversion (for zos) is not there yet
187-
* so we use this hack for now*/
188-
while (NULL != propsfile_read_text(portLibrary, fileHandle, linePtr, sizeof(line) - (line - linePtr) )) {
189-
char* equalsSign;
190-
191-
if(strlen(line) > 1 && line[strlen(line)-1] == '\n' && line[strlen(line)-2] == '\\'){ /* line continuation */
185+
/* Previously had a j9file_read_text here, but functionality to do the charset conversion (for zos) is not there yet
186+
* so we use this hack for now.
187+
*/
188+
while (NULL != propsfile_read_text(portLibrary, fileHandle, linePtr, sizeof(line) - (linePtr - line))) {
189+
UDATA allocLength = 0;
190+
char *equalsSign = NULL;
191+
lineLength += strlen(linePtr);
192+
193+
if ((lineLength >= 2) && ('\n' == line[lineLength - 1]) && ('\\' == line[lineLength - 2])) { /* line continuation */
192194
/* update linePtr to new position and overwrite the '\\' */
193-
linePtr = linePtr + strlen(linePtr) - 2;
195+
lineLength -= 2;
196+
linePtr = line + lineLength;
194197
continue;
195198
}
196199

197200
linePtr = line;
198-
lineNumber++;
199-
equalsSign = strstr(line,"=");
200-
if (equalsSign && line[0] != '#') { /*lines starting with # are comments*/
201-
UDATA lineLength;
202-
char* copiedLine;
203-
KeyValuePair pair;
204-
205-
/* Ensure that we have something on the LHS */
201+
allocLength = lineLength + 1; /* + 1 for NULL */
202+
lineLength = 0;
203+
lineNumber += 1;
204+
205+
/* Lines starting with # are comments. */
206+
if ('#' == line[0]) {
207+
continue;
208+
}
209+
210+
equalsSign = strstr(line, "=");
211+
if (NULL != equalsSign) {
212+
char *copiedLine = NULL;
213+
KeyValuePair pair = { NULL, NULL };
214+
215+
/* Ensure that we have something on the LHS. */
206216
if (equalsSign == line) {
207217
goto fail;
208218
}
209219

210220
/* Copy the line */
211-
lineLength = strlen(line);
212-
copiedLine = j9mem_allocate_memory(lineLength + 1 /* for NULL */, OMRMEM_CATEGORY_VM);
221+
copiedLine = j9mem_allocate_memory(allocLength, OMRMEM_CATEGORY_VM);
213222
if (NULL == copiedLine) {
214223
goto fail;
215224
}
216-
memcpy(copiedLine, line, lineLength + 1);
217-
218-
/* Slam a NULL where the equals sign was */
225+
memcpy(copiedLine, line, allocLength);
226+
227+
/* Slam a NULL where the equals sign was. */
219228
pair.key = copiedLine;
220-
pair.value = &copiedLine[equalsSign-line+1];
229+
pair.value = &copiedLine[equalsSign - line + 1];
221230
pair.value[-1] = 0;
222-
223-
/* Deal with any trailing whitespace on the value */
231+
232+
/* Deal with any trailing whitespace on the value. */
224233
trimTrailing(pair.key);
225234
trimTrailing(pair.value);
226-
227-
/* Insert the halves into the hash table */
235+
236+
/* Insert the halves into the hash table. */
228237
if (NULL == hashTableAdd(result->properties, &pair)) {
229238
j9mem_free_memory(pair.key);
230239
goto fail;
231240
}
232241
}
233242
}
234243

235-
/* Done with the file */
244+
/* Done with the file. */
236245
j9file_close(fileHandle);
237246
return result;
238247

239248
fail:
240-
if (fileHandle != -1) {
249+
if (-1 != fileHandle) {
241250
j9file_close(fileHandle);
242251
}
243-
/* Call close to free memory */
252+
/* Call close to free memory. */
244253
props_file_close(result);
245254
return NULL;
246255
}
247256

248257

249-
void
258+
void
250259
props_file_close(j9props_file_t file)
251260
{
252261
PORT_ACCESS_FROM_PORT(file->portLibrary);
253-
262+
254263
if (file->properties != NULL) {
255264
J9HashTableState walkState;
256265
KeyValuePair* pair;
257-
266+
258267
/* Loop through and free any keys */
259268
pair = hashTableStartDo(file->properties, &walkState);
260269
while (pair != NULL) {
@@ -265,33 +274,33 @@ props_file_close(j9props_file_t file)
265274
/* The free the entries */
266275
hashTableFree(file->properties);
267276
}
268-
277+
269278
/* Free the heap allocated structure */
270279
j9mem_free_memory(file);
271280
}
272281

273282

274-
const char*
283+
const char*
275284
props_file_get(j9props_file_t file, const char* key)
276285
{
277286
KeyValuePair pair, *result;
278287
pair.key = (char*)key;
279-
288+
280289
result = hashTableFind(file->properties, &pair);
281290
if (result == NULL) {
282291
return NULL;
283292
}
284-
293+
285294
return result->value;
286295
}
287296

288297

289-
void
298+
void
290299
props_file_do(j9props_file_t file, j9props_file_iterator iterator, void* userData)
291300
{
292301
J9HashTableState walkState;
293302
KeyValuePair* pair;
294-
303+
295304
pair = hashTableStartDo(file->properties, &walkState);
296305
while (pair != NULL) {
297306
BOOLEAN keepGoing = (*iterator)(file, pair->key, pair->value, userData);

0 commit comments

Comments
 (0)