arrays - Segmentation fault on sprintf in C -


This function is in the socket server when the customer sends a query, the server takes the query and finds matches from the link list . For the first few questions, the function works fine, and then a split mistake occurs on the problem sprintf call ("before sprint. \ N"). I really do not think that this only works for a while. what did I do?

  four * search node (four * query) {int i, isFound, count = 0; Node * temp = head; Char * searchResult = calloc (1, sizeof (* searchResult)); Char * Last result = Calloco (1, size (* last result)) ;; Printf ("before search node. \ N"); While (temp-> Next) {isFound = TRUE; Temp = temp- & gt; next; (I = 0; i & lt; strlen (query); i ++) {/ * Compare each letter in both stars * / if (tolower (query [i])! = Tolower (temp-> gt; ; Khanam [i])) {IsFound = FALSE; break; }} If (ifFound == true) {/ * If a match is found, then write it in the temporary string * / printf ("Match found. \ N"); SearchResult = realloc (searchResult, strlen (searchResult) + 1 + strlen (nodeostring (temp)) + 1); Printf ("before sprintf. \ N"); Sprintf (searchResult, "% s% s", searchResult, nodtostring (temp)); Count ++; / * Calculate the number of results * /}} printf ("before finalizing the string. \ N"); If (calculation> gt; 0) {/ * if at least one result is found, then combine all the results with a headline * / sprintf (the end result, "% d food items found"). \ N \ N ", counting); Dome (end result, search references); Free (searchResult); Final return result; } / * If no mail is found, return this message / / "No food items were found. \ NPlease check your spelling and try again. \ N"; }  

I do not know when the search was passed as the latter Logic is - Arguments. My system's man page shows that it is undefined:

C99 and POSIX.1-2001 specify that if the result is sprintf (), snprintf (), vsprintf (), or Copying between vsnprintf () overlap objects will cause the copy (for example, if the target string array and one of the given input inframents see only one buffer).

You probably should use the form instead.


Comments