Here are some patches for vocab.c and resource.c. Changelog is below.
2001-04-10 Matt Hargett <[EMAIL PROTECTED]>
* vocab.c: Added NULL pointer checks all over the place.
* vocab.c: All declarations of pointers should now be initialized to NULL.
* vocab.c (vocab_decypher_said_block): Added default to switch.
* resource.c: Added NULL pointer checks all over the place.
* resource.c: All declarations of pointers should now be initialized to
NULL.
* resource.c (resourceLoader): removed unused autodetect parameter.
--
http://www.clock.org/~matt
-- Attached file included as plaintext by Listar --
-- File: vocab.c.patch
--- vocab.c.old Tue Apr 10 14:15:30 2001
+++ vocab.c Tue Apr 10 14:52:22 2001
@@ -65,6 +65,16 @@ char *class_names[] =
int
_vocab_cmp_words(const void *word1, const void *word2)
{
+ if (NULL == word1)
+ {
+ fprintf(stderr, "vocab.c: _vocab_cmp_words(): NULL passed for parameter
+word1\r\n");
+ }
+
+ if (NULL == word2)
+ {
+ fprintf(stderr, "vocab.c: _vocab_cmp_words(): NULL passed for parameter
+word2\r\n");
+ }
+
return strcasecmp((*((word_t **) word1))->word, (*((word_t **) word2))->word);
}
@@ -75,11 +85,17 @@ vocab_get_words_sci1(int *word_counter)
{
int counter = 0;
int seeker;
- word_t **words;
+ word_t **words = NULL;
char currentword[256] = ""; /* They're not going to use words longer than 255 ;-) */
int currentwordpos = 0;
+ if (NULL == word_counter)
+ {
+ fprintf(stderr, "vocab.c: vocab_get_words_sci1(): NULL passed for parameter
+word_counter\r\n");
+ return(NULL);
+ }
+
resource_t *resource = findResource(sci_vocab, VOCAB_RESOURCE_SCI1_MAIN_VOCAB);
vocab_version = 1;
@@ -97,6 +113,12 @@ vocab_get_words_sci1(int *word_counter)
}
words = malloc(sizeof(word_t *));
+ if (NULL == words)
+ {
+ fprintf(stderr, "vocab.c: vocab_get_words_sci1(): malloc failed for words\r\n");
+ return(NULL);
+ }
+
while (seeker < resource->length) {
byte c;
@@ -115,6 +137,13 @@ vocab_get_words_sci1(int *word_counter)
words[counter] = malloc(sizeof(word_t) + currentwordpos);
/* Allocate more memory, so that the word fits into the structure */
+ if (NULL == words[counter])
+ {
+ fprintf(stderr, "vocab.c: vocab_get_words_sci1(): malloc failed for
+words[counter]\r\n");
+ return(NULL);
+ }
+
+
strcpy(&(words[counter]->word[0]), &(currentword[0])); /* Copy the word */
/* Now decode class and group: */
@@ -141,13 +170,20 @@ vocab_get_words(int *word_counter)
{
int counter = 0;
int seeker;
- word_t **words;
+ word_t **words = NULL;
char currentword[256] = ""; /* They're not going to use words longer than 255 ;-) */
int currentwordpos = 0;
resource_t *resource = findResource(sci_vocab, VOCAB_RESOURCE_SCI0_MAIN_VOCAB);
+ if (NULL == word_counter)
+ {
+ fprintf(stderr, "vocab.c: vocab_get_words(): NULL passed for parameter
+word_counter\r\n");
+ return(NULL);
+ }
+
+
if (!resource) {
fprintf(stderr,"SCI0: Could not find a main vocabulary, trying SCI01.\n");
return vocab_get_words_sci1(word_counter); /* NOT critical: SCI1 games and some
demos don't have one! */
@@ -163,6 +199,14 @@ vocab_get_words(int *word_counter)
words = malloc(sizeof(word_t *));
+ if (NULL == words)
+ {
+ fprintf(stderr, "vocab.c: vocab_get_words(): malloc failed for words\r\n");
+ return(NULL);
+ }
+
+
+
while (seeker < resource->length) {
byte c;
@@ -315,7 +359,7 @@ parse_tree_branch_t *
vocab_get_branches(int *branches_nr)
{
resource_t *resource = findResource(sci_vocab, VOCAB_RESOURCE_PARSE_TREE_BRANCHES);
- parse_tree_branch_t *retval;
+ parse_tree_branch_t *retval = NULL;
int i;
if (!resource) {
@@ -331,6 +375,12 @@ vocab_get_branches(int *branches_nr)
}
retval = malloc(sizeof(parse_tree_branch_t) * *branches_nr);
+ if (NULL == retval)
+ {
+ fprintf(stderr, "vocab.c: vocab_get_branches(): malloc failed for retval\r\n");
+ return(NULL);
+ }
+
for (i = 0; i < *branches_nr; i++) {
int k;
@@ -357,17 +407,48 @@ vocab_lookup_word(char *word, int word_l
word_t **words, int words_nr,
suffix_t **suffices, int suffices_nr)
{
- word_t *tempword = malloc(sizeof(word_t) + word_len + 256);
- /* 256: For suffices. Should suffice. */
- word_t **dict_word;
- result_word_t *retval;
- char *tester;
+
+ word_t **dict_word = NULL;
+ result_word_t *retval = NULL;
+ char *tester = NULL;
int i;
+
+ /* 256: For suffices. Should suffice. */
+ word_t *tempword = malloc(sizeof(word_t) + word_len + 256);
+ if (NULL == tempword)
+ {
+ fprintf(stderr, "vocab.c: vocab_lookup_word(): malloc failed for tempword\r\n");
+ return(NULL);
+ }
+
+ if (NULL == word)
+ {
+ fprintf(stderr, "vocab.c: vocab_lookup_word(): NULL passed for parameter
+word\r\n");
+ return(NULL);
+ }
+
+ if (NULL == words)
+ {
+ fprintf(stderr, "vocab.c: vocab_lookup_word(): NULL passed for parameter
+words\r\n");
+ return(NULL);
+ }
+
+ if (NULL == suffices)
+ {
+ fprintf(stderr, "vocab.c: vocab_lookup_word(): NULL passed for parameter
+suffices\r\n");
+ return(NULL);
+ }
strncpy(&(tempword->word[0]), word, word_len);
tempword->word[word_len] = 0;
retval = malloc(sizeof(result_word_t));
+ if (NULL == retval)
+ {
+ fprintf(stderr, "vocab.c: vocab_lookup_word(): malloc failed for retval\r\n");
+ return(NULL);
+ }
+
dict_word = bsearch(&tempword, words, words_nr, sizeof(word_t *), _vocab_cmp_words);
@@ -443,19 +524,22 @@ vocab_decypher_said_block(state_t *s, he
nextitem);
nextitem = 42; /* Make sure that group 0xff doesn't abort */
- } else switch(nextitem) {
- case 0xf0: sciprintf(" ,"); break;
- case 0xf1: sciprintf(" &"); break;
- case 0xf2: sciprintf(" /"); break;
- case 0xf3: sciprintf(" ("); break;
- case 0xf4: sciprintf(" )"); break;
- case 0xf5: sciprintf(" ["); break;
- case 0xf6: sciprintf(" ]"); break;
- case 0xf7: sciprintf(" #"); break;
- case 0xf8: sciprintf(" <"); break;
- case 0xf9: sciprintf(" >"); break;
- case 0xff: break;
- }
+ } else
+ switch(nextitem)
+ {
+ case 0xf0: sciprintf(" ,"); break;
+ case 0xf1: sciprintf(" &"); break;
+ case 0xf2: sciprintf(" /"); break;
+ case 0xf3: sciprintf(" ("); break;
+ case 0xf4: sciprintf(" )"); break;
+ case 0xf5: sciprintf(" ["); break;
+ case 0xf6: sciprintf(" ]"); break;
+ case 0xf7: sciprintf(" #"); break;
+ case 0xf8: sciprintf(" <"); break;
+ case 0xf9: sciprintf(" >"); break;
+ case 0xff: break;
+ default: sciprintf(" [Err: %02]", nextitem);
+ }
} while (nextitem != 0xff);
sciprintf("\n");
@@ -572,8 +656,13 @@ vocab_tokenize_string(char *sentence, in
int wordlen = 0;
result_word_t *retval = malloc(sizeof(result_word_t));
/* malloc'd size is always one result_word_t too big */
+ if (NULL ==retval)
+ {
+ fprintf(stderr, "vocab.c: vocab_tokenize_string(): malloc failed for retval\r\n");
+ return(NULL);
+ }
- result_word_t *lookup_result;
+ result_word_t *lookup_result;
*result_nr = 0;
@@ -597,6 +686,14 @@ vocab_tokenize_string(char *sentence, in
if (!lookup_result) { /* Not found? */
*error = calloc(wordlen + 1, 1);
+
+ if (NULL == error)
+ {
+ fprintf(stderr, "vocab.c: vocab_tokenize_string(): calloc failed for
+error\r\n");
+ return(NULL);
+ }
+
+
strncpy(*error, lastword, wordlen); /* Set the offending word */
free(retval);
return NULL; /* And return with error */
@@ -629,6 +726,11 @@ vocab_tokenize_string(char *sentence, in
void
_vocab_recursive_ptree_dump_treelike(parse_tree_node_t *nodes, int nr, int prevnr)
{
+ if (NULL == nodes)
+ {
+ fprintf(stderr, "vocab.c: _vocab_recursive_ptree_dump_treelike(): NULL passed for
+parameter nodes\r\n");
+ }
+
if ((nr > VOCAB_TREE_NODES)/* || (nr < prevnr)*/) {
sciprintf("Error(%04x)", nr);
return;
-- Attached file included as plaintext by Listar --
-- File: resource.c.patch
--- resource.c.old Tue Apr 10 13:12:03 2001
+++ resource.c Tue Apr 10 14:07:37 2001
@@ -89,7 +89,7 @@ struct singly_linked_resources_struct {
struct singly_linked_resources_struct *next;
};
-int resourceLoader(int decompress(resource_t *result, int resh), int autodetect, int
allow_patches);
+int resourceLoader(int decompress(resource_t *result, int resh), int allow_patches);
void killlist(struct singly_linked_resources_struct *rs);
@@ -128,7 +128,8 @@ int loadResources(int version, int allow
#ifdef _SCI_RESOURCE_DEBUG
if (autodetect) fprintf(stderr, "Autodetect: Trying %s\n",
SCI_Version_Types[sci_version]);
#endif
- if ((retval = resourceLoader(decompressors[sci_version], autodetect,
allow_patches))) {
+ retval = resourceLoader(decompressors[sci_version], allow_patches);
+ if (retval) {
freeResources();
if (autodetect == ((retval == SCI_ERROR_UNKNOWN_COMPRESSION)
|| (retval ==
SCI_ERROR_DECOMPRESSION_OVERFLOW)
@@ -151,7 +152,19 @@ _addResource(struct singly_linked_resour
** the other resource will be free()d and replaced by the new [resource].
*/
{
- struct singly_linked_resources_struct *seeker;
+ struct singly_linked_resources_struct *seeker = NULL;
+
+ if (NULL == base)
+ {
+ fprintf(stderr, "resource.c: _addResource(): NULL passed for parameter
+base\r\n");
+ }
+
+ if (NULL == resource)
+
+ {
+ fprintf(stderr, "resource.c: _addResource(): NULL passed for parameter
+resource\r\n");
+ }
+
if (!base->resource) {
base->resource = resource;
@@ -189,30 +202,44 @@ _addResource(struct singly_linked_resour
int
-resourceLoader(int decompress(resource_t *result, int resh), int autodetect, int
allow_patches)
+resourceLoader(int decompress(resource_t *result, int resh), int allow_patches)
{
int resourceFile = 0;
int resourceFileCounter = 0;
- resource_t *resource;
+ resource_t *resource = NULL;
char filename[13];
int resourceCounter;
- struct singly_linked_resources_struct *seeker;
+ struct singly_linked_resources_struct *seeker = NULL;
struct singly_linked_resources_struct base;
int found_resfiles = 0;
base.next = 0;
base.resource = 0;
+ if (NULL == decompress)
+ {
+ fprintf("resource.c: resourceLoader(): NULL passed for parameter
+decompress\r\n");
+ }
+
do {
- if (resourceFileCounter > 0 && resourceFile > 0) {
+ if ((resourceFileCounter > 0) && (resourceFile > 0)) {
int decomperr;
resource = malloc(sizeof(resource_t));
+ if (NULL == resource)
+ {
+ fprintf(stderr, "resource.c: resourceLoader(): malloc
+failed for resource\r\n");
+ }
+
while (!(decomperr = (*decompress)(resource, resourceFile))) {
_addResource(&base, resource, 0);
found_resfiles = 1;
resource = malloc(sizeof(resource_t));
+ if (NULL == resource)
+ {
+ fprintf(stderr, "resource.c: resourceLoader():
+malloc failed for resource\r\n");
+ }
}
free(resource);
close(resourceFile);
@@ -262,6 +289,11 @@ resourceLoader(int decompress(resource_t
resource_map = malloc(max_resource * sizeof(resource_t));
+ if (NULL == resource_map)
+ {
+ fprintf(stderr, "resource.c: malloc failed for resource_map\r\n");
+ }
+
seeker = &base;
resourceCounter = 0;
@@ -290,6 +322,11 @@ resourceLoader(int decompress(resource_t
void killlist(struct singly_linked_resources_struct *rs)
{
+ if (NULL == rs)
+ {
+ fprintf(stderr, "resource.c: killlist(): NULL passed for parameter
+rs\r\n");
+ }
+
if (rs->next) {
killlist(rs->next);
free(rs->next);
@@ -361,13 +398,21 @@ int loadResourcePatches(struct singly_li
perror("""__FILE__"": (""__LINE__""): open()");
else {
- read(file, filehdr, 2);
+ if (read(file, filehdr, 2) <=0)
+ {
+ fprintf(stderr, "resource.c:
+loadResourcePatches(): read underrun or premature EOF in %s",file);
+ fprintf("\r\n");
+ }
if ((filehdr[0] & 0x7f) != restype) {
printf("Resource type mismatch\n");
close(file);
} else {
newrsc = malloc(sizeof(resource_t));
+ if (NULL == newrsc)
+ {
+ fprintf(stderr, "resource.c:
+loadResourcePatches(): malloc failed for newsrc\r\n");
+ }
newrsc->length = filestat.st_size - 2;
newrsc->id = restype << 11 | resnumber;
newrsc->number = resnumber;
@@ -375,7 +420,10 @@ int loadResourcePatches(struct singly_li
newrsc->type = restype;
newrsc->data = malloc(newrsc->length);
- read(file, newrsc->data,
newrsc->length);
+ if (read(file, newrsc->data,
+newrsc->length) <=0)
+ {
+ fprintf(stderr, "resource.c:
+loadResourcePatches(): read underrun or premature EOF in %s\r\n",file);
+ }
close(file);
_addResource(resourcelist, newrsc, 1);
/* Add and overwrite old stuff */
@@ -396,6 +444,16 @@ int loadResourcePatches(struct singly_li
int resourcecmp (const void *first, const void *second)
{
+ if (NULL == first)
+ {
+ fprintf(stderr, "resource.c: resourcecmp(): NULL passed for
+first\r\n");
+ }
+
+ if (NULL == second)
+ {
+ fprintf(stderr, "resource.c: resourcecmp(): NULL passed for
+second\r\n");
+ }
+
if (((resource_t *)first)->type ==
((resource_t *)second)->type)
return (((resource_t *)first)->number <
@@ -409,7 +467,7 @@ int resourcecmp (const void *first, cons
resource_t *findResource(unsigned short type, unsigned short number)
{
- resource_t binseeker, *retval;
+ resource_t binseeker, *retval = NULL;
binseeker.type = type;
binseeker.number = number;
retval = (resource_t *)