Skip to content

Commit

Permalink
Merge pull request #6 from suzukiplan/v1.03
Browse files Browse the repository at this point in the history
ensures duplicate memory of MML string
  • Loading branch information
suzukiplan committed Feb 11, 2016
2 parents 53914d2 + 12277bd commit e25c704
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changes

## Version 1.03
- ensures duplicate memory of MML string: https://github.com/AmamiyaRinyuki/vgs-bgm-plugins/issues/1

## Version 1.02
- added '\0' terminated check
- bugfix: https://github.com/suzukiplan/vgs-mml-compiler/issues/3
Expand Down
15 changes: 10 additions & 5 deletions src/vgsmml.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ struct VgsBgmData* __stdcall vgsmml_compile_from_memory(void* data, size_t size,
int nLine, cLine;
int i;
int* pos;
char* buf = (char*)data;
char* buf;

if (NULL == err) {
return NULL;
Expand All @@ -161,12 +161,15 @@ struct VgsBgmData* __stdcall vgsmml_compile_from_memory(void* data, size_t size,
return NULL;
}

/* memory check */
if ('\0' != ((char*)data)[size - 1]) {
strcpy(err->message, "need specify the \'\\0\' terminated string to the data argument.");
err->code = VGSMML_ERR_INVALID;
/* ensures duplicate memory */
buf = (char*)malloc(size + 1);
if (NULL == buf) {
strcpy(err->message, "no memory");
err->code = VGSMML_ERR_NO_MEMORY;
return NULL;
}
memcpy(buf, data, size);
buf[size] = '\0';

/* count line */
for (nLine = 1, i = 0; buf[i]; i++) {
Expand All @@ -177,6 +180,7 @@ struct VgsBgmData* __stdcall vgsmml_compile_from_memory(void* data, size_t size,
if (NULL == (pos = (int*)malloc((nLine + 1) * sizeof(int)))) {
strcpy(err->message, "no memory");
err->code = VGSMML_ERR_NO_MEMORY;
free(buf);
return NULL;
}

Expand All @@ -203,6 +207,7 @@ struct VgsBgmData* __stdcall vgsmml_compile_from_memory(void* data, size_t size,
/* execute next phase */
result = phase3(buf, pos, err);
free(pos);
free(buf);
return result;
}

Expand Down
5 changes: 2 additions & 3 deletions test/test_memchk.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ int main(int argc, char* argv[])
struct VgsMmlErrorInfo err;
char foo[4];
strcpy(foo, "foo");
if (NULL == vgsmml_compile_from_memory(foo, 3, &err) && VGSMML_ERR_INVALID == err.code) {
puts(err.message);
puts("test succeed");
if (NULL == vgsmml_compile_from_memory(foo, 3, &err) && VGSMML_ERR_SYNTAX_UNKNOWN == err.code && 1 == err.line) {
printf("%s (code=%d, line=%d)\n", err.message, err.code, err.line);
return 0;
}
puts("test failed");
Expand Down

0 comments on commit e25c704

Please sign in to comment.