Yes, this is a legitimate question, despite Job saying that he repents in 42:6. After all, the word repent only means to change course and does not necessarily imply sin. For example, in Genesis 6:6 God repents of having created man. In Ex. 32:14 it says that the Lord, "repented of the evil which he said he would do unto his people." Since we know that God can't sin, then repenting doesn't necessarily imply that a sin occurred. It might simply imply that a person changed his mind or his planned course of action. However, since Job repented "in dust and ashes" it seems that he was repenting of a sin. If he was merely changing course then dust and ashes would hardly have been necessary. This was mourning added to repentance.
So it seems Job sinned. Ok, when? We know he got through the initial shock of his trial without sin (1:22, 2:10). Most put it in his final speech, chapters 29-31. After all, he says some pretty shocking things about God in this speech. When we read it, we almost flinch back from the page in fear of being too near when the thunderbolt hits. Job 30:20-23 "I cry to you for help and you do not answer me; I stand, and you only look at me. You have turned cruel to me; with the might of your hand you persecute me. You lift me up on the wind; you make me ride on it, and you toss me about in the roar of the storm. For I know that you will bring me to death and to the house appointed for all living." Even worse is 31:6 "Let me be weighed in a just balance, and let God know my integrity!" Is Job actually implying that God might not be just when He judges Job?! Good grief! Surely this is where Job sinned.
Except it's not. And, yes, I can be very positive about that. You see, the same language -- if not worse -- is used in the Psalms and in other poems of lamentation by inspired writers. For example: Ps. 73:13-14 "All in vain have I kept my heart clean and washed my hands in innocence. For all the day long I have been stricken and rebuked every morning." Psa 13:1-2 To the choirmaster. A Psalm of David. "How long, O LORD? Will you forget me forever? How long will you hide your face from me? How long must I take counsel in my soul and have sorrow in my heart all the day? How long shall my enemy be exalted over me?" Psa 35:15-17 "But at my stumbling they rejoiced and gathered; they gathered together against me; wretches whom I did not know tore at me without ceasing; like profane mockers at a feast, they gnash at me with their teeth. How long, O Lord, will you look on? Rescue me from their destruction, my precious life from the lions!" Psa 35:22-23 "You have seen, O LORD; be not silent! O Lord, be not far from me! Awake and rouse yourself for my vindication, for my cause, my God and my Lord!" Asaph claims that to live righteously is vanity since he is punished anyway. Is this not hinting that God is unjust? David continually asks "How long?" wondering why God isn't meeting out justice and implying that following God doesn't pay off. God just watches why we suffer.
This is idea for idea, if not word for word, what Job is expressing. And the psalmists were inspired by the Holy Spirit to write those words. So those words CAN'T be sin. Expressing doubts to God, crying out to Him in agony, asking why and saying that none of this seems fair is not sinful, because God inspired people to write down those expressions to Him, and God cannot sin or cause to sin (James 1:13). In fact, it seems that God wants us to bring those thoughts to Him. He wants us to cry to Him, to express our pain to Him. He even wants to hear our doubts and our disappointments in Him, and the times we are angry with Him. Maybe because if we are expressing those feelings to Him, then we are still talking to Him. (How great is our God that He doesn't get so easily offended like all the gods in mythology, but rather welcomes our expressions of pain and doubt! He cares how we feel and wants us to tell Him.) So, if we are hurting for some reason, if we don't understand what is happening in our lives and why God is allowing the bad things to happen we can go to Him with those questions. We don't have to be afraid to express doubt, discouragement, fear and/or confusion to our God. He wants us to tell Him and He showed us so in His inspired word. And that is awesome.
Well, then, when did Job sin? At the end of chapter 31. Chapter 31 is written in the style of an official defense in a court during Job's time. He gets carried away in his proclamations of innocence and begins to demand God's answer rather than pleading for it. He even challenges God to indict him. Job drops his humility before God and proclaims that he will march in before God and tell Him what's what. Job 31:35-37 "Oh, that I had one to hear me! (Here is my signature! Let the Almighty answer me!) Oh, that I had the indictment written by my adversary! Surely I would carry it on my shoulder; I would bind it on me as a crown; I would give him an account of all my steps; like a prince I would approach him." When God answers Job in chapters 38-41 it is this defiance of Job that is repeatedly rebuked (38:3, 40:2-3, 7-8). None of Job's other questions, expressions of anguish or disappointment, or confusion are ever mentioned.
So, when we are hurting it is ok to be afraid, to be confused as to why God is allowing these things to happen, to be disappointed in God's lack of action, to question why, why, why and to take those questions and thoughts to Him. It is not ok to forget our place, to demand action from God and to declare that we know better than He. If we go down that road we might just wind up in the "dust and ashes."