patch-lib_morph_c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. $OpenBSD: patch-lib_morph_c,v 1.1 2008/09/01 20:02:53 sthen Exp $
  2. --- lib/morph.c.orig Wed Jan 3 21:51:35 2007
  3. +++ lib/morph.c Mon Sep 1 20:53:39 2008
  4. @@ -51,24 +51,24 @@ static struct {
  5. char *str;
  6. int strlen;
  7. } prepositions[NUMPREPS] = {
  8. - "to", 2,
  9. - "at", 2,
  10. - "of", 2,
  11. - "on", 2,
  12. - "off", 3,
  13. - "in", 2,
  14. - "out", 3,
  15. - "up", 2,
  16. - "down", 4,
  17. - "from", 4,
  18. - "with", 4,
  19. - "into", 4,
  20. - "for", 3,
  21. - "about", 5,
  22. - "between", 7,
  23. + { "to", 2 },
  24. + { "at", 2 },
  25. + { "of", 2 },
  26. + { "on", 2 },
  27. + { "off", 3 },
  28. + { "in", 2 },
  29. + { "out", 3 },
  30. + { "up", 2 },
  31. + { "down", 4 },
  32. + { "from", 4 },
  33. + { "with", 4 },
  34. + { "into", 4 },
  35. + { "for", 3 },
  36. + { "about", 5 },
  37. + { "between", 7 }
  38. };
  39. -static FILE *exc_fps[NUMPARTS + 1];
  40. +static FILE *exc_fps[NUMPARTS];
  41. static int do_init();
  42. static int strend(char *, char *);
  43. @@ -100,7 +100,7 @@ int re_morphinit(void)
  44. {
  45. int i;
  46. - for (i = 1; i <= NUMPARTS; i++) {
  47. + for (i = 0; i < NUMPARTS; i++) {
  48. if (exc_fps[i] != NULL) {
  49. fclose(exc_fps[i]); exc_fps[i] = NULL;
  50. }
  51. @@ -144,18 +144,19 @@ static int do_init(void)
  52. } else
  53. sprintf(searchdir, DEFAULTPATH);
  54. #else
  55. - if ((env = getenv("WNSEARCHDIR")) != NULL)
  56. - strcpy(searchdir, env);
  57. - else if ((env = getenv("WNHOME")) != NULL)
  58. - sprintf(searchdir, "%s%s", env, DICTDIR);
  59. - else
  60. + if ((env = getenv("WNSEARCHDIR")) != NULL) {
  61. + snprintf(searchdir, sizeof(searchdir), "%s", env);
  62. + } else if ((env = getenv("WNHOME")) != NULL) {
  63. + snprintf(searchdir, sizeof(searchdir), "%s%s", env, DICTDIR);
  64. + } else {
  65. strcpy(searchdir, DEFAULTPATH);
  66. + }
  67. #endif
  68. - for (i = 1; i <= NUMPARTS; i++) {
  69. - sprintf(fname, EXCFILE, searchdir, partnames[i]);
  70. + for (i = 0; i < NUMPARTS; i++) {
  71. + snprintf(fname, sizeof(fname), EXCFILE, searchdir, partnames[i+1]);
  72. if ((exc_fps[i] = fopen(fname, "r")) == NULL) {
  73. - sprintf(msgbuf,
  74. + snprintf(msgbuf, sizeof(msgbuf),
  75. "WordNet library error: Can't open exception file(%s)\n\n",
  76. fname);
  77. display_message(msgbuf);
  78. @@ -178,13 +179,16 @@ char *morphstr(char *origstr, int pos)
  79. int prep;
  80. char *end_idx1, *end_idx2;
  81. char *append;
  82. -
  83. +
  84. if (pos == SATELLITE)
  85. pos = ADJ;
  86. /* First time through for this string */
  87. if (origstr != NULL) {
  88. + if (strlen(origstr) > WORDBUF - 1)
  89. + return(NULL);
  90. +
  91. /* Assume string hasn't had spaces substitued with '_' */
  92. strtolower(strsubst(strcpy(str, origstr), ' ', '_'));
  93. searchstr[0] = '\0';
  94. @@ -232,7 +236,7 @@ char *morphstr(char *origstr, int pos)
  95. if (end_idx < 0) return(NULL); /* shouldn't do this */
  96. strncpy(word, str + st_idx, end_idx - st_idx);
  97. word[end_idx - st_idx] = '\0';
  98. - if(tmp = morphword(word, pos))
  99. + if ((tmp = morphword(word, pos)) != NULL)
  100. strcat(searchstr,tmp);
  101. else
  102. strcat(searchstr,word);
  103. @@ -240,7 +244,7 @@ char *morphstr(char *origstr, int pos)
  104. st_idx = end_idx + 1;
  105. }
  106. - if(tmp = morphword(strcpy(word, str + st_idx), pos))
  107. + if ((tmp = morphword(strcpy(word, str + st_idx), pos)) != NULL)
  108. strcat(searchstr,tmp);
  109. else
  110. strcat(searchstr,word);
  111. @@ -270,16 +274,15 @@ char *morphword(char *word, int pos)
  112. {
  113. int offset, cnt;
  114. int i;
  115. - static char retval[WORDBUF];
  116. - char *tmp, tmpbuf[WORDBUF], *end;
  117. -
  118. - sprintf(retval,"");
  119. - sprintf(tmpbuf, "");
  120. - end = "";
  121. -
  122. + static char retval[WORDBUF] = "";
  123. + char *tmp, tmpbuf[WORDBUF] = "", *end = "";
  124. +
  125. if(word == NULL)
  126. return(NULL);
  127. + if (strlen(word) > WORDBUF - 1)
  128. + return(NULL);
  129. +
  130. /* first look for word on exception list */
  131. if((tmp = exc_lookup(word, pos)) != NULL)
  132. @@ -335,7 +338,10 @@ static char *wordbase(char *word, int ender)
  133. {
  134. char *pt1;
  135. static char copy[WORDBUF];
  136. -
  137. +
  138. + if (strlen(word) > WORDBUF - 1)
  139. + return(NULL);
  140. +
  141. strcpy(copy, word);
  142. if(strend(copy,sufx[ender])) {
  143. pt1=strchr(copy,'\0');
  144. @@ -368,13 +374,14 @@ static char *exc_lookup(char *word, int pos)
  145. {
  146. static char line[WORDBUF], *beglp, *endlp;
  147. char *excline;
  148. - int found = 0;
  149. if (exc_fps[pos] == NULL)
  150. return(NULL);
  151. /* first time through load line from exception file */
  152. if(word != NULL){
  153. + if (strlen(word) > WORDBUF - 1)
  154. + return(NULL);
  155. if ((excline = bin_search(word, exc_fps[pos])) != NULL) {
  156. strcpy(line, excline);
  157. endlp = strchr(line,' ');
  158. @@ -403,6 +410,9 @@ static char *morphprep(char *s)
  159. char word[WORDBUF], end[WORDBUF];
  160. static char retval[WORDBUF];
  161. + if (strlen(s) > WORDBUF - 1)
  162. + return (NULL);
  163. +
  164. /* Assume that the verb is the first word in the phrase. Strip it
  165. off, check for validity, then try various morphs with the
  166. rest of the phrase tacked on, trying to find a match. */
  167. @@ -410,7 +420,7 @@ static char *morphprep(char *s)
  168. rest = strchr(s, '_');
  169. last = strrchr(s, '_');
  170. if (rest != last) { /* more than 2 words */
  171. - if (lastwd = morphword(last + 1, NOUN)) {
  172. + if ((lastwd = morphword(last + 1, NOUN)) != NULL) {
  173. strncpy(end, rest, last - rest + 1);
  174. end[last-rest+1] = '\0';
  175. strcat(end, lastwd);